Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
show change of assert.throws since Node.js v13
// 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)
}
// 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
You can’t perform that action at this time.