Skip to content

Commit

Permalink
docs: elaboration for resetModules (#3059)
Browse files Browse the repository at this point in the history
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
  • Loading branch information
jonathan-graf and sheremet-va committed Mar 27, 2023
1 parent 9dc6929 commit 09fec84
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions docs/api/vi.md
Expand Up @@ -291,23 +291,24 @@ test('importing the next module imports mocked one', async () => {

- **Type**: `() => Vitest`

Resets modules registry by clearing cache of all modules. Might be useful to isolate modules where local state conflicts between tests.
Resets modules registry by clearing cache of all modules. This allows modules to be reevaluated when reimported. Top-level imports cannot be reevaluated. Might be useful to isolate modules where local state conflicts between tests.

```ts
import { vi } from 'vitest'
import { data } from './data.js' // Will not get reevaluated beforeEach test
beforeAll(() => {
beforeEach(() => {
vi.resetModules()
})

test('change state', async () => {
const mod = await import('./some/path')
const mod = await import('./some/path.js') // Will get reevaluated
mod.changeLocalState('new value')
expect(mod.getLocalState()).toBe('new value')
})

test('module has old state', async () => {
const mod = await import('./some/path')
const mod = await import('./some/path.js') // Will get reevaluated
expect(mod.getLocalState()).toBe('old value')
})
```
Expand Down

0 comments on commit 09fec84

Please sign in to comment.