Skip to content

Commit

Permalink
fix(node/assert): enable test-assert-fail.js and align assert.fail to…
Browse files Browse the repository at this point in the history
… it (#874)

Co-authored-by: Yoshiya Hinosawa <[email protected]>
  • Loading branch information
uki00a and kt3k authored Apr 28, 2021
1 parent ac32161 commit 2be830a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
1 change: 1 addition & 0 deletions node/_tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"tests": {
"parallel": [
"test-assert.js",
"test-assert-fail.js",
"test-event-emitter-listener-count.js"
]
},
Expand Down
47 changes: 47 additions & 0 deletions node/_tools/suites/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 15.5.1
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';

require('../common');
const assert = require('assert');

// No args
assert.throws(
() => { assert.fail(); },
{
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'Failed',
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: true,
stack: /Failed/
}
);

// One arg = message
assert.throws(() => {
assert.fail('custom message');
}, {
code: 'ERR_ASSERTION',
name: 'AssertionError',
message: 'custom message',
operator: 'fail',
actual: undefined,
expected: undefined,
generatedMessage: false
});

// One arg = Error
assert.throws(() => {
assert.fail(new TypeError('custom message'));
}, {
name: 'TypeError',
message: 'custom message'
});
16 changes: 14 additions & 2 deletions node/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,20 @@ function notDeepStrictEqual(
);
}

function fail() {
toNode(() => fail());
function fail(message?: string | Error): never {
if (typeof message === "string" || message == null) {
const err = new AssertionError({
message: message ?? "Failed",
operator: "fail",
});
if (message == null) {
// When the default error message (`"Failed"`) is used, `generatedMessage` should be set to `true`.
err.generatedMessage = true;
}
throw err;
} else {
throw message;
}
}
function match(actual: string, regexp: RegExp, message?: string | Error) {
if (arguments.length < 2) {
Expand Down

0 comments on commit 2be830a

Please sign in to comment.