Compare commits
2 Commits
40dc13b644
...
a0d46f7018
Author | SHA1 | Date |
---|---|---|
Bauke | a0d46f7018 | |
Bauke | 035cac86cc |
|
@ -4,23 +4,32 @@ import {type Result, Test, TestContext} from "./test.js";
|
|||
export async function setup(
|
||||
name: string,
|
||||
fn: (group: Group) => Promise<void>,
|
||||
options?: GroupOptions,
|
||||
): Promise<Group> {
|
||||
const group = new Group(name);
|
||||
const group = new Group(name, options);
|
||||
await fn(group);
|
||||
await group.run();
|
||||
return group;
|
||||
}
|
||||
|
||||
/** Options for test groups. */
|
||||
export type GroupOptions = {
|
||||
parallel?: boolean;
|
||||
};
|
||||
|
||||
/** A collection of tests. */
|
||||
export class Group {
|
||||
export class Group implements GroupOptions {
|
||||
public context: TestContext = new TestContext();
|
||||
public parallel: boolean;
|
||||
public results: Result[] = [];
|
||||
public tests: Test[] = [];
|
||||
|
||||
private _afterAll: (() => Promise<void>) | undefined;
|
||||
private _beforeAll: (() => Promise<void>) | undefined;
|
||||
|
||||
constructor(public name: string) {}
|
||||
constructor(public name: string, options?: GroupOptions) {
|
||||
this.parallel = options?.parallel ?? false;
|
||||
}
|
||||
|
||||
/** Set a function to run after all tests have finished. */
|
||||
afterAll(fn: Group["_afterAll"]): void {
|
||||
|
@ -48,9 +57,18 @@ export class Group {
|
|||
await this._beforeAll();
|
||||
}
|
||||
|
||||
const results = await Promise.all(
|
||||
let results: Result[];
|
||||
if (this.parallel) {
|
||||
results = await Promise.all(
|
||||
this.tests.map(async (test) => test.run(this.context)),
|
||||
);
|
||||
} else {
|
||||
results = [];
|
||||
for (const test of this.tests) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
results.push(await test.run(this.context));
|
||||
}
|
||||
}
|
||||
|
||||
if (this._afterAll !== undefined) {
|
||||
await this._afterAll();
|
||||
|
|
|
@ -1,24 +1,31 @@
|
|||
export class AssertionError extends Error {
|
||||
public readonly actual: string;
|
||||
public readonly expected: string;
|
||||
public readonly title: string | undefined;
|
||||
|
||||
constructor(message: string, actual: any, expected: any) {
|
||||
constructor(message: string, actual: any, expected: any, title?: string) {
|
||||
super(message);
|
||||
|
||||
this.actual = JSON.stringify(actual);
|
||||
this.expected = JSON.stringify(expected);
|
||||
this.title = title;
|
||||
}
|
||||
}
|
||||
|
||||
/** Test execution context with assertions. */
|
||||
export class TestContext {
|
||||
/** Assert strict equality with `===`. */
|
||||
equals<T>(actual: T, expected: T): void {
|
||||
equals<T>(actual: T, expected: T, title?: string): void {
|
||||
if (actual === expected) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new AssertionError("Failed equals assertion", actual, expected);
|
||||
throw new AssertionError(
|
||||
"Failed equals assertion",
|
||||
actual,
|
||||
expected,
|
||||
title,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,6 +127,7 @@ export class Result implements ResultData {
|
|||
|
||||
if (this.error !== undefined) {
|
||||
message += `\n %c${this.error.message}`;
|
||||
message += this.error.title === undefined ? "" : `: ${this.error.title}`;
|
||||
message += `\n | Actual: ${this.error.actual}`;
|
||||
message += `\n | Expected: ${this.error.expected}`;
|
||||
styles.push("color: pink;");
|
||||
|
|
|
@ -34,7 +34,9 @@ void setup("add", async (group) => {
|
|||
});
|
||||
});
|
||||
|
||||
void setup("subtract", async (group) => {
|
||||
void setup(
|
||||
"subtract",
|
||||
async (group) => {
|
||||
group.beforeAll(async () => {
|
||||
console.log("subtract beforeAll hook");
|
||||
});
|
||||
|
@ -52,6 +54,10 @@ void setup("subtract", async (group) => {
|
|||
});
|
||||
|
||||
group.test("subtract(1, 1) = 2", async (test) => {
|
||||
test.equals(await subtract(1, 1), 2);
|
||||
});
|
||||
test.equals(await subtract(1, 1), 2, "extra title");
|
||||
});
|
||||
},
|
||||
{
|
||||
parallel: false,
|
||||
},
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue