Jest module mocking fails when .babelrc exists
See original GitHub issue🐛 Bug Report
I’ve got an ES6+ project, using Jest for testing. Due to client-side requirements, I needed to add Babel to the project to compile to ES5. After setting up Babel (and doing nothing else), my project’s tests no longer pass. I have attempted to add a "transform": {}
section to the Jest config in my package.json
, but this doesn’t solve the issue either.
To Reproduce
Steps to reproduce the behavior:
- Clone https://github.com/AKPWebDesign/JestIssue
yarn
- Install dependenciesyarn test
- Failsrm .babelrc
- Remove Babel configyarn test
- Passes
Expected behavior
I expected this bare-bones Babel config to have no effect on Jest, and for Jest to continue working as it did previously.
Run npx envinfo --preset jest
npx: installed 1 in 2.03s
System:
OS: macOS High Sierra 10.13.4
CPU: x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
Binaries:
Node: 8.11.2 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 5.6.0 - /usr/local/bin/npm
npmPackages:
jest: ^23.1.0 => 23.1.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Cannot mock a module with jest, and test function calls
My tests look something like that: import React from 'react'; import { shallow } from 'enzyme'; import should from 'should/as-function'; import ...
Read more >The Jest Object
The most common use of this API is for specifying the module a given test intends to be testing (and thus doesn't want...
Read more >babel-jest - Awesome JS
[docs] Update link to Jest 28 upgrade guide in error message (#13483) ... [jest-mock] Revert #13145 which broke mocking of transpiled ES modules....
Read more >test suite failed to run jest encountered an unexpected token ...
Out of the box Jest supports Babel, which will be used to transform your ... If you simply want to mock your non-JS...
Read more >How to fix Jest and Mock Service Worker integration errors
It is very strange because I changed nothing and it suddenly complains typescript doesn't exist. Error: Cannot find module 'typescript' Require ...
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 FreeTop 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
Top GitHub Comments
The issue is that you set babel to ignore the test directory, and your test relies on mock hoisting (which is done using babel).
You can move your
jest.mock
call aboverequire
, or removeignore
from.babelrc
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.