Skip to content

Commit 3d6533e

Browse files
committed
test: simplify vm-module-errors test
Use `assert.rejects` to test error cases. This simplifies the test. PR-URL: #27123 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent eb8a51a commit 3d6533e

File tree

1 file changed

+13
-36
lines changed

1 file changed

+13
-36
lines changed

test/parallel/test-vm-module-errors.js

+13-36
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@ const assert = require('assert');
88

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

11-
async function expectsRejection(fn, settings) {
12-
const validateError = common.expectsError(settings);
13-
// Retain async context.
14-
const storedError = new Error('Thrown from:');
15-
try {
16-
await fn();
17-
} catch (err) {
18-
try {
19-
validateError(err);
20-
} catch (validationError) {
21-
console.error(validationError);
22-
console.error('Original error:');
23-
console.error(err);
24-
throw storedError;
25-
}
26-
return;
27-
}
28-
assert.fail('Missing expected exception');
29-
}
30-
3111
async function createEmptyLinkedModule() {
3212
const m = new SourceTextModule('');
3313
await m.link(common.mustNotCall());
@@ -57,19 +37,19 @@ async function checkArgType() {
5737
for (const invalidLinker of [
5838
0, 1, undefined, null, true, 'str', {}, Symbol.iterator
5939
]) {
60-
await expectsRejection(async () => {
40+
await assert.rejects(async () => {
6141
const m = new SourceTextModule('');
6242
await m.link(invalidLinker);
6343
}, {
6444
code: 'ERR_INVALID_ARG_TYPE',
65-
type: TypeError
45+
name: 'TypeError'
6646
});
6747
}
6848
}
6949

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

81-
await expectsRejection(async () => {
61+
await assert.rejects(async () => {
8262
const m = new SourceTextModule('');
8363
m.link(common.mustNotCall());
8464
assert.strictEqual(m.linkingStatus, 'linking');
@@ -94,15 +74,14 @@ async function checkModuleState() {
9474
code: 'ERR_VM_MODULE_NOT_LINKED'
9575
});
9676

97-
await expectsRejection(async () => {
77+
await assert.rejects(async () => {
9878
const m = new SourceTextModule('import "foo";');
9979
try {
10080
await m.link(common.mustCall(() => ({})));
10181
} catch {
10282
assert.strictEqual(m.linkingStatus, 'errored');
10383
m.instantiate();
10484
}
105-
assert.fail('Unreachable');
10685
}, {
10786
code: 'ERR_VM_MODULE_NOT_LINKED'
10887
});
@@ -124,15 +103,15 @@ async function checkModuleState() {
124103
await m.evaluate();
125104
}
126105

127-
await expectsRejection(async () => {
106+
await assert.rejects(async () => {
128107
const m = new SourceTextModule('');
129108
await m.evaluate();
130109
}, {
131110
code: 'ERR_VM_MODULE_STATUS',
132111
message: 'Module status must be one of instantiated, evaluated, and errored'
133112
});
134113

135-
await expectsRejection(async () => {
114+
await assert.rejects(async () => {
136115
const m = new SourceTextModule('');
137116
await m.evaluate(false);
138117
}, {
@@ -141,7 +120,7 @@ async function checkModuleState() {
141120
'Received type boolean'
142121
});
143122

144-
await expectsRejection(async () => {
123+
await assert.rejects(async () => {
145124
const m = await createEmptyLinkedModule();
146125
await m.evaluate();
147126
}, {
@@ -157,7 +136,7 @@ async function checkModuleState() {
157136
message: 'Module status must be errored'
158137
});
159138

160-
await expectsRejection(async () => {
139+
await assert.rejects(async () => {
161140
const m = await createEmptyLinkedModule();
162141
m.instantiate();
163142
await m.evaluate();
@@ -175,7 +154,7 @@ async function checkModuleState() {
175154
message: 'Module status must not be uninstantiated or instantiating'
176155
});
177156

178-
await expectsRejection(async () => {
157+
await assert.rejects(async () => {
179158
const m = await createEmptyLinkedModule();
180159
m.namespace;
181160
}, {
@@ -186,20 +165,19 @@ async function checkModuleState() {
186165

187166
// Check link() fails when the returned module is not valid.
188167
async function checkLinking() {
189-
await expectsRejection(async () => {
168+
await assert.rejects(async () => {
190169
const m = new SourceTextModule('import "foo";');
191170
try {
192171
await m.link(common.mustCall(() => ({})));
193172
} catch (err) {
194173
assert.strictEqual(m.linkingStatus, 'errored');
195174
throw err;
196175
}
197-
assert.fail('Unreachable');
198176
}, {
199177
code: 'ERR_VM_MODULE_NOT_MODULE'
200178
});
201179

202-
await expectsRejection(async () => {
180+
await assert.rejects(async () => {
203181
const c = createContext({ a: 1 });
204182
const foo = new SourceTextModule('', { context: c });
205183
await foo.link(common.mustNotCall());
@@ -210,12 +188,11 @@ async function checkLinking() {
210188
assert.strictEqual(bar.linkingStatus, 'errored');
211189
throw err;
212190
}
213-
assert.fail('Unreachable');
214191
}, {
215192
code: 'ERR_VM_MODULE_DIFFERENT_CONTEXT'
216193
});
217194

218-
await expectsRejection(async () => {
195+
await assert.rejects(async () => {
219196
const erroredModule = new SourceTextModule('import "foo";');
220197
try {
221198
await erroredModule.link(common.mustCall(() => ({})));

0 commit comments

Comments
 (0)