Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: updates for vi.mock in the API section #1021

Merged
merged 4 commits into from
Mar 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update docs api mocks section
  • Loading branch information
calebwaldner committed Mar 24, 2022
commit 9ad465cccb6fb7f41c1cbfacd4eadf2190c166e3
14 changes: 13 additions & 1 deletion docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,19 @@ 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 `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, therefor `jest` documentation may differ as `jest` uses commonJS modules. *Example:*
calebwaldner marked this conversation as resolved.
Show resolved Hide resolved

```ts
vi.mock("path", () => {
return {
default: ()=({})
namedExport: ()=({})
calebwaldner marked this conversation as resolved.
Show resolved Hide resolved
// 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