@vitest-agent/mcp 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/bin/vitest-agent-mcp.js +1 -17
- package/index.d.ts +324 -315
- package/index.js +2 -4
- package/middleware/idempotency.js +1 -1
- package/package.json +2 -2
- package/server.js +2 -4
- package/tools/acceptance-metrics.js +1 -1
- package/tools/cache-health.js +1 -1
- package/tools/commit-changes.js +1 -1
- package/tools/configure.js +1 -1
- package/tools/coverage.js +1 -1
- package/tools/errors.js +1 -1
- package/tools/failure-signature-get.js +1 -1
- package/tools/file-coverage.js +1 -1
- package/tools/history.js +1 -1
- package/tools/inventory.js +1 -1
- package/tools/overview.js +1 -1
- package/tools/run-tests.js +15 -3
- package/tools/settings-list.js +1 -1
- package/tools/status.js +1 -1
- package/tools/tdd-artifact.js +1 -1
- package/tools/tdd-task.js +1 -1
- package/tools/test.js +1 -1
- package/tools/trends.js +1 -1
- package/tools/turn-search.js +1 -1
- package/public/patterns/_meta.json +0 -67
- package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
- package/public/patterns/known-issues-and-caveats.md +0 -52
- package/public/patterns/operating-vitest-agent-as-an-agent.md +0 -53
- package/public/patterns/running-tests-via-mcp.md +0 -58
- package/public/patterns/silencing-leaking-output-in-tests.md +0 -91
- package/public/patterns/testing-effect-schema-definitions.md +0 -71
- package/public/patterns/testing-effect-services-with-mock-layers.md +0 -63
- package/public/vendor/vitest-docs/ATTRIBUTION.md +0 -5
- package/public/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
- package/public/vendor/vitest-docs/api/advanced/metadata.md +0 -68
- package/public/vendor/vitest-docs/api/advanced/plugin.md +0 -168
- package/public/vendor/vitest-docs/api/advanced/reporters.md +0 -342
- package/public/vendor/vitest-docs/api/advanced/runner.md +0 -334
- package/public/vendor/vitest-docs/api/advanced/test-case.md +0 -302
- package/public/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
- package/public/vendor/vitest-docs/api/advanced/test-module.md +0 -140
- package/public/vendor/vitest-docs/api/advanced/test-project.md +0 -321
- package/public/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
- package/public/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
- package/public/vendor/vitest-docs/api/advanced/vitest.md +0 -684
- package/public/vendor/vitest-docs/api/assert-type.md +0 -22
- package/public/vendor/vitest-docs/api/assert.md +0 -1960
- package/public/vendor/vitest-docs/api/browser/assertions.md +0 -1277
- package/public/vendor/vitest-docs/api/browser/commands.md +0 -154
- package/public/vendor/vitest-docs/api/browser/context.md +0 -338
- package/public/vendor/vitest-docs/api/browser/interactivity.md +0 -681
- package/public/vendor/vitest-docs/api/browser/locators.md +0 -1171
- package/public/vendor/vitest-docs/api/browser/react.md +0 -346
- package/public/vendor/vitest-docs/api/browser/svelte.md +0 -292
- package/public/vendor/vitest-docs/api/browser/vue.md +0 -222
- package/public/vendor/vitest-docs/api/describe.md +0 -374
- package/public/vendor/vitest-docs/api/expect-typeof.md +0 -571
- package/public/vendor/vitest-docs/api/expect.md +0 -2304
- package/public/vendor/vitest-docs/api/hooks.md +0 -463
- package/public/vendor/vitest-docs/api/mock.md +0 -701
- package/public/vendor/vitest-docs/api/test.md +0 -926
- package/public/vendor/vitest-docs/api/vi.md +0 -1372
- package/public/vendor/vitest-docs/config/alias.md +0 -13
- package/public/vendor/vitest-docs/config/allowonly.md +0 -32
- package/public/vendor/vitest-docs/config/api.md +0 -27
- package/public/vendor/vitest-docs/config/attachmentsdir.md +0 -6
- package/public/vendor/vitest-docs/config/bail.md +0 -9
- package/public/vendor/vitest-docs/config/benchmark.md +0 -65
- package/public/vendor/vitest-docs/config/browser/api.md +0 -23
- package/public/vendor/vitest-docs/config/browser/commands.md +0 -6
- package/public/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
- package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
- package/public/vendor/vitest-docs/config/browser/enabled.md +0 -40
- package/public/vendor/vitest-docs/config/browser/expect.md +0 -250
- package/public/vendor/vitest-docs/config/browser/headless.md +0 -7
- package/public/vendor/vitest-docs/config/browser/instances.md +0 -47
- package/public/vendor/vitest-docs/config/browser/isolate.md +0 -11
- package/public/vendor/vitest-docs/config/browser/locators.md +0 -24
- package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
- package/public/vendor/vitest-docs/config/browser/playwright.md +0 -214
- package/public/vendor/vitest-docs/config/browser/preview.md +0 -32
- package/public/vendor/vitest-docs/config/browser/provider.md +0 -79
- package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
- package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
- package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
- package/public/vendor/vitest-docs/config/browser/trace.md +0 -43
- package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
- package/public/vendor/vitest-docs/config/browser/ui.md +0 -7
- package/public/vendor/vitest-docs/config/browser/viewport.md +0 -6
- package/public/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
- package/public/vendor/vitest-docs/config/cache.md +0 -26
- package/public/vendor/vitest-docs/config/chaiconfig.md +0 -29
- package/public/vendor/vitest-docs/config/clearmocks.md +0 -22
- package/public/vendor/vitest-docs/config/coverage.md +0 -455
- package/public/vendor/vitest-docs/config/css.md +0 -47
- package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
- package/public/vendor/vitest-docs/config/deps.md +0 -127
- package/public/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
- package/public/vendor/vitest-docs/config/diff.md +0 -96
- package/public/vendor/vitest-docs/config/dir.md +0 -7
- package/public/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
- package/public/vendor/vitest-docs/config/env.md +0 -5
- package/public/vendor/vitest-docs/config/environment.md +0 -96
- package/public/vendor/vitest-docs/config/environmentoptions.md +0 -30
- package/public/vendor/vitest-docs/config/exclude.md +0 -49
- package/public/vendor/vitest-docs/config/execargv.md +0 -10
- package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
- package/public/vendor/vitest-docs/config/expect.md +0 -38
- package/public/vendor/vitest-docs/config/experimental.md +0 -510
- package/public/vendor/vitest-docs/config/faketimers.md +0 -51
- package/public/vendor/vitest-docs/config/fileparallelism.md +0 -11
- package/public/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
- package/public/vendor/vitest-docs/config/globals.md +0 -42
- package/public/vendor/vitest-docs/config/globalsetup.md +0 -72
- package/public/vendor/vitest-docs/config/hideskippedtests.md +0 -7
- package/public/vendor/vitest-docs/config/hooktimeout.md +0 -7
- package/public/vendor/vitest-docs/config/include-source.md +0 -115
- package/public/vendor/vitest-docs/config/include.md +0 -71
- package/public/vendor/vitest-docs/config/includetasklocation.md +0 -17
- package/public/vendor/vitest-docs/config/index.md +0 -85
- package/public/vendor/vitest-docs/config/isolate.md +0 -13
- package/public/vendor/vitest-docs/config/logheapusage.md +0 -7
- package/public/vendor/vitest-docs/config/maxconcurrency.md +0 -9
- package/public/vendor/vitest-docs/config/maxworkers.md +0 -49
- package/public/vendor/vitest-docs/config/mockreset.md +0 -22
- package/public/vendor/vitest-docs/config/mode.md +0 -7
- package/public/vendor/vitest-docs/config/name.md +0 -111
- package/public/vendor/vitest-docs/config/onconsolelog.md +0 -25
- package/public/vendor/vitest-docs/config/onstacktrace.md +0 -32
- package/public/vendor/vitest-docs/config/onunhandlederror.md +0 -35
- package/public/vendor/vitest-docs/config/open.md +0 -7
- package/public/vendor/vitest-docs/config/outputfile.md +0 -7
- package/public/vendor/vitest-docs/config/passwithnotests.md +0 -7
- package/public/vendor/vitest-docs/config/pool.md +0 -45
- package/public/vendor/vitest-docs/config/printconsoletrace.md +0 -6
- package/public/vendor/vitest-docs/config/projects.md +0 -6
- package/public/vendor/vitest-docs/config/provide.md +0 -45
- package/public/vendor/vitest-docs/config/reporters.md +0 -69
- package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
- package/public/vendor/vitest-docs/config/restoremocks.md +0 -22
- package/public/vendor/vitest-docs/config/retry.md +0 -140
- package/public/vendor/vitest-docs/config/root.md +0 -6
- package/public/vendor/vitest-docs/config/runner.md +0 -6
- package/public/vendor/vitest-docs/config/sequence.md +0 -158
- package/public/vendor/vitest-docs/config/server.md +0 -68
- package/public/vendor/vitest-docs/config/setupfiles.md +0 -40
- package/public/vendor/vitest-docs/config/silent.md +0 -9
- package/public/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
- package/public/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
- package/public/vendor/vitest-docs/config/snapshotformat.md +0 -28
- package/public/vendor/vitest-docs/config/snapshotserializers.md +0 -6
- package/public/vendor/vitest-docs/config/stricttags.md +0 -30
- package/public/vendor/vitest-docs/config/tags.md +0 -141
- package/public/vendor/vitest-docs/config/teardowntimeout.md +0 -7
- package/public/vendor/vitest-docs/config/testnamepattern.md +0 -21
- package/public/vendor/vitest-docs/config/testtimeout.md +0 -7
- package/public/vendor/vitest-docs/config/typecheck.md +0 -77
- package/public/vendor/vitest-docs/config/ui.md +0 -15
- package/public/vendor/vitest-docs/config/unstubenvs.md +0 -20
- package/public/vendor/vitest-docs/config/unstubglobals.md +0 -20
- package/public/vendor/vitest-docs/config/update.md +0 -16
- package/public/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
- package/public/vendor/vitest-docs/config/watch.md +0 -11
- package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
- package/public/vendor/vitest-docs/guide/advanced/index.md +0 -147
- package/public/vendor/vitest-docs/guide/advanced/pool.md +0 -148
- package/public/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
- package/public/vendor/vitest-docs/guide/advanced/tests.md +0 -125
- package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
- package/public/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
- package/public/vendor/vitest-docs/guide/browser/index.md +0 -630
- package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
- package/public/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
- package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
- package/public/vendor/vitest-docs/guide/cli-generated.md +0 -972
- package/public/vendor/vitest-docs/guide/cli.md +0 -234
- package/public/vendor/vitest-docs/guide/common-errors.md +0 -163
- package/public/vendor/vitest-docs/guide/coverage.md +0 -515
- package/public/vendor/vitest-docs/guide/debugging.md +0 -127
- package/public/vendor/vitest-docs/guide/environment.md +0 -101
- package/public/vendor/vitest-docs/guide/extending-matchers.md +0 -160
- package/public/vendor/vitest-docs/guide/features.md +0 -310
- package/public/vendor/vitest-docs/guide/filtering.md +0 -175
- package/public/vendor/vitest-docs/guide/ide.md +0 -43
- package/public/vendor/vitest-docs/guide/improving-performance.md +0 -245
- package/public/vendor/vitest-docs/guide/in-source.md +0 -159
- package/public/vendor/vitest-docs/guide/index.md +0 -128
- package/public/vendor/vitest-docs/guide/learn/async.md +0 -147
- package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
- package/public/vendor/vitest-docs/guide/learn/matchers.md +0 -277
- package/public/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
- package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
- package/public/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
- package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
- package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
- package/public/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
- package/public/vendor/vitest-docs/guide/lifecycle.md +0 -379
- package/public/vendor/vitest-docs/guide/migration.md +0 -863
- package/public/vendor/vitest-docs/guide/mocking/classes.md +0 -158
- package/public/vendor/vitest-docs/guide/mocking/dates.md +0 -52
- package/public/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
- package/public/vendor/vitest-docs/guide/mocking/functions.md +0 -61
- package/public/vendor/vitest-docs/guide/mocking/globals.md +0 -20
- package/public/vendor/vitest-docs/guide/mocking/modules.md +0 -414
- package/public/vendor/vitest-docs/guide/mocking/requests.md +0 -114
- package/public/vendor/vitest-docs/guide/mocking/timers.md +0 -48
- package/public/vendor/vitest-docs/guide/mocking.md +0 -239
- package/public/vendor/vitest-docs/guide/open-telemetry.md +0 -156
- package/public/vendor/vitest-docs/guide/parallelism.md +0 -82
- package/public/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
- package/public/vendor/vitest-docs/guide/projects.md +0 -291
- package/public/vendor/vitest-docs/guide/recipes.md +0 -59
- package/public/vendor/vitest-docs/guide/reporters.md +0 -723
- package/public/vendor/vitest-docs/guide/snapshot.md +0 -620
- package/public/vendor/vitest-docs/guide/test-annotations.md +0 -103
- package/public/vendor/vitest-docs/guide/test-context.md +0 -902
- package/public/vendor/vitest-docs/guide/test-tags.md +0 -314
- package/public/vendor/vitest-docs/guide/testing-types.md +0 -149
- package/public/vendor/vitest-docs/guide/ui.md +0 -160
- package/public/vendor/vitest-docs/guide/using-plugins.md +0 -5
- package/public/vendor/vitest-docs/manifest.json +0 -1691
- package/resources/index.js +0 -166
- package/resources/indexes.js +0 -77
- package/resources/manifest-schema.js +0 -46
- package/resources/paths.js +0 -20
- package/resources/patterns.js +0 -22
- package/resources/upstream-docs.js +0 -22
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
# Mocking Classes
|
|
2
|
-
|
|
3
|
-
You can mock an entire class with a single [`vi.fn`](/api/vi#fn) call.
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
class Dog {
|
|
7
|
-
name: string
|
|
8
|
-
|
|
9
|
-
constructor(name: string) {
|
|
10
|
-
this.name = name
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
static getType(): string {
|
|
14
|
-
return 'animal'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
greet = (): string => {
|
|
18
|
-
return `Hi! My name is ${this.name}!`
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
speak(): string {
|
|
22
|
-
return 'bark!'
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
isHungry() {}
|
|
26
|
-
feed() {}
|
|
27
|
-
}
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
We can re-create this class with `vi.fn` (or `vi.spyOn().mockImplementation()`):
|
|
31
|
-
|
|
32
|
-
```ts
|
|
33
|
-
const Dog = vi.fn(class {
|
|
34
|
-
static getType = vi.fn(() => 'mocked animal')
|
|
35
|
-
|
|
36
|
-
constructor(name) {
|
|
37
|
-
this.name = name
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
greet = vi.fn(() => `Hi! My name is ${this.name}!`)
|
|
41
|
-
speak = vi.fn(() => 'loud bark!')
|
|
42
|
-
feed = vi.fn()
|
|
43
|
-
})
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
::: warning
|
|
47
|
-
If a non-primitive is returned from the constructor function, that value will become the result of the new expression. In this case the `[[Prototype]]` may not be correctly bound:
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
const CorrectDogClass = vi.fn(function (name) {
|
|
51
|
-
this.name = name
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
const IncorrectDogClass = vi.fn(name => ({
|
|
55
|
-
name
|
|
56
|
-
}))
|
|
57
|
-
|
|
58
|
-
const Marti = new CorrectDogClass('Marti')
|
|
59
|
-
const Newt = new IncorrectDogClass('Newt')
|
|
60
|
-
|
|
61
|
-
Marti instanceof CorrectDogClass // ✅ true
|
|
62
|
-
Newt instanceof IncorrectDogClass // ❌ false!
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
If you are mocking classes, prefer the class syntax over the function.
|
|
66
|
-
:::
|
|
67
|
-
|
|
68
|
-
::: tip WHEN TO USE?
|
|
69
|
-
Generally speaking, you would re-create a class like this inside the module factory if the class is re-exported from another module:
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { Dog } from './dog.js'
|
|
73
|
-
|
|
74
|
-
vi.mock(import('./dog.js'), () => {
|
|
75
|
-
const Dog = vi.fn(class {
|
|
76
|
-
feed = vi.fn()
|
|
77
|
-
// ... other mocks
|
|
78
|
-
})
|
|
79
|
-
return { Dog }
|
|
80
|
-
})
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
This method can also be used to pass an instance of a class to a function that accepts the same interface:
|
|
84
|
-
|
|
85
|
-
```ts [src/feed.ts]
|
|
86
|
-
function feed(dog: Dog) {
|
|
87
|
-
// ...
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
```ts [tests/dog.test.ts]
|
|
91
|
-
import { expect, test, vi } from 'vitest'
|
|
92
|
-
import { feed } from '../src/feed.js'
|
|
93
|
-
|
|
94
|
-
const Dog = vi.fn(class {
|
|
95
|
-
feed = vi.fn()
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
test('can feed dogs', () => {
|
|
99
|
-
const dogMax = new Dog('Max')
|
|
100
|
-
|
|
101
|
-
feed(dogMax)
|
|
102
|
-
|
|
103
|
-
expect(dogMax.feed).toHaveBeenCalled()
|
|
104
|
-
expect(dogMax.isHungry()).toBe(false)
|
|
105
|
-
})
|
|
106
|
-
```
|
|
107
|
-
:::
|
|
108
|
-
|
|
109
|
-
Now, when we create a new instance of the `Dog` class its `speak` method (alongside `feed` and `greet`) is already mocked:
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
const Cooper = new Dog('Cooper')
|
|
113
|
-
Cooper.speak() // loud bark!
|
|
114
|
-
Cooper.greet() // Hi! My name is Cooper!
|
|
115
|
-
|
|
116
|
-
// you can use built-in assertions to check the validity of the call
|
|
117
|
-
expect(Cooper.speak).toHaveBeenCalled()
|
|
118
|
-
expect(Cooper.greet).toHaveBeenCalled()
|
|
119
|
-
|
|
120
|
-
const Max = new Dog('Max')
|
|
121
|
-
|
|
122
|
-
// methods are not shared between instances if you assigned them directly
|
|
123
|
-
expect(Max.speak).not.toHaveBeenCalled()
|
|
124
|
-
expect(Max.greet).not.toHaveBeenCalled()
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
We can reassign the return value for a specific instance:
|
|
128
|
-
|
|
129
|
-
```ts
|
|
130
|
-
const dog = new Dog('Cooper')
|
|
131
|
-
|
|
132
|
-
// "vi.mocked" is a type helper, since
|
|
133
|
-
// TypeScript doesn't know that Dog is a mocked class,
|
|
134
|
-
// it wraps any function in a Mock<T> type
|
|
135
|
-
// without validating if the function is a mock
|
|
136
|
-
vi.mocked(dog.speak).mockReturnValue('woof woof')
|
|
137
|
-
|
|
138
|
-
dog.speak() // woof woof
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
To mock the property, we can use the `vi.spyOn(dog, 'name', 'get')` method. This makes it possible to use spy assertions on the mocked property:
|
|
142
|
-
|
|
143
|
-
```ts
|
|
144
|
-
const dog = new Dog('Cooper')
|
|
145
|
-
|
|
146
|
-
const nameSpy = vi.spyOn(dog, 'name', 'get').mockReturnValue('Max')
|
|
147
|
-
|
|
148
|
-
expect(dog.name).toBe('Max')
|
|
149
|
-
expect(nameSpy).toHaveBeenCalledTimes(1)
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
::: tip
|
|
153
|
-
You can also spy on getters and setters using the same method.
|
|
154
|
-
:::
|
|
155
|
-
|
|
156
|
-
::: danger
|
|
157
|
-
Using classes with `vi.fn()` was introduced in Vitest 4. Previously, you had to use `function` and `prototype` inheritance directly. See [v3 guide](https://v3.vitest.dev/guide/mocking.html#classes).
|
|
158
|
-
:::
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# Mocking Dates
|
|
2
|
-
|
|
3
|
-
Sometimes you need to be in control of the date to ensure consistency when testing. Vitest uses [`@sinonjs/fake-timers`](https://github.com/sinonjs/fake-timers) package for manipulating timers, as well as system date. You can find more about the specific API in detail [here](/api/vi#vi-setsystemtime).
|
|
4
|
-
|
|
5
|
-
## Example
|
|
6
|
-
|
|
7
|
-
```js
|
|
8
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
9
|
-
|
|
10
|
-
const businessHours = [9, 17]
|
|
11
|
-
|
|
12
|
-
function purchase() {
|
|
13
|
-
const currentHour = new Date().getHours()
|
|
14
|
-
const [open, close] = businessHours
|
|
15
|
-
|
|
16
|
-
if (currentHour > open && currentHour < close) {
|
|
17
|
-
return { message: 'Success' }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return { message: 'Error' }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
describe('purchasing flow', () => {
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
// tell vitest we use mocked time
|
|
26
|
-
vi.useFakeTimers()
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
afterEach(() => {
|
|
30
|
-
// restoring date after each test run
|
|
31
|
-
vi.useRealTimers()
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
it('allows purchases within business hours', () => {
|
|
35
|
-
// set hour within business hours
|
|
36
|
-
const date = new Date(2000, 1, 1, 13)
|
|
37
|
-
vi.setSystemTime(date)
|
|
38
|
-
|
|
39
|
-
// access Date.now() will result in the date set above
|
|
40
|
-
expect(purchase()).toEqual({ message: 'Success' })
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('disallows purchases outside of business hours', () => {
|
|
44
|
-
// set hour outside business hours
|
|
45
|
-
const date = new Date(2000, 1, 1, 19)
|
|
46
|
-
vi.setSystemTime(date)
|
|
47
|
-
|
|
48
|
-
// access Date.now() will result in the date set above
|
|
49
|
-
expect(purchase()).toEqual({ message: 'Error' })
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
```
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# Mocking the File System
|
|
2
|
-
|
|
3
|
-
Mocking the file system ensures that the tests do not depend on the actual file system, making the tests more reliable and predictable. This isolation helps in avoiding side effects from previous tests. It allows for testing error conditions and edge cases that might be difficult or impossible to replicate with an actual file system, such as permission issues, disk full scenarios, or read/write errors.
|
|
4
|
-
|
|
5
|
-
Vitest doesn't provide any file system mocking API out of the box. You can use `vi.mock` to mock the `fs` module manually, but it's hard to maintain. Instead, we recommend using [`memfs`](https://npmx.dev/package/memfs) to do that for you. `memfs` creates an in-memory file system, which simulates file system operations without touching the actual disk. This approach is fast and safe, avoiding any potential side effects on the real file system.
|
|
6
|
-
|
|
7
|
-
## Example
|
|
8
|
-
|
|
9
|
-
To automatically redirect every `fs` call to `memfs`, you can create `__mocks__/fs.cjs` and `__mocks__/fs/promises.cjs` files at the root of your project:
|
|
10
|
-
|
|
11
|
-
::: code-group
|
|
12
|
-
```ts [__mocks__/fs.cjs]
|
|
13
|
-
// we can also use `import`, but then
|
|
14
|
-
// every export should be explicitly defined
|
|
15
|
-
|
|
16
|
-
const { fs } = require('memfs')
|
|
17
|
-
module.exports = fs
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
```ts [__mocks__/fs/promises.cjs]
|
|
21
|
-
// we can also use `import`, but then
|
|
22
|
-
// every export should be explicitly defined
|
|
23
|
-
|
|
24
|
-
const { fs } = require('memfs')
|
|
25
|
-
module.exports = fs.promises
|
|
26
|
-
```
|
|
27
|
-
:::
|
|
28
|
-
|
|
29
|
-
```ts [read-hello-world.js]
|
|
30
|
-
import { readFileSync } from 'node:fs'
|
|
31
|
-
|
|
32
|
-
export function readHelloWorld(path) {
|
|
33
|
-
return readFileSync(path, 'utf-8')
|
|
34
|
-
}
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
```ts [hello-world.test.js]
|
|
38
|
-
import { beforeEach, expect, it, vi } from 'vitest'
|
|
39
|
-
import { fs, vol } from 'memfs'
|
|
40
|
-
import { readHelloWorld } from './read-hello-world.js'
|
|
41
|
-
|
|
42
|
-
// tell vitest to use fs mock from __mocks__ folder
|
|
43
|
-
// this can be done in a setup file if fs should always be mocked
|
|
44
|
-
vi.mock('node:fs')
|
|
45
|
-
vi.mock('node:fs/promises')
|
|
46
|
-
|
|
47
|
-
beforeEach(() => {
|
|
48
|
-
// reset the state of in-memory fs
|
|
49
|
-
vol.reset()
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
it('should return correct text', () => {
|
|
53
|
-
const path = '/hello-world.txt'
|
|
54
|
-
fs.writeFileSync(path, 'hello world')
|
|
55
|
-
|
|
56
|
-
const text = readHelloWorld(path)
|
|
57
|
-
expect(text).toBe('hello world')
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
it('can return a value multiple times', () => {
|
|
61
|
-
// you can use vol.fromJSON to define several files
|
|
62
|
-
vol.fromJSON(
|
|
63
|
-
{
|
|
64
|
-
'./dir1/hw.txt': 'hello dir1',
|
|
65
|
-
'./dir2/hw.txt': 'hello dir2',
|
|
66
|
-
},
|
|
67
|
-
// default cwd
|
|
68
|
-
'/tmp',
|
|
69
|
-
)
|
|
70
|
-
|
|
71
|
-
expect(readHelloWorld('/tmp/dir1/hw.txt')).toBe('hello dir1')
|
|
72
|
-
expect(readHelloWorld('/tmp/dir2/hw.txt')).toBe('hello dir2')
|
|
73
|
-
})
|
|
74
|
-
```
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
# Mocking Functions
|
|
2
|
-
|
|
3
|
-
Mocking functions can be split up into two different categories: spying and mocking.
|
|
4
|
-
|
|
5
|
-
If you need to observe the behaviour of a method on an object, you can use [`vi.spyOn()`](/api/vi#vi-spyon) to create a spy that tracks calls to that method.
|
|
6
|
-
|
|
7
|
-
If you need to pass down a custom function implementation as an argument or create a new mocked entity, you can use [`vi.fn()`](/api/vi#vi-fn) to create a mock function.
|
|
8
|
-
|
|
9
|
-
Both `vi.spyOn` and `vi.fn` share the same methods.
|
|
10
|
-
|
|
11
|
-
## Example
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
15
|
-
|
|
16
|
-
const messages = {
|
|
17
|
-
items: [
|
|
18
|
-
{ message: 'Simple test message', from: 'Testman' },
|
|
19
|
-
// ...
|
|
20
|
-
],
|
|
21
|
-
addItem(item) {
|
|
22
|
-
messages.items.push(item)
|
|
23
|
-
messages.callbacks.forEach(callback => callback(item))
|
|
24
|
-
},
|
|
25
|
-
onItem(callback) {
|
|
26
|
-
messages.callbacks.push(callback)
|
|
27
|
-
},
|
|
28
|
-
getLatest, // can also be a `getter or setter if supported`
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function getLatest(index = messages.items.length - 1) {
|
|
32
|
-
return messages.items[index]
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
it('should get the latest message with a spy', () => {
|
|
36
|
-
const spy = vi.spyOn(messages, 'getLatest')
|
|
37
|
-
expect(spy.getMockName()).toEqual('getLatest')
|
|
38
|
-
|
|
39
|
-
expect(messages.getLatest()).toEqual(
|
|
40
|
-
messages.items[messages.items.length - 1],
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
expect(spy).toHaveBeenCalledTimes(1)
|
|
44
|
-
|
|
45
|
-
spy.mockImplementationOnce(() => 'access-restricted')
|
|
46
|
-
expect(messages.getLatest()).toEqual('access-restricted')
|
|
47
|
-
|
|
48
|
-
expect(spy).toHaveBeenCalledTimes(2)
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('passing down the mock', () => {
|
|
52
|
-
const callback = vi.fn()
|
|
53
|
-
messages.onItem(callback)
|
|
54
|
-
|
|
55
|
-
messages.addItem({ message: 'Another test message', from: 'Testman' })
|
|
56
|
-
expect(callback).toHaveBeenCalledWith({
|
|
57
|
-
message: 'Another test message',
|
|
58
|
-
from: 'Testman',
|
|
59
|
-
})
|
|
60
|
-
})
|
|
61
|
-
```
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# Mocking Globals
|
|
2
|
-
|
|
3
|
-
You can mock global variables that are not present with `jsdom` or `node` by using [`vi.stubGlobal`](/api/vi#vi-stubglobal) helper. It will put the value of the global variable into a `globalThis` object.
|
|
4
|
-
|
|
5
|
-
By default, Vitest does not reset these globals, but you can turn on the [`unstubGlobals`](/config/unstubglobals) option in your config to restore the original values after each test or call [`vi.unstubAllGlobals()`](/api/vi#vi-unstuballglobals) manually.
|
|
6
|
-
|
|
7
|
-
```ts
|
|
8
|
-
import { vi } from 'vitest'
|
|
9
|
-
|
|
10
|
-
const IntersectionObserverMock = vi.fn(class {
|
|
11
|
-
disconnect = vi.fn()
|
|
12
|
-
observe = vi.fn()
|
|
13
|
-
takeRecords = vi.fn()
|
|
14
|
-
unobserve = vi.fn()
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
vi.stubGlobal('IntersectionObserver', IntersectionObserverMock)
|
|
18
|
-
|
|
19
|
-
// now you can access it as `IntersectionObserver` or `window.IntersectionObserver`
|
|
20
|
-
```
|