Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active October 23, 2019 08:42
Show Gist options
  • Save shisama/020346b82655b7ba04adfa27b9b50ad5 to your computer and use it in GitHub Desktop.
Save shisama/020346b82655b7ba04adfa27b9b50ad5 to your computer and use it in GitHub Desktop.
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