-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
48 lines (44 loc) · 1.54 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { CurrentRuntime, Runtime } from "@cross/runtime";
/**
* Test subject
*/
export type TestSubject = (context: unknown | undefined, done: (value?: unknown) => void) => void | Promise<void>;
/**
* Runtime independent test function
*/
export interface WrappedTest {
(name: string, testFn: TestSubject, options?: WrappedTestOptions): Promise<void>;
}
/**
* Runtime independent test options
*/
export interface WrappedTestOptions {
timeout?: number; // Timeout duration in milliseconds (optional)
skip?: boolean; // Whether to skip the test (optional)
waitForCallback?: boolean; // Whether to wait for the done-callback to be called
}
let wrappedTestToUse: WrappedTest;
if (CurrentRuntime == Runtime.Deno) {
const { wrappedTest } = await import("./shims/deno.ts");
// @ts-ignore js
wrappedTestToUse = wrappedTest;
} else if (CurrentRuntime == Runtime.Node) {
const { wrappedTest } = await import("./shims/node.ts");
// @ts-ignore js
wrappedTestToUse = wrappedTest;
} else if (CurrentRuntime == Runtime.Bun) {
const { wrappedTest } = await import("./shims/bun.ts");
// @ts-ignore js
wrappedTestToUse = wrappedTest;
} else {
throw new Error("Unsupported runtime");
}
/**
* Defines and executes a single test.
* @param name - The name of the test.
* @param options? - Options for the test (structure depends on your shim)
* @param testFn - The function containing the test logic.
*/
export async function test(name: string, testFn: TestSubject, options: WrappedTestOptions = {}) {
await wrappedTestToUse(name, testFn, options);
}