Questions tagged [babel-jest]

A Babel transpiler to assist with the conversion of ES6 to ES5 for the Jest testing framework.

A Babel plugin to assist ES6 to ES5 transpilation for the Jest testing framework.

607 questions
308
votes
26 answers

How to resolve "Cannot use import statement outside a module" from Jest when running tests?

I have a React application (not using Create React App) built using TypeScript, Jest, Webpack, and Babel. When trying to run yarn jest, I get the following error: I have tried removing all packages and re-adding them. It does not resolve this. I…
Logan Shoemaker
  • 3,377
  • 2
  • 13
  • 11
183
votes
12 answers

Does Jest support ES6 import/export?

If I use import/export from ES6 then all my Jest tests fail with error: Unexpected reserved word I convert my object under test to use old school IIFE syntax and suddenly my tests pass. Or, take an even simpler test case: var Validation =…
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
147
votes
18 answers

Jest won't transform the module - SyntaxError: Cannot use import statement outside a module

I couldn't get rid of this SyntaxError: Cannot use import statement outside a module error no matter what I have tried and it got so frustrating. Is there anybody out here solved this issue? I have read a million stackoverflow and github issue…
dugong
  • 3,690
  • 4
  • 11
  • 27
111
votes
6 answers

Mocking globals in Jest

Is there any way in Jest to mock global objects, such as navigator, or Image*? I've pretty much given up on this, and left it up to a series of mockable utility methods. For example: // Utils.js export isOnline() { return…
Andrew
  • 14,204
  • 15
  • 60
  • 104
98
votes
11 answers

`regeneratorRuntime` is not defined when running Jest test

The title pretty much explains what I'm facing. I'm trying to test a React component that has some state, and I attempt to provide my store to the component in order to get what it needs. When I run the test of the component using Jest, I get the…
rafafan2010
  • 1,559
  • 2
  • 14
  • 23
91
votes
1 answer

What is the difference between babel-core and @babel/core?

What is the difference between babel-core and @babel/core ? Are they the same thing but different versions? If not so, when do you use one and when do you use the other? Thank you.
YulePale
  • 6,688
  • 16
  • 46
  • 95
84
votes
9 answers

Mocking `document` in jest

I'm trying to write tests for my web components projects in jest. I already use babel with es2015 preset. I'm facing an issue while loading the js file. I have followed a piece of code where document object has a currentScript object. But in test…
thecodejack
  • 12,689
  • 10
  • 44
  • 59
76
votes
2 answers

Error while loading config - You appear to be using a native ECMAScript module configuration file

This error is coming up when I am making a pull request. There is a GitHub workflow audit that runs checks on the pull request and it loads the test file from another repository. - name: Run Audits run: npx jest audits/ch-2 --json…
tksilicon
  • 3,276
  • 3
  • 24
  • 36
52
votes
19 answers

jest hangs indefinitely, runs no tests

Every time I run jest it never runs anything. I have let the counter go arbitrarily high. I have run jest with --no-cache jest --debug output is as follows: { "configs": [ { "automock": false, "browser": false, "cache":…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
46
votes
5 answers

Unexpected token 'import' error while running Jest tests?

I realize this question has been asked several times but all of the solutions I've come across don't seem to work for me. I'm running into the following error while trying to run Jest tests for a Vue app. Jest encountered an unexpected token This…
Don
  • 3,876
  • 10
  • 47
  • 76
41
votes
5 answers

Jest transformIgnorePatterns not working

I have spent a long time looking at other questions about this and looking at other projects on Github but none of the answers seem to work for me. I am loading a third party library in my project, and when running Jest tests I get the error export…
Heather Roberts
  • 1,978
  • 1
  • 24
  • 33
40
votes
4 answers

`toBeInstanceOf(Number)` does not work in jest

I have a test as follows: expect(result.data.quota).toBeInstanceOf(Number); This test fails with a weird error saying the a Number was expected and a Number was received: expect(value).toBeInstanceOf(constructor) Expected constructor: Number …
user6269864
38
votes
6 answers

Jest + Babel in VS Code debugger causes breakpoints to move

I am trying to debug a simple project using babel, jest, and vs code. When I set a breakpoint and then start debugging, my breakpoints jump around and are no longer where they were when I started. A sample repo can be seen here -…
RyanHirsch
  • 1,847
  • 1
  • 22
  • 37
37
votes
4 answers

jest config is throwing "type ErrorHandler = (error: mixed, isFatal: boolean) => void" after update to 26.x

i don't know why this is suddenly not working but this is my jest.config.js: module.exports = { preset: 'react-native', verbose: true, moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], setupFiles: ['./jestSetup.js'], …
gpbaculio
  • 5,693
  • 13
  • 60
  • 102
35
votes
2 answers

How to assert function invocation order in jest

I am mocking two functions with with jest.fn: let first = jest.fn(); let second = jest.fn(); How can I assert that first called before second? What I am looking for is something like sinon's .calledBefore assertion. Update I used this simple…
Ahmed Ayoub
  • 694
  • 1
  • 7
  • 12
1
2 3
40 41