Skip to content

Commit

Permalink
test: use assert.throws() or rejects() instead of try / catch
Browse files Browse the repository at this point in the history
  • Loading branch information
Masashi Hirano committed Apr 21, 2019
1 parent 52fb259 commit 823bfbe
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 71 deletions.
23 changes: 11 additions & 12 deletions test/addons/non-node-context/test-make-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +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');
return;
}
assert.fail('Missing expected exception');
assert.strictEqual(exception.code, 'ERR_BUFFER_CONTEXT_NOT_AVAILABLE');
assert.strictEqual(exception.message,
'Buffer is not available for the current Context');
return true;
}
);
14 changes: 7 additions & 7 deletions test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ let error;

assert(error);

try {
await import(file);
} catch (e) {
assert.strictEqual(error, e);
return;
}
assert.fail('Missing expected exception');
await assert.rejects(
() => import(file),
(e) => {
assert.strictEqual(error, e);
return true;
}
);
})();
28 changes: 11 additions & 17 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,34 +296,28 @@ testAssertionMessage({ a: undefined, b: null },
testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
'{\n+ a: NaN,\n+ b: Infinity,\n+ c: -Infinity\n+ }');

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

{
let threw = false;
try {
assert.strictEqual(1, 2, 'oh no');
} catch (e) {
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);
threw = true;
return true;
}
assert.ok(threw, 'Missing expected exception');
}
);

{
let threw = false;
Expand Down
15 changes: 7 additions & 8 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,13 @@ 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');
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'));
Expand Down
24 changes: 13 additions & 11 deletions test/parallel/test-vm-codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +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;
}
return;
}
assert.fail('Missing expected exception');
);
}

{
Expand Down
26 changes: 10 additions & 16 deletions test/sequential/test-module-loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,39 +201,33 @@ assert.throws(

assert.strictEqual(require(`${loadOrder}file1`).file1, 'file1');
assert.strictEqual(require(`${loadOrder}file2`).file2, 'file2.js');
{
let threw = false;
try {
require(`${loadOrder}file3`);
} catch (e) {
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, '/')));
}
threw = true;
return 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');
{
let threw = false;
try {
require(`${loadOrder}file7`);
} catch (e) {
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, '/')));
}
threw = true;
return true;
}
assert.ok(threw, 'Missing expected exception');
}
);

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

0 comments on commit 823bfbe

Please sign in to comment.