mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 12:49:10 +00:00

Some checks are pending
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
This commit adds `Deno.test.beforeAll`, `Deno.test.beforeEach`, `Deno.test.afterAll` and `Deno.test.afterEach` APIs. These APIs can be used to perform setup and teardown for test cases. The API is similar to the Vitest API: https://vitest.dev/api/#setup-and-teardown, with the main difference being that that `before*` hooks don't return a cleanup function.
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
const logs: string[] = [];
|
|
|
|
Deno.test.beforeAll(async () => {
|
|
logs.push("beforeAll started");
|
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
logs.push("beforeAll completed");
|
|
});
|
|
|
|
let testCount = 0;
|
|
|
|
Deno.test.beforeEach(async () => {
|
|
testCount++;
|
|
logs.push(`beforeEach started for test ${testCount}`);
|
|
|
|
if (testCount === 2) {
|
|
// Async rejection
|
|
await new Promise((_, reject) => {
|
|
setTimeout(() => reject(new Error("Async error in beforeEach")), 5);
|
|
});
|
|
}
|
|
|
|
logs.push(`beforeEach completed for test ${testCount}`);
|
|
});
|
|
|
|
Deno.test.afterEach(async () => {
|
|
logs.push(`afterEach started for test ${testCount}`);
|
|
|
|
if (testCount === 3) {
|
|
// Async rejection in afterEach
|
|
await Promise.reject(new Error("Async error in afterEach"));
|
|
}
|
|
|
|
logs.push(`afterEach completed for test ${testCount}`);
|
|
});
|
|
|
|
Deno.test.afterAll(async () => {
|
|
logs.push("afterAll started");
|
|
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
logs.push("afterAll completed");
|
|
});
|
|
|
|
Deno.test("first test", () => {
|
|
logs.push("first test executed");
|
|
});
|
|
|
|
Deno.test("second test (beforeEach fails)", () => {
|
|
logs.push("second test executed");
|
|
});
|
|
|
|
Deno.test("third test (afterEach fails)", () => {
|
|
logs.push("third test executed");
|
|
});
|
|
|
|
globalThis.onunload = () => {
|
|
console.log(logs);
|
|
};
|