Add a parallel option to groups.
This commit is contained in:
parent
035cac86cc
commit
a0d46f7018
|
@ -4,23 +4,32 @@ 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);
|
const group = new Group(name, options);
|
||||||
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 {
|
export class Group implements GroupOptions {
|
||||||
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) {}
|
constructor(public name: string, options?: GroupOptions) {
|
||||||
|
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 {
|
||||||
|
@ -48,9 +57,18 @@ export class Group {
|
||||||
await this._beforeAll();
|
await this._beforeAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
const results = await Promise.all(
|
let results: Result[];
|
||||||
this.tests.map(async (test) => test.run(this.context)),
|
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) {
|
if (this._afterAll !== undefined) {
|
||||||
await this._afterAll();
|
await this._afterAll();
|
||||||
|
|
|
@ -34,24 +34,30 @@ void setup("add", async (group) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
void setup("subtract", async (group) => {
|
void setup(
|
||||||
group.beforeAll(async () => {
|
"subtract",
|
||||||
console.log("subtract beforeAll hook");
|
async (group) => {
|
||||||
});
|
group.beforeAll(async () => {
|
||||||
|
console.log("subtract beforeAll hook");
|
||||||
|
});
|
||||||
|
|
||||||
group.afterAll(async () => {
|
group.afterAll(async () => {
|
||||||
console.log("subtract afterAll hook");
|
console.log("subtract afterAll hook");
|
||||||
});
|
});
|
||||||
|
|
||||||
group.test("subtract(1, 1) = 0", async (test) => {
|
group.test("subtract(1, 1) = 0", async (test) => {
|
||||||
test.equals(await subtract(1, 1), 0);
|
test.equals(await subtract(1, 1), 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
group.skip("subtract(1, 1) = 1", async (test) => {
|
group.skip("subtract(1, 1) = 1", async (test) => {
|
||||||
test.equals(await subtract(1, 1), 1);
|
test.equals(await subtract(1, 1), 1);
|
||||||
});
|
});
|
||||||
|
|
||||||
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, "extra title");
|
||||||
});
|
});
|
||||||
});
|
},
|
||||||
|
{
|
||||||
|
parallel: false,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue