question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:23 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
barelyhumancommented, Dec 4, 2022

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

0reactions
mansoor-omranicommented, Dec 4, 2022

@barelyhuman

That’s working. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a (Zustand) function mock with Jest results in "TypeError
To do this you should use the actual store of your app const initialStoreState = useStore.getState() beforeEach(() => { useStore.
Read more >
ES6 Class Mocks - Jest
If you use arrow functions in your classes, they will not be part of the mock. ... of the class, this is the...
Read more >
Setup - Testing Library
In these docs we'll demonstrate configuring Jest, but you should be able to do similar things with any testing framework (React Testing ...
Read more >
How to solve the "is not a function" error in JavaScript
js we use require() to load external modules and files. This can cause, in some cases, an error like this: TypeError: require(...) is...
Read more >
Angular >=13 | jest-preset-angular - GitHub Pages
Angular 13 introduces ESM package format for Angular packages. jest-preset-angular currently supports testing with Jest in CommonJS mode with Angular 13 using ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found