I'm trying to create an automatic mock using jest in my ES6 javascript project.
I'm using node v15.0.1
, and jest 26.6.0
on ubuntu 18.04.5
.
I have a test file containing the following code:
import RenderBuffer from './renderbuffer.js'
jest.mock('./renderbuffer.js');
beforeEach(() => {
RenderBuffer.mockClear();
});
When I run the test I run into the following issue:
ReferenceError: require is not defined
4 |
5 | beforeEach(() => {
> 6 | RenderBuffer.mockClear();
| ^
7 | });
8 |
The error is surprising to me as I'm not using a require statement.
My package.json config contains the following:
"type": "module",
"main": "src/index.js",
"devDependencies": {
"jest": "^26.5.3",
"jest-canvas-mock": "^2.3.0"
},
"jest": {
"setupFiles": ["jest-canvas-mock"]
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"test-coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
}
Any ideas as to what the root cause of this issue is?
--experimental-vm-modules
. The support of ESM in Jest is experimental. It's expected to be lacking and buggy. This includes module mocking. Don't use it for real work. – Heterophyllous