std/testing
Milly 55297e90a8
fix(testing): FakeTime fakes AbortSignal.timeout (#5500)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-07-22 13:07:48 +09:00
..
__snapshots__ test(testing): fix flakiness of snapshot test (#5414) 2024-07-11 19:25:08 +09:00
_test_suite.ts fix(testing): throw error when async func is passed to describe (#5385) 2024-07-11 15:28:27 +09:00
_test_utils.ts refactor(testing): remove use of public keyword (#5051) 2024-06-17 15:11:31 +12:00
_time.ts fix(testing): FakeTime fakes AbortSignal.timeout (#5500) 2024-07-22 13:07:48 +09:00
bdd_test.ts fix(testing): throw error when async func is passed to describe (#5385) 2024-07-11 15:28:27 +09:00
bdd.ts fix(testing): cause type error for async function as describe body (#5355) 2024-07-09 12:13:07 +09:00
deno.json chore: release 2024.07.12 (#5431) 2024-07-12 19:45:19 +09:00
mock_test.ts fix(testing): correctly throw in constructor with spy() (#5139) 2024-06-26 16:32:08 +09:00
mock.ts refactor(expect,testing): update @std/assert imports (#5242) 2024-07-02 14:07:46 +10:00
README.md docs(testing): add module docs (#5147) 2024-06-26 16:26:28 +10:00
snapshot_test.ts test(testing): fix flakiness of snapshot test (#5414) 2024-07-11 19:25:08 +09:00
snapshot.ts fix(testing): escape CR in snapshot files (#5352) 2024-07-08 19:33:33 +09:00
time_test.ts fix(testing): FakeTime fakes AbortSignal.timeout (#5500) 2024-07-22 13:07:48 +09:00
time.ts fix(testing): FakeTime fakes AbortSignal.timeout (#5500) 2024-07-22 13:07:48 +09:00
types_test.ts refactor: make the code work under verbatimModuleSyntax (#4406) 2024-02-27 21:57:25 +00:00
types.ts docs(testing): add module docs (#5147) 2024-06-26 16:26:28 +10:00

This package provides utilities for testing.

import { assertSpyCalls, spy } from "@std/testing/mock";
import { FakeTime } from "@std/testing/time";

function secondInterval(cb: () => void): number {
  return setInterval(cb, 1000);
}

Deno.test("secondInterval calls callback every second and stops after being cleared", () => {
  using time = new FakeTime();

  const cb = spy();
  const intervalId = secondInterval(cb);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 0);
  time.tick(500);
  assertSpyCalls(cb, 1);
  time.tick(3500);
  assertSpyCalls(cb, 4);

  clearInterval(intervalId);
  time.tick(1000);
  assertSpyCalls(cb, 4);
});