std/testing
Asher Gomez 040e666033
docs(testing): add module docs (#5147)
* docs(testing): add module docs

* work
2024-06-26 16:26:28 +10:00
..
__snapshots__ test(testing): correct test names (#4585) 2024-04-15 16:39:17 +10:00
_test_suite.ts docs(testing): improve the docs of @std/testing (#5033) 2024-06-17 11:31:31 +09:00
_test_utils.ts refactor(testing): remove use of public keyword (#5051) 2024-06-17 15:11:31 +12:00
_time.ts chore: update copyright year (#4046) 2024-01-02 08:11:32 +11:00
bdd_test.ts test(testing): improve bdd testing (#5136) 2024-06-25 19:26:12 +09:00
bdd.ts refactor(testing): remove use of public keyword (#5051) 2024-06-17 15:11:31 +12:00
deno.json BREAKING(testing): remove deprecated asserts module (#5099) 2024-06-25 16:18:30 +10:00
mock_test.ts refactor(testing): remove dead code and improve test of testing/mock.ts (#5137) 2024-06-25 18:32:50 +09:00
mock.ts refactor(testing): remove dead code and improve test of testing/mock.ts (#5137) 2024-06-25 18:32:50 +09:00
README.md docs(testing): add module docs (#5147) 2024-06-26 16:26:28 +10:00
snapshot_test.ts chore: switch to JSR-oriented codebase (#4650) 2024-04-29 11:57:30 +09:00
snapshot.ts refactor(testing): remove @std/fmt/colors dependency from snapshot module (#5145) 2024-06-26 15:23:17 +10:00
time_test.ts test(testing): improve FakeTime testing (#5123) 2024-06-25 15:51:24 +09:00
time.ts BREAKING(testing): disable multiple FakeTime creations (#5130) 2024-06-25 14:15:25 +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);
});