Skip to content

Commit 5f4e215

Browse files
committed
Fix setupTests and add support for 'setupFilesAfterEnv'
Fixes codesandbox#1800
1 parent 6d41acb commit 5f4e215

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

packages/app/src/sandbox/eval/tests/jest-lite.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,24 +234,40 @@ export default class TestRunner {
234234

235235
this.sendMessage('total_test_start');
236236

237-
let testModule = null;
237+
let testModules: Module[] = [];
238238

239239
try {
240240
if (this.manager.preset.name === react.name) {
241-
testModule = this.manager.resolveModule('./src/setupTests.js', '/');
241+
try {
242+
testModules = [
243+
this.manager.resolveModule('./src/setupTests.js', '/'),
244+
];
245+
} catch (e) {
246+
testModules = [
247+
this.manager.resolveModule('./src/setupTests.ts', '/'),
248+
];
249+
}
242250
} else if (this.manager.preset.name === reactTs.name) {
243-
testModule = this.manager.resolveModule('./src/setupTests.ts', '/');
251+
testModules = [this.manager.resolveModule('./src/setupTests.ts', '/')];
252+
} else if (this.manager.configurations.package) {
253+
const { parsed } = this.manager.configurations.package;
254+
255+
if (parsed && parsed.jest && parsed.jest.setupFilesAfterEnv) {
256+
testModules = parsed.jest.setupFilesAfterEnv.map((path: string) =>
257+
this.manager.resolveModule(path, '/')
258+
);
259+
}
244260
}
245261
} catch (e) {
246262
/* ignore */
247263
}
248264

249-
if (testModule) {
250-
await this.manager.transpileModules(testModule, true);
251-
this.manager.evaluateModule(testModule, {
252-
force: true,
253-
testGlobals: true,
254-
});
265+
if (testModules.length) {
266+
await Promise.all(
267+
testModules.map(testSetup => {
268+
return this.manager.transpileModules(testSetup, true);
269+
})
270+
);
255271
}
256272

257273
if (this.manager.modules) {
@@ -268,6 +284,15 @@ export default class TestRunner {
268284
dispatch(actions.error.clear(t.path, 'jest'));
269285

270286
try {
287+
if (testModules.length) {
288+
testModules.forEach(module => {
289+
this.manager.evaluateModule(module, {
290+
force: true,
291+
testGlobals: true,
292+
});
293+
});
294+
}
295+
271296
this.manager.evaluateModule(t, {
272297
force: true,
273298
testGlobals: true,

0 commit comments

Comments
 (0)