Created
June 15, 2020 05:41
-
-
Save mizchi/b185f9fc50cbd1adf460755487f04db3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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