Skip to content

Commit 1220a85

Browse files
tukibseanmakesgames
authored andcommitted
adds instructions for how to create tests
1 parent 2abbd46 commit 1220a85

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
@@ -12,7 +12,41 @@ Article PRs should be reviewed using the [PR review process here](https://github
1212

1313
### Creating tests
1414

15-
Test files must be within `/src`, with a file name ending in `.test.<ext>`, where `<ext>` is one of `js` `mjs` `jsx` `ts` `tsx`.
15+
1. For some module `foo.js`, create a file `foo.test.js` in the same directory.
16+
17+
- `.ts` / `.mjs` / `.js` are all valid extensions.
18+
- `.tsx` / `.jsx` are permitted, though unless the XML syntax is necessary, use `.ts` / `.mjs` instead.
19+
- Tests must be within `/src`.
20+
21+
2. Import the module to be tested.
22+
23+
- For TypeScript, Jest utilities are not in global scope, so you will need to import these from `@jest/globals`.
24+
25+
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.
26+
27+
```ts
28+
/** @file criticalMathModule.test.ts */
29+
import { describe, expect, it } from "@jest/globals"; // only needed for TypeScript
30+
import { isEven, log2 } from "./criticalMathModule";
31+
32+
describe("isEven", () => {
33+
it("checks if numbers are even", () => {
34+
expect(isEven(1)).toBe(false);
35+
expect(isEven(4)).toBe(true);
36+
});
37+
});
38+
39+
describe("log2", () => {
40+
it("returns the base-2 logarithm of a number", () => {
41+
expect(log2(1024)).toBeCloseTo(10);
42+
});
43+
44+
it("throws an error for non-positive numbers", () => {
45+
expect(log2(0)).toThrow();
46+
expect(log2(-1)).toThrow();
47+
});
48+
});
49+
```
1650

1751
## How to review a code change
1852

0 commit comments

Comments
 (0)