Skip to content

Commit

Permalink
test: add test for importing itself inside __mocks__ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 5, 2022
1 parent 58c188a commit 5f8b525
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/mocks/package.json
Expand Up @@ -19,7 +19,8 @@
"devDependencies": {
"@vitest/ui": "latest",
"vite": "^2.9.5",
"vitest": "latest"
"vitest": "latest",
"zustand": "4.0.0-rc.1"
},
"stackblitz": {
"startCommand": "npm run test:ui"
Expand Down
14 changes: 14 additions & 0 deletions examples/mocks/src/zustand.ts
@@ -0,0 +1,14 @@
import actualCreate from 'zustand'

// a variable to hold reset functions for all stores declared in the app
const storeResetFns = new Set()

// when creating a store, we get its initial state, create a reset function and add it in the set
const create = vi.fn((createState) => {
const store = actualCreate(createState)
const initialState = store.getState()
storeResetFns.add(() => store.setState(initialState, true))
return store
})

export default create
9 changes: 9 additions & 0 deletions examples/mocks/test/self-importing.test.ts
@@ -0,0 +1,9 @@
import zustand from 'zustand'

vi.mock('zustand')

describe('zustand didn\'t go into an infinite loop', () => {
test('zustand is mocked', () => {
expect(vi.isMockFunction(zustand)).toBe(true)
})
})
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f8b525

Please sign in to comment.