Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): add context to beforeEach and afterEach hooks #6201

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions testing/_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export interface DescribeDefinition<T> extends Omit<Deno.TestDefinition, "fn"> {
| ((this: T) => void | Promise<void>)[];
/** Run some shared setup before each test in the suite. */
beforeEach?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
| ((this: T, context: Deno.TestContext) => void | Promise<void>)
| ((this: T, context: Deno.TestContext) => void | Promise<void>)[];
/** Run some shared teardown after each test in the suite. */
afterEach?:
| ((this: T) => void | Promise<void>)
| ((this: T) => void | Promise<void>)[];
| ((this: T, context: Deno.TestContext) => void | Promise<void>)
| ((this: T, context: Deno.TestContext) => void | Promise<void>)[];
}

/** The options for creating an individual test case with the it function. */
Expand Down Expand Up @@ -368,21 +368,21 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
if (activeIndex === 0) context = { ...context };
const { beforeEach } = testSuite.describe;
if (typeof beforeEach === "function") {
await beforeEach.call(context);
await beforeEach.call(context, t);
} else if (beforeEach) {
for (const hook of beforeEach) {
await hook.call(context);
await hook.call(context, t);
}
}
try {
await TestSuiteInternal.runTest(t, fn, context, activeIndex + 1);
} finally {
const { afterEach } = testSuite.describe;
if (typeof afterEach === "function") {
await afterEach.call(context);
await afterEach.call(context, t);
} else if (afterEach) {
for (const hook of afterEach) {
await hook.call(context);
await hook.call(context, t);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions testing/bdd_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import {
assert,
assertEquals,
assertExists,
assertMatch,
assertObjectMatch,
assertRejects,
assertStrictEquals,
Expand Down Expand Up @@ -2059,4 +2061,29 @@ Deno.test("describe()", async (t) => {
);
},
);

await t.step("context for beforeEach and afterEach", async (t) => {
await t.step(
"test context names",
async () =>
await assertOptions({}, (fns) => {
const suite = describe({
// deno-lint-ignore no-explicit-any
afterEach(this: any, ctx: Deno.TestContext) {
assertExists(ctx);
assertMatch(ctx.name, /^(a|b)/);
},
// deno-lint-ignore no-explicit-any
beforeEach(this: any, ctx: Deno.TestContext) {
assertExists(ctx);
assertMatch(ctx.name, /^(a|b)/);
},
}, function example() {
assertEquals(it({ name: "a", fn: fns[0] }), undefined);
});
assert(suite && typeof suite.symbol === "symbol");
assertEquals(it({ suite, name: "b", fn: fns[1] }), undefined);
}),
);
});
});