MCPcopy Create free account
hub / github.com/denoland/std / assertHooks

Function assertHooks

testing/bdd_test.ts:1541–1597  ·  view source on GitHub ↗

* Asserts that all the different hook types are called in the correct order when the tests run. * This is used to reduce code duplication when testing calling `describe` with different call signatures.

(
      cb: (
        options: {
          beforeAllFn: Spy;
          afterAllFn: Spy;
          beforeEachFn: Spy;
          afterEachFn: Spy;
          fns: readonly [Spy, Spy];
        },
      ) => void,
    )

Source from the content-addressed store, hash-verified

1539 * This is used to reduce code duplication when testing calling `describe` with different call signatures.
1540 */
1541 async function assertHooks(
1542 cb: (
1543 options: {
1544 beforeAllFn: Spy;
1545 afterAllFn: Spy;
1546 beforeEachFn: Spy;
1547 afterEachFn: Spy;
1548 fns: readonly [Spy, Spy];
1549 },
1550 ) => void,
1551 ) {
1552 using test = stub(Deno, "test");
1553 const fns = [spy(), spy()] as const;
1554 const { beforeAllFn, afterAllFn, beforeEachFn, afterEachFn } = hookFns();
1555
1556 const context = new TestContext("example");
1557 try {
1558 cb({ beforeAllFn, afterAllFn, beforeEachFn, afterEachFn, fns });
1559
1560 assertSpyCalls(fns[0], 0);
1561 assertSpyCalls(fns[1], 0);
1562
1563 assertSpyCall(test, 0);
1564 const call = test.calls[0];
1565 const options = call?.args[0] as Deno.TestDefinition;
1566 assertEquals(Object.keys(options).sort(), ["fn", "name"]);
1567 assertEquals(options.name, "example");
1568
1569 const result = options.fn(context);
1570 assertStrictEquals(Promise.resolve(result), result);
1571 assertEquals(await result, undefined);
1572 assertSpyCalls(context.spies.step, 2);
1573 } finally {
1574 TestSuiteInternal.reset();
1575 }
1576
1577 let fn = fns[0];
1578 assertSpyCall(fn, 0, {
1579 self: { allTimer: 1, eachTimer: 2 },
1580 args: [context.steps[0]],
1581 returned: undefined,
1582 });
1583 assertSpyCalls(fn, 1);
1584
1585 fn = fns[1];
1586 assertSpyCall(fn, 0, {
1587 self: { allTimer: 1, eachTimer: 3 },
1588 args: [context.steps[1]],
1589 returned: undefined,
1590 });
1591 assertSpyCalls(fn, 1);
1592
1593 assertSpyCalls(beforeAllFn, 1);
1594 assertSpyCalls(afterAllFn, 1);
1595 assertSpyCalls(beforeEachFn, 2);
1596 assertSpyCalls(afterEachFn, 2);
1597 }
1598

Callers 1

bdd_test.tsFile · 0.85

Calls 9

stubFunction · 0.90
spyFunction · 0.90
assertSpyCallsFunction · 0.90
assertSpyCallFunction · 0.90
assertEqualsFunction · 0.90
assertStrictEqualsFunction · 0.90
hookFnsFunction · 0.85
keysMethod · 0.80
resetMethod · 0.45

Tested by

no test coverage detected