Skip to content

Commit

Permalink
docs: updates for vi.mock in the API section (#1021)
Browse files Browse the repository at this point in the history
* Update docs api mocks section

* Removed misplaced space

* Update docs/api/index.md

Co-authored-by: Vladimir <[email protected]>

* Update docs/api/index.md

Co-authored-by: Anjorin Damilare <[email protected]>

Co-authored-by: Vladimir <[email protected]>
Co-authored-by: Anjorin Damilare <[email protected]>
  • Loading branch information
3 people committed Mar 25, 2022
1 parent f52cd93 commit ec40047
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,18 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo
Makes all `imports` to passed module to be mocked. Inside a path you _can_ use configured Vite aliases.

- If `factory` is defined, will return its result. Factory function can be asynchronous. You may call [`vi.importActual`](#vi-importactual) inside to get the original module. The call to `vi.mock` is hoisted to the top of the file, so you don't have access to variables declared in the global file scope!
- If mocking a module with a default export, you'll need to provide a `default` key within the returned factory function object. This is an ES modules specific caveat, therefore `jest` documentation may differ as `jest` uses commonJS modules. *Example:*

```ts
vi.mock("path", () => {
return {
default: { myDefaultKey: vi.fn() }
namedExport: vi.fn()
// etc...
}
})
```

- If `__mocks__` folder with file of the same name exist, all imports will return its exports. For example, `vi.mock('axios')` with `<root>/__mocks__/axios.ts` folder will return everything exported from `axios.ts`.
- If there is no `__mocks__` folder or a file with the same name inside, will call original module and mock it. (For the rules applied, see [algorithm](/guide/mocking#automocking-algorithm).)

Expand Down

0 comments on commit ec40047

Please sign in to comment.