Skip to content

Commit

Permalink
assert: allow number|null as Error.code match
Browse files Browse the repository at this point in the history
Some errors have numeric codes, or sometimes you want to assert that you
*didn't* get a code on the Error.
  • Loading branch information
isaacs committed May 6, 2024
1 parent bf457f2 commit f7c0ec7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/asserts/src/index.ts
Expand Up @@ -34,12 +34,12 @@ export type ErrorMessageMatch = {
}

export type ErrorNameMatch = {
name: string | RegExp
name: string | RegExp | null | undefined
[k: string]: any
}

export type ErrorCodeMatch = {
code: string | RegExp
code: string | RegExp | number | null | undefined
[k: string]: any
}

Expand Down
27 changes: 27 additions & 0 deletions src/asserts/test/index.ts
Expand Up @@ -474,6 +474,15 @@ t.test('throws, doesNotThrow', t => {
),
{ message: 'ok' }
)
t.match(
a.throws(
() => {
throw Object.assign(new Error(''), { code: null, foo: 'bar' })
},
{ code: null, foo: 'bar' }
),
{ code: null, foo: 'bar' }
)

t.end()
})
Expand Down Expand Up @@ -533,6 +542,24 @@ t.test('rejects', async t => {
throw new Error('ok')
})
)
t.match(
await a.rejects(
async () => {
throw Object.assign(new Error(''), { code: null, foo: 'bar' })
},
{ code: null, foo: 'bar' }
),
{ code: null, foo: 'bar' }
)
t.match(
await a.rejects(
Promise.reject(
Object.assign(new Error(''), { code: null, foo: 'bar' })
)
),
{ code: null, foo: 'bar' },
{ code: null, foo: 'bar' }
)
})

t.test('resolves', async t => {
Expand Down

0 comments on commit f7c0ec7

Please sign in to comment.