Skip to content

Commit

Permalink
Fix Jest example's usage of default imports
Browse files Browse the repository at this point in the history
Default exports and imports are deprecated. The default import of `zustand` generates a deprecation warning on the console. The default export that was suggested previously would actually fail to compile. See the comments at the very bottom of #559 for further discussion.

This commit fixes the example in the documentation by introducing named exports & imports instead of default exports & imports.
  • Loading branch information
adimit committed Feb 16, 2023
1 parent c1970e9 commit b566217
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions docs/guides/testing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Thus, there can be cases where the state of one test can affect another. To make

```jsx
import { create as actualCreate } from 'zustand'
// const actualCreate = jest.requireActual('zustand') // if using jest
// const { create: actualCreate } = jest.requireActual('zustand') // if using jest
import { act } from 'react-dom/test-utils'

// a variable to hold reset functions for all stores declared in the app
Expand Down Expand Up @@ -42,7 +42,7 @@ If you are using zustand, as documented in [TypeScript Guide](./typescript.md),

```tsx
import { create as actualCreate, StateCreator } from 'zustand'
// const actualCreate = jest.requireActual('zustand') // if using jest
// const { create: actualCreate } = jest.requireActual('zustand') // if using jest
import { act } from 'react-dom/test-utils'

// a variable to hold reset functions for all stores declared in the app
Expand Down Expand Up @@ -70,13 +70,13 @@ You should use the following code in the `__mocks__/zustand.js` file (the `__moc

```js
import { act } from '@testing-library/react-native'
const actualCreate = jest.requireActual('zustand')
const { create: actualCreate } = jest.requireActual('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 = (createState) => {
export const create = (createState) => {
const store = actualCreate.default(createState)
const initialState = store.getState()
storeResetFns.add(() => {
Expand All @@ -93,8 +93,6 @@ beforeEach(async () => {
})
)
})

export default create
```

If the `jest.config.js` has `automock: false`, then you need to do the following in `jest.setup.js`:
Expand Down

0 comments on commit b566217

Please sign in to comment.