Skip to content

Commit 1e857d2

Browse files
authored
docs: fix fakeTimers migration example and remove // (#7458)
1 parent f5a0d91 commit 1e857d2

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

docs/.vitepress/config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ export default ({ mode }: { mode: string }) => {
3131
lang: 'en-US',
3232
title: vitestName,
3333
description: vitestDescription,
34+
srcExclude: [
35+
'**/guide/examples/*',
36+
'**/guide/cli-generated.md',
37+
],
3438
locales: {
3539
root: {
3640
label: 'English',

docs/guide/examples/promise-done.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```js
2+
it('should work', (done) => { // [!code --]
3+
it('should work', () => new Promise(done => { // [!code ++]
4+
// ...
5+
done()
6+
}) // [!code --]
7+
})) // [!code ++]
8+
```

docs/guide/migration.md

+18-17
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ foo.bar() // 'Hello, mock!'
6969

7070
foo.bar.mockReset()
7171

72-
foo.bar() // undefined // [!code --]
73-
foo.bar() // 'Hello, world!' // [!code ++]
72+
foo.bar() // undefined [!code --]
73+
foo.bar() // 'Hello, world!' [!code ++]
7474
```
7575

7676
### `vi.spyOn` Reuses Mock if Method is Already Mocked
@@ -81,8 +81,8 @@ Previously, Vitest would always assign a new spy when spying on an object. This
8181
vi.spyOn(fooService, 'foo').mockImplementation(() => 'bar')
8282
vi.spyOn(fooService, 'foo').mockImplementation(() => 'bar')
8383
vi.restoreAllMocks()
84-
vi.isMockFunction(fooService.foo) // true // [!code --]
85-
vi.isMockFunction(fooService.foo) // false // [!code ++]
84+
vi.isMockFunction(fooService.foo) // true [!code --]
85+
vi.isMockFunction(fooService.foo) // false [!code ++]
8686
```
8787

8888
### Fake Timers Defaults
@@ -92,8 +92,8 @@ Vitest no longer provides default `fakeTimers.toFake` options. Now, Vitest will
9292
```ts
9393
vi.useFakeTimers()
9494

95-
performance.now() // original // [!code --]
96-
performance.now() // fake // [!code ++]
95+
performance.now() // original [!code --]
96+
performance.now() // fake [!code ++]
9797
```
9898

9999
You can revert to the previous behaviour by specifying timers when calling `vi.useFakeTimers` or globally in the config:
@@ -102,7 +102,15 @@ You can revert to the previous behaviour by specifying timers when calling `vi.u
102102
export default defineConfig({
103103
test: {
104104
fakeTimers: {
105-
toFake: ['setTimeout', 'clearTimeout', 'Date'], // [!code ++]
105+
toFake: [ // [!code ++]
106+
'setTimeout', // [!code ++]
107+
'clearTimeout', // [!code ++]
108+
'setInterval', // [!code ++]
109+
'clearInterval', // [!code ++]
110+
'setImmediate', // [!code ++]
111+
'clearImmediate', // [!code ++]
112+
'Date', // [!code ++]
113+
] // [!code ++]
106114
},
107115
},
108116
})
@@ -278,8 +286,8 @@ Previously Vitest resolved `mock.results` values if the function returned a Prom
278286
const fn = vi.fn().mockResolvedValueOnce('result')
279287
await fn()
280288

281-
const result = fn.mock.results[0] // 'result' // [!code --]
282-
const result = fn.mock.results[0] // 'Promise<result>' // [!code ++]
289+
const result = fn.mock.results[0] // 'result' [!code --]
290+
const result = fn.mock.results[0] // 'Promise<result>' [!code ++]
283291

284292
const settledResult = fn.mock.settledResults[0] // 'result'
285293
```
@@ -530,14 +538,7 @@ If you want to modify the object, you will use [replaceProperty API](https://jes
530538

531539
From Vitest v0.10.0, the callback style of declaring tests is deprecated. You can rewrite them to use `async`/`await` functions, or use Promise to mimic the callback style.
532540

533-
```
534-
it('should work', (done) => { // [!code --]
535-
it('should work', () => new Promise(done => { // [!code ++]
536-
// ...
537-
done()
538-
}) // [!code --]
539-
})) // [!code ++]
540-
```
541+
<!--@include: ./examples/promise-done.md-->
541542

542543
### Hooks
543544

eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default antfu(
3030
'docs/guide/snapshot.md',
3131
// uses invalid js example
3232
'docs/advanced/api/import-example.md',
33+
'docs/guide/examples/*.md',
3334
],
3435
},
3536
{

0 commit comments

Comments
 (0)