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 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
21 changes: 11 additions & 10 deletions test/addons/non-node-context/test-make-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const {
// Because the `Buffer` function and its protoype property only (currently)
// exist in a Node.js instance’s main context, trying to create buffers from
// another context throws an exception.
assert.throws(
() => makeBufferInNewContext(),
(exception) => {
assert.strictEqual(exception.constructor.name, 'Error');
assert(!(exception.constructor instanceof Error));

try {
makeBufferInNewContext();
} catch (exception) {
assert.strictEqual(exception.constructor.name, 'Error');
assert(!(exception.constructor instanceof Error));

assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
assert.strictEqual(exception.message,
'Buffer is not available for the current Context');
}
assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
assert.strictEqual(exception.message,
'Buffer is not available for the current Context');
return true;
}
);
12 changes: 7 additions & 5 deletions test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ let error;

assert(error);

try {
await import(file);
} catch (e) {
assert.strictEqual(error, e);
}
await assert.rejects(
() => import(file),
(e) => {
assert.strictEqual(error, e);
return true;
}
);
})();
36 changes: 20 additions & 16 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,27 @@ 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');
}
assert.throws(
() => assert.strictEqual(1, 2),
(e) => {
assert.strictEqual(
e.message,
`${strictEqualMessageStart}\n1 !== 2\n`
);
assert.ok(e.generatedMessage, 'Message not marked as generated');
return true;
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
);

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);
}
assert.throws(
() => assert.strictEqual(1, 2, 'oh no'),
(e) => {
assert.strictEqual(e.message, 'oh no');
// Message should not be marked as generated.
assert.strictEqual(e.generatedMessage, false);
return true;
}
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
);

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

const ex = util.inspect(new Error('FAIL'), true);
assert(ex.includes('Error: FAIL'));
assert(ex.includes('[stack]'));
Expand Down
23 changes: 14 additions & 9 deletions test/parallel/test-vm-codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ const WASM_BYTES = Buffer.from(


function expectsError(fn, type) {
try {
fn();
assert.fail('expected fn to error');
} catch (err) {
if (typeof type === 'string')
assert.strictEqual(err.name, type);
else
assert(err instanceof type);
}
assert.throws(
() => {
fn();
assert.fail('expected fn to error');
},
(err) => {
if (typeof type === 'string') {
assert.strictEqual(err.name, type);
} else {
assert(err instanceof type);
}
return true;
}
);
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
}

{
Expand Down
40 changes: 23 additions & 17 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,33 @@ 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, '/')));
}
assert.throws(
() => require(`${loadOrder}file3`),
(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, '/')));
}
return true;
}
);

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, '/')));
}
assert.throws(
() => require(`${loadOrder}file7`),
(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, '/')));
}
return true;
}
);

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