"TypeError: create is not a function" when using with jest configured for ESM
See original GitHub issueConsider 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:
- Created 2 years ago
- Comments:23 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A few of my older packages has this with rollup, let me look it up
@barelyhuman
That’s working. Thank you.