Skip to content

Commit 75057b2

Browse files
committed
Add doc info on describe function titles
1 parent 18b9db6 commit 75057b2

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

docs/src/modules/4-testing/4-unit.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
---
2-
order: 3
3-
---
41
# Unit Testing
52

63
The testing library used by this repository is [`vitest`](https://vitest.dev).
74

85
> [!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`,
107
> 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)).
118
>
129
> Refer to [this page](https://vitest.dev/guide/migration.html#jest) for more differences between `jest` and `vitest`.
1310
14-
## Adding Tests
11+
## Adding/Writing Tests
1512

1613
By default, any Typescript (`.ts`) files located within a `__tests__` folder are considered test files. This means that if `vitest` does not
1714
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', () => {
4542
});
4643
```
4744

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+
```
4966

5067
### Test Filtering
5168

0 commit comments

Comments
 (0)