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: simplify tests by using assert.rejects #27123

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions test/parallel/test-stream-readable-async-iterators.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,13 @@ async function tests() {
assert.strictEqual(e, err);
}));
readable.destroy(err);
try {
await readable[Symbol.asyncIterator]().next();
} catch (e) {
assert.strictEqual(e, err);
}
await assert.rejects(
readable[Symbol.asyncIterator]().next(),
(e) => {
assert.strictEqual(e, err);
return true;
}
);
}

{
Expand Down
49 changes: 13 additions & 36 deletions test/parallel/test-vm-module-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ const assert = require('assert');

const { SourceTextModule, createContext } = require('vm');

async function expectsRejection(fn, settings) {
const validateError = common.expectsError(settings);
// Retain async context.
const storedError = new Error('Thrown from:');
try {
await fn();
} catch (err) {
try {
validateError(err);
} catch (validationError) {
console.error(validationError);
console.error('Original error:');
console.error(err);
throw storedError;
}
return;
}
assert.fail('Missing expected exception');
}

async function createEmptyLinkedModule() {
const m = new SourceTextModule('');
await m.link(common.mustNotCall());
Expand Down Expand Up @@ -57,19 +37,19 @@ async function checkArgType() {
for (const invalidLinker of [
0, 1, undefined, null, true, 'str', {}, Symbol.iterator
]) {
await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.link(invalidLinker);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
name: 'TypeError'
});
}
}

// Check methods/properties can only be used under a specific state.
async function checkModuleState() {
await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.link(common.mustNotCall());
assert.strictEqual(m.linkingStatus, 'linked');
Expand All @@ -78,7 +58,7 @@ async function checkModuleState() {
code: 'ERR_VM_MODULE_ALREADY_LINKED'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('');
m.link(common.mustNotCall());
assert.strictEqual(m.linkingStatus, 'linking');
Expand All @@ -94,15 +74,14 @@ async function checkModuleState() {
code: 'ERR_VM_MODULE_NOT_LINKED'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('import "foo";');
try {
await m.link(common.mustCall(() => ({})));
} catch {
assert.strictEqual(m.linkingStatus, 'errored');
m.instantiate();
}
assert.fail('Unreachable');
}, {
code: 'ERR_VM_MODULE_NOT_LINKED'
});
Expand All @@ -124,15 +103,15 @@ async function checkModuleState() {
await m.evaluate();
}

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.evaluate();
}, {
code: 'ERR_VM_MODULE_STATUS',
message: 'Module status must be one of instantiated, evaluated, and errored'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('');
await m.evaluate(false);
}, {
Expand All @@ -141,7 +120,7 @@ async function checkModuleState() {
'Received type boolean'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = await createEmptyLinkedModule();
await m.evaluate();
}, {
Expand All @@ -157,7 +136,7 @@ async function checkModuleState() {
message: 'Module status must be errored'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = await createEmptyLinkedModule();
m.instantiate();
await m.evaluate();
Expand All @@ -175,7 +154,7 @@ async function checkModuleState() {
message: 'Module status must not be uninstantiated or instantiating'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const m = await createEmptyLinkedModule();
m.namespace;
}, {
Expand All @@ -186,20 +165,19 @@ async function checkModuleState() {

// Check link() fails when the returned module is not valid.
async function checkLinking() {
await expectsRejection(async () => {
await assert.rejects(async () => {
const m = new SourceTextModule('import "foo";');
try {
await m.link(common.mustCall(() => ({})));
} catch (err) {
assert.strictEqual(m.linkingStatus, 'errored');
throw err;
}
assert.fail('Unreachable');
}, {
code: 'ERR_VM_MODULE_NOT_MODULE'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const c = createContext({ a: 1 });
const foo = new SourceTextModule('', { context: c });
await foo.link(common.mustNotCall());
Expand All @@ -210,12 +188,11 @@ async function checkLinking() {
assert.strictEqual(bar.linkingStatus, 'errored');
throw err;
}
assert.fail('Unreachable');
}, {
code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'
});

await expectsRejection(async () => {
await assert.rejects(async () => {
const erroredModule = new SourceTextModule('import "foo";');
try {
await erroredModule.link(common.mustCall(() => ({})));
Expand Down