Last active
October 23, 2019 08:42
-
-
Save shisama/020346b82655b7ba04adfa27b9b50ad5 to your computer and use it in GitHub Desktop.
show change of assert.throws since Node.js v13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v12 | |
const assert = require('assert'); | |
const SomeError = class extends Error{}; | |
try { | |
// 第一引数でthrowされたエラーがSomeErrorか判定 | |
assert.throws(() => {throw new TypeError({})}, SomeError) | |
} catch (e) { | |
// eはTypeError | |
assert.ok(e instanceof TypeError) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// v13 | |
const assert = require('assert'); | |
const SomeError = class extends Error{}; | |
try { | |
// 第一引数でthrowされたエラーがSomeErrorか判定 | |
() => assert.throws(() => {throw new TypeError({})}, SomeError) | |
} catch (e) { | |
// eはTypeErrorではなくAssertionErrorに内包されている | |
assert.strictEqual( | |
e, | |
{ | |
generatedMessage: true, | |
actual: new TypeError({}), | |
expected: AssertionError, | |
code: 'ERR_ASSERTION', | |
name: 'AssertionError', | |
operator: 'throws', | |
message: 'The error is expected to be an instance of "AssertionError". ' + | |
'Received "TypeError"\n\nError message:\n\n[object Object]' | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment