Skip to content

Commit 08c79d6

Browse files
docs: Improve formatting and readability of the Mocking / Cheat Sheet section (#5632)
1 parent 200609c commit 08c79d6

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

docs/guide/mocking.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,14 @@ describe('delayed execution', () => {
474474

475475
I want to…
476476

477-
- Spy on a `method`
477+
### Spy on a `method`
478478

479479
```ts
480480
const instance = new SomeClass()
481481
vi.spyOn(instance, 'method')
482482
```
483483

484-
- Mock exported variables
484+
### Mock exported variables
485485
```js
486486
// some-path.js
487487
export const getter = 'variable'
@@ -493,9 +493,14 @@ import * as exports from './some-path.js'
493493
vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked')
494494
```
495495

496-
- Mock exported function
496+
### Mock an exported function
497+
498+
1. Example with `vi.mock`:
499+
500+
::: warning
501+
Don't forget that a `vi.mock` call is hoisted to top of the file. It will always be executed before all imports.
502+
:::
497503

498-
Example with `vi.mock`:
499504
```ts
500505
// ./some-path.js
501506
export function method() {}
@@ -508,20 +513,16 @@ vi.mock('./some-path.js', () => ({
508513
}))
509514
```
510515

511-
::: warning
512-
Don't forget that `vi.mock` call is hoisted to top of the file. **Do not** put `vi.mock` calls inside `beforeEach`, only one of these will actually mock a module.
513-
:::
514-
515-
Example with `vi.spyOn`:
516+
2. Example with `vi.spyOn`:
516517
```ts
517518
import * as exports from './some-path.js'
518519

519520
vi.spyOn(exports, 'method').mockImplementation(() => {})
520521
```
521522

522-
- Mock exported class implementation
523+
### Mock an exported class implementation
523524

524-
Example with `vi.mock` and prototype:
525+
1. Example with `vi.mock` and `.prototype`:
525526
```ts
526527
// some-path.ts
527528
export class SomeClass {}
@@ -537,7 +538,7 @@ vi.mock('./some-path.js', () => {
537538
// SomeClass.mock.instances will have SomeClass
538539
```
539540

540-
Example with `vi.mock` and return value:
541+
2. Example with `vi.mock` and a return value:
541542
```ts
542543
import { SomeClass } from './some-path.js'
543544

@@ -550,7 +551,7 @@ vi.mock('./some-path.js', () => {
550551
// SomeClass.mock.returns will have returned object
551552
```
552553

553-
Example with `vi.spyOn`:
554+
3. Example with `vi.spyOn`:
554555

555556
```ts
556557
import * as exports from './some-path.js'
@@ -560,9 +561,9 @@ vi.spyOn(exports, 'SomeClass').mockImplementation(() => {
560561
})
561562
```
562563

563-
- Spy on an object returned from a function
564+
### Spy on an object returned from a function
564565

565-
Example using cache:
566+
1. Example using cache:
566567

567568
```ts
568569
// some-path.ts
@@ -603,7 +604,7 @@ const obj = useObject()
603604
expect(obj.method).toHaveBeenCalled()
604605
```
605606

606-
- Mock part of a module
607+
### Mock part of a module
607608

608609
```ts
609610
import { mocked, original } from './some-path.js'
@@ -619,7 +620,7 @@ original() // has original behaviour
619620
mocked() // is a spy function
620621
```
621622

622-
- Mock current date
623+
### Mock the current date
623624

624625
To mock `Date`'s time, you can use `vi.setSystemTime` helper function. This value will **not** automatically reset between different tests.
625626

@@ -634,7 +635,7 @@ expect(now.valueOf()).toBe(mockDate.valueOf())
634635
vi.useRealTimers()
635636
```
636637

637-
- Mock global variable
638+
### Mock a global variable
638639

639640
You can set global variable by assigning a value to `globalThis` or using [`vi.stubGlobal`](/api/vi#vi-stubglobal) helper. When using `vi.stubGlobal`, it will **not** automatically reset between different tests, unless you enable [`unstubGlobals`](/config/#unstubglobals) config option or call [`vi.unstubAllGlobals`](/api/vi#vi-unstuballglobals).
640641

@@ -643,9 +644,13 @@ vi.stubGlobal('__VERSION__', '1.0.0')
643644
expect(__VERSION__).toBe('1.0.0')
644645
```
645646

646-
- Mock `import.meta.env`
647+
### Mock `import.meta.env`
648+
649+
1. To change environmental variable, you can just assign a new value to it.
647650

648-
To change environmental variable, you can just assign a new value to it. This value will **not** automatically reset between different tests.
651+
::: warning
652+
The environmental variable value will **_not_** automatically reset between different tests.
653+
:::
649654

650655
```ts
651656
import { beforeEach, expect, it } from 'vitest'
@@ -663,7 +668,7 @@ it('changes value', () => {
663668
})
664669
```
665670

666-
If you want to automatically reset value, you can use `vi.stubEnv` helper with [`unstubEnvs`](/config/#unstubenvs) config option enabled (or call [`vi.unstubAllEnvs`](/api/vi#vi-unstuballenvs) manually in `beforeEach` hook):
671+
2. If you want to automatically reset the value(s), you can use the `vi.stubEnv` helper with the [`unstubEnvs`](/config/#unstubenvs) config option enabled (or call [`vi.unstubAllEnvs`](/api/vi#vi-unstuballenvs) manually in a `beforeEach` hook):
667672

668673
```ts
669674
import { expect, it, vi } from 'vitest'

0 commit comments

Comments
 (0)