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

test: fix ineffective error tests #27333

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions test/addons/non-node-context/test-make-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ try {
assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
assert.strictEqual(exception.message,
'Buffer is not available for the current Context');
return;
}
assert.fail('Missing expected exception');
2 changes: 2 additions & 0 deletions test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ let error;
await import(file);
} catch (e) {
assert.strictEqual(error, e);
return;
}
assert.fail('Missing expected exception');
})();
40 changes: 25 additions & 15 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,23 +296,33 @@ testAssertionMessage({ a: undefined, b: null },
testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
'{\n+ a: NaN,\n+ b: Infinity,\n+ c: -Infinity\n+ }');

// https://github.com/nodejs/node-v0.x-archive/issues/5292
try {
assert.strictEqual(1, 2);
} catch (e) {
assert.strictEqual(
e.message,
`${strictEqualMessageStart}\n1 !== 2\n`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
{
let threw = false;
// https://github.com/nodejs/node-v0.x-archive/issues/5292
try {
assert.strictEqual(1, 2);
} catch (e) {
assert.strictEqual(
e.message,
`${strictEqualMessageStart}\n1 !== 2\n`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
threw = true;
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
assert.ok(threw, 'Missing expected exception');
}

try {
assert.strictEqual(1, 2, 'oh no');
} catch (e) {
assert.strictEqual(e.message, 'oh no');
// Message should not be marked as generated.
assert.strictEqual(e.generatedMessage, false);
{
let threw = false;
try {
assert.strictEqual(1, 2, 'oh no');
} catch (e) {
assert.strictEqual(e.message, 'oh no');
// Message should not be marked as generated.
assert.strictEqual(e.generatedMessage, false);
threw = true;
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
assert.ok(threw, 'Missing expected exception');
}

{
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,11 +538,15 @@ assert.strictEqual(util.inspect(-5e-324), '-5e-324');
].forEach((err) => {
assert.strictEqual(util.inspect(err), err.stack);
});
let threw = false;
try {
undef(); // eslint-disable-line no-undef
} catch (e) {
assert.strictEqual(util.inspect(e), e.stack);
threw = true;
}
assert.ok(threw, 'Missing expected exception');

const ex = util.inspect(new Error('FAIL'), true);
assert(ex.includes('Error: FAIL'));
assert(ex.includes('[stack]'));
Expand Down
7 changes: 5 additions & 2 deletions test/parallel/test-vm-codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ function expectsError(fn, type) {
fn();
assert.fail('expected fn to error');
} catch (err) {
if (typeof type === 'string')
if (typeof type === 'string') {
assert.strictEqual(err.name, type);
else
} else {
assert(err instanceof type);
}
return;
}
assert.fail('Missing expected exception');
}

{
Expand Down
42 changes: 27 additions & 15 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,38 @@ assert.throws(

assert.strictEqual(require(`${loadOrder}file1`).file1, 'file1');
assert.strictEqual(require(`${loadOrder}file2`).file2, 'file2.js');
try {
require(`${loadOrder}file3`);
} catch (e) {
// Not a real .node module, but we know we require'd the right thing.
if (common.isOpenBSD) // OpenBSD errors with non-ELF object error
assert.ok(/File not an ELF object/.test(e.message.replace(backslash, '/')));
else
assert.ok(/file3\.node/.test(e.message.replace(backslash, '/')));
{
let threw = false;
try {
require(`${loadOrder}file3`);
} catch (e) {
// Not a real .node module, but we know we require'd the right thing.
if (common.isOpenBSD) { // OpenBSD errors with non-ELF object error
assert.ok(/File not an ELF object/.test(e.message.replace(backslash, '/')));
} else {
assert.ok(/file3\.node/.test(e.message.replace(backslash, '/')));
}
threw = true;
}
assert.ok(threw, 'Missing expected exception');
}

assert.strictEqual(require(`${loadOrder}file4`).file4, 'file4.reg');
assert.strictEqual(require(`${loadOrder}file5`).file5, 'file5.reg2');
assert.strictEqual(require(`${loadOrder}file6`).file6, 'file6/index.js');
try {
require(`${loadOrder}file7`);
} catch (e) {
if (common.isOpenBSD)
assert.ok(/File not an ELF object/.test(e.message.replace(backslash, '/')));
else
assert.ok(/file7\/index\.node/.test(e.message.replace(backslash, '/')));
{
let threw = false;
try {
require(`${loadOrder}file7`);
} catch (e) {
if (common.isOpenBSD) {
assert.ok(/File not an ELF object/.test(e.message.replace(backslash, '/')));
} else {
assert.ok(/file7\/index\.node/.test(e.message.replace(backslash, '/')));
}
threw = true;
}
assert.ok(threw, 'Missing expected exception');
}

assert.strictEqual(require(`${loadOrder}file8`).file8, 'file8/index.reg');
Expand Down