Skip to content

Commit 11d630d

Browse files
committed
adds instructions for how to create tests
1 parent 8fccd53 commit 11d630d

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

CONTRIBUTING.md

+35-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,40 @@ This document builds on the [default contributing.md](https://github.com/comcode
1010

1111
### Creating tests
1212

13-
Test files must be within `/src`, with a file name ending in `.test.<ext>`, where `<ext>` is one of `js` `mjs` `jsx` `ts` `tsx`.
13+
1. For some module `foo.js`, create a file `foo.test.js` in the same directory.
14+
15+
- `.ts` / `.mjs` / `.js` are all valid extensions.
16+
- `.tsx` / `.jsx` are permitted, though unless the XML syntax is necessary, use `.ts` / `.mjs` instead.
17+
- Tests must be within `/src`.
18+
19+
2. Import the module to be tested.
20+
21+
- For TypeScript, Jest utilities are not in global scope, so you will need to import these from `@jest/globals`.
22+
23+
3. Use Jest's [`expect`](https://jestjs.io/docs/expect), [`it`](https://jestjs.io/docs/api#testname-fn-timeout) and [`describe`](https://jestjs.io/docs/api#describename-fn) to organise and assert the behaviour of the module. A test name should start with a verb, and descriptions should be used to indicate what component is being tested.
24+
25+
```ts
26+
/** @file criticalMathModule.test.ts */
27+
import { describe, expect, it } from "@jest/globals"; // only needed for TypeScript
28+
import { isEven, log2 } from "./criticalMathModule";
29+
30+
describe("isEven", () => {
31+
it("checks if numbers are even", () => {
32+
expect(isEven(1)).toBe(false);
33+
expect(isEven(4)).toBe(true);
34+
});
35+
});
36+
37+
describe("log2", () => {
38+
it("returns the base-2 logarithm of a number", () => {
39+
expect(log2(1024)).toBeCloseTo(10);
40+
});
41+
42+
it("throws an error for non-positive numbers", () => {
43+
expect(log2(0)).toThrow();
44+
expect(log2(-1)).toThrow();
45+
});
46+
});
47+
```
1448

1549
## How to review a code change

0 commit comments

Comments
 (0)