|
1 | | ---- |
2 | | -order: 3 |
3 | | ---- |
4 | 1 | # Unit Testing |
5 | 2 |
|
6 | 3 | The testing library used by this repository is [`vitest`](https://vitest.dev). |
7 | 4 |
|
8 | 5 | > [!IMPORTANT] |
9 | | -> Other Source Academy repositories use `jest` as their testing package. Although `vitest` has been designed as a drop in replacement for `jest`, |
| 6 | +> Other Source Academy repositories may use `jest` as their testing package. Although `vitest` has been designed as a drop in replacement for `jest`, |
10 | 7 | > there are subtle differences between the two. For example, `vi.spyOn` doesn't replace the implementation within the module while `jest.spyOn` does (See [here](https://vitest.dev/guide/mocking.html#mocking-pitfalls)). |
11 | 8 | > |
12 | 9 | > Refer to [this page](https://vitest.dev/guide/migration.html#jest) for more differences between `jest` and `vitest`. |
13 | 10 |
|
14 | | -## Adding Tests |
| 11 | +## Adding/Writing Tests |
15 | 12 |
|
16 | 13 | By default, any Typescript (`.ts`) files located within a `__tests__` folder are considered test files. This means that if `vitest` does not |
17 | 14 | detect any tests within that file, it will throw an error. This also includes any subdirectories under a `__tests__` folder. |
@@ -45,7 +42,27 @@ test('CurveTab', () => { |
45 | 42 | }); |
46 | 43 | ``` |
47 | 44 |
|
48 | | -For more instructions on how to write tests you can refer to the [Vitest website](https://vitest.dev/guide/#writing-tests). |
| 45 | +For comprehensive instructions on how to write tests you can refer to the [Vitest website](https://vitest.dev/guide/#writing-tests). |
| 46 | + |
| 47 | +### Describe Block Titles for functions |
| 48 | + |
| 49 | +Vitest supports passing functions directly to `describe` blocks. You should use this syntax instead of using a string title where possible: |
| 50 | +```ts |
| 51 | +function foo() {} |
| 52 | + |
| 53 | +// Don't do this |
| 54 | +describe('foo', () => {}); |
| 55 | + |
| 56 | +// Do this instead! |
| 57 | +describe(foo, () => {}); |
| 58 | +``` |
| 59 | + |
| 60 | +This is so that if you ever rename the function, the title of the describe block will change during your refactoring and there won't be a mismatch. |
| 61 | + |
| 62 | +Of course, if you're not specifically testing a function, you can still use the normal string description: |
| 63 | +```ts |
| 64 | +describe('A bunch of tests that need to execute', () => {}); |
| 65 | +``` |
49 | 66 |
|
50 | 67 | ### Test Filtering |
51 | 68 |
|
|
0 commit comments