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