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.

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:

  1. Clone https://github.com/AKPWebDesign/JestIssue
  2. yarn - Install dependencies
  3. yarn test - Fails
  4. rm .babelrc - Remove Babel config
  5. yarn 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:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Jun 6, 2018

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 above require, or remove ignore from .babelrc

0reactions
github-actions[bot]commented, May 12, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

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