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

docs(testing): add module docs #5147

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
27 changes: 27 additions & 0 deletions testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,30 @@ This package provides utilities for testing.
- [Faking time and timers](https://jsr.io/@std/testing/doc/time/~)
- [Snapshot testing](https://jsr.io/@std/testing/doc/snapshot/~)
- [Type assertions](https://jsr.io/@std/testing/doc/types/~)

```ts
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);
});
```
18 changes: 18 additions & 0 deletions testing/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// Copyright @dsherret and dsherret/conditional-type-checks contributors. All rights reserved. MIT license.

/**
* Testing utilities for types.
*
* ```ts ignore
* import { assertType, IsExact, IsNullable } from "@std/testing/types";
*
* const result = "some result" as string | number;
*
* // compile error if the type of `result` is not exactly `string | number`
* assertType<IsExact<typeof result, string | number>>(true);
*
* // causes a compile error that `true` is not assignable to `false`
* assertType<IsNullable<string>>(true); // error: string is not nullable
* ```
*
* @module
*/

/**
* Asserts at compile time that the provided type argument's type resolves to the expected boolean literal type.
*
Expand Down