Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node/assert): Enable test-assert-fail.js and align fail to it #874

Merged
merged 3 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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