Skip to content

Commit 816e5de

Browse files
committed
Support ES module imports in test files
1 parent ac132a7 commit 816e5de

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

packages/testing/src/Env.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { FormatError } from "./FormatError";
55
import { Args } from "./cli/Args";
66

77
import _config from "./config.json";
8+
import { importDynamically } from "./util";
89

910
type TIndexedValue = { [key: string]: unknown };
1011

@@ -25,16 +26,10 @@ export class Env {
2526
public async call(identifier: string): Promise<void> {
2627
if (Args.parseFlag("no-env")) return;
2728

28-
try {
29-
this.api =
30-
this.api ?? ((await import(resolvePath(this.rootDirPath, _config.envModuleFilename))) as IEnvApi);
31-
} catch (err: unknown) {
32-
if ((err as { code: string }).code !== "MODULE_NOT_FOUND") {
33-
throw new FormatError(err, `Cannot evaluate environment module '${_config.envModuleFilename}'`);
34-
}
35-
this.api = {};
36-
}
37-
29+
this.api =
30+
this.api ??
31+
(await importDynamically<IEnvApi>(resolvePath(this.rootDirPath, _config.envModuleFilename))) ??
32+
{};
3833
if (!(this.api as TIndexedValue)[identifier]) {
3934
return;
4035
}

packages/testing/src/api.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { Env } from "./Env";
77

88
import { FormatError } from "./FormatError";
99

10+
import { importDynamically } from "./util";
11+
1012
import _config from "./config.json";
1113

1214
interface IRecord {
@@ -30,7 +32,7 @@ function traversePath(path: string) {
3032

3133
importMutex
3234
.lock(async () => {
33-
await import(filepath);
35+
await importDynamically(filepath);
3436
})
3537
.catch((err: Error) => {
3638
throw new FormatError(err, "Cannot evaluate test file");
@@ -89,7 +91,7 @@ export async function init(apiArg: unknown, testTargetPath: string /* , options?
8991
const testSuiteModuleReference: string = resolvePath(apiArg);
9092
!existsSync(testSuiteModuleReference)
9193
? reject(new ReferenceError(`Test suite module not found '${testSuiteModuleReference}'`))
92-
: resolve((await import(testSuiteModuleReference)) as TTestApi);
94+
: resolve(await importDynamically(testSuiteModuleReference));
9395
})
9496
: (apiArg as { [key: string]: Test });
9597

packages/testing/src/cli/cli.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Args } from "./Args";
1010
import { Printer } from "./Printer";
1111

1212
import OFFICIAL_SUITES from "./suites.json";
13+
import { importDynamically } from "../util";
1314

1415
async function runSuite(): Promise<IResults> {
1516
const commandOrTestSuiteModulePath = Args.parsePositional(0);
@@ -20,7 +21,7 @@ async function runSuite(): Promise<IResults> {
2021
}
2122

2223
if (commandOrTestSuiteModulePath === "gen") {
23-
await import("./cli.gen");
24+
await importDynamically("./cli.gen");
2425

2526
return;
2627
}
@@ -40,9 +41,10 @@ async function runSuite(): Promise<IResults> {
4041
}
4142

4243
try {
43-
const TestClass = Object.values(
44-
(await import(testSuiteModuleReference)) as { [s: string]: unknown } | ArrayLike<unknown>
45-
)[0] as { suiteTitle: string; suiteColor: TColor };
44+
const TestClass = Object.values(await importDynamically(testSuiteModuleReference))[0] as {
45+
suiteTitle: string;
46+
suiteColor: TColor;
47+
};
4648
Printer.badge(
4749
(TestClass.suiteTitle || "").replace(/( ?test(s)?)?$/i, " tests"),
4850
TestClass.suiteColor ?? [225, 225, 225]

packages/testing/src/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export async function importDynamically<T>(reference: string): Promise<T> {
2+
let resolvedReference: string;
3+
try {
4+
resolvedReference = require.resolve(reference);
5+
} catch {
6+
return null;
7+
}
8+
9+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
10+
return (await new Function(`return import("${resolvedReference}")`)()) as T;
11+
}

0 commit comments

Comments
 (0)