zustand: "TypeError: create is not a function" when using with jest configured for ESM

Consider the following setup:

package.json:

{
  "name": "repro",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "dependencies": {
    "jest": "27.1.1",
    "react": "17.0.2",
    "zustand": "3.5.10"
  }
}

index.js:

import create from 'zustand'

export const useStore = create(set => ({
    foo: 0,
}))

repro.test.js:

import { useStore } from "."

test("repro", () => {
    console.log(useStore.getState())
});

Then, when running node --experimental-vm-modules node_modules/.bin/jest I get:

    TypeError: create is not a function

      3 | console.log(create)
      4 |
    > 5 | export const useStore = create(set => ({
        |                         ^
      6 |     foo: 0,
      7 | }))
      8 |

      at index.js:5:25
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)

  console.log
    { default: [Function: create] }

It seems that the import is not picking the esm bundle?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 32 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I am not using ESM but I had a similar error while running tests with jest: Test suite failed to run TypeError: (0 , _zustand.create) is not a function and what worked for me was to remove the zustand.ts file in __mocks__

I’m still running into this problem, following the instructions provided at https://github.com/pmndrs/zustand/blob/main/docs/guides/testing.mdx#typescript-usage.

Is it possible that it’s because I’m using the slices pattern with TypeScript?

I’d also note that the docs at https://docs.pmnd.rs/zustand/guides/testing#typescript-usage don’t reflect the changes described here.

@barelyhuman Thanks a lot! It worked like a charm. Thanks for your time and attention.

@mkhib ignore the above, I failed to see that there was a __mocks__ folder.

Your mocks version of zustand is to export {create} and not export default create

That should solve your issue

image

That’s actually nice, if we can configure rollup to do it

A few of my older packages has this with rollup, let me look it up