Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created June 15, 2020 05:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/b185f9fc50cbd1adf460755487f04db3 to your computer and use it in GitHub Desktop.
Save mizchi/b185f9fc50cbd1adf460755487f04db3 to your computer and use it in GitHub Desktop.
const testFns: Array<{ name: string; fn: Function }> = [];
const beforeEachs: Function[] = [];
const afterEachs: Function[] = [];
export function clear() {
testFns.length = 0;
beforeEachs.length = 0;
afterEachs.length = 0;
}
export function beforeEach(fn: Function) {
beforeEachs.push(fn);
}
export function afterEach(fn: Function) {
afterEachs.push(fn);
}
export function test(name: string, fn: Function) {
testFns.push({ name, fn });
}
type Result =
| {
type: "passed";
name: string;
}
| {
type: "failed";
name: string;
error: any;
};
export async function run() {
const results: Array<Result> = [];
for (const test of testFns) {
try {
for (const beforeFn of beforeEachs) await beforeFn();
await test.fn();
console.log(`[PASS] ${test.name}`);
results.push({ name: test.name, type: "passed" });
} catch (err) {
console.error(`[FAIL] ${test.name}`);
results.push({ name: test.name, type: "failed", error: err });
}
for (const afterFn of afterEachs) {
(await afterFn()).catch((err: any) => console.error(err));
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment