Skip to content

Commit 9868381

Browse files
authored
chore: use throws and rejects assertions (#36)
<!-- What / Why --> <!-- Describe the request in detail. What it does and why it's being changed. --> When testing that a function throws an error, it's easy to forget to include the `fail` assertion in the try block, and end up with a test that verifies nothing. I find `throws` and `rejects` assertions to be less error prone, and try to use them instead of assertions in a catch block. Caveat: Unfortunately with tap 16, you can supply an object for comparison with the thrown error, but can't run arbitrary assertions. `throws` and `rejects` return the thrown error in newer versions of tap. ## References <!-- Examples: Related to #0 Depends on #0 Blocked by #0 Fixes #0 Closes #0 -->
1 parent 36eeeab commit 9868381

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

test/server.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,17 @@ t.test('redactThrow', async t => {
8484
throw badError
8585
}
8686
const safeHandler = redactThrow(handler)
87-
try {
88-
await safeHandler()
89-
t.fail('should throw')
90-
} catch (goodError) {
91-
t.same(goodError.sensitive, undefined, 'should not have sensitive field')
92-
}
87+
88+
await t.rejects(safeHandler, { sensitive: undefined }, 'should not have sensitive field')
9389
})
94-
t.test('invalid argument not function', async t => {
95-
try {
90+
91+
await t.test('invalid argument not function', async t => {
92+
t.throws(() => {
9693
redactThrow('hello world')
97-
t.fail('should throw')
98-
} catch (error) {
99-
t.same(error.message, 'redactThrow expects a function', 'should throw with correct message')
100-
}
94+
}, { message: 'redactThrow expects a function' }, 'should throw with correct message')
10195
})
10296

103-
t.test('ensures args are passed down', async t => {
97+
await t.test('ensures args are passed down', async t => {
10498
const handler = async (a, b, c) => {
10599
if (a !== 1) {
106100
throw new Error('a is not 1')

0 commit comments

Comments
 (0)