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.

Cannot import a jest.mock() when running tests with --coverage

See original GitHub issue

Steps:

  • Create a new app: create-react-app my-app
  • Create a new file that does a jest.mock(), ex:

src/mock.js

jest.mock('react-dom', () => {})
  • Import this file in App.test.js:

src/App.test.js

import './mock.js'

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
...
  • Run the tests with --coverage:
npm run test -- --coverage

I get this error: image

I can’t find a workaround for this, there’s no documentation on how to import mocks so I can reuse them.

@cpojer It may be a Jest issue ? Am I doing something wrong ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
cpojercommented, Sep 21, 2016

We’ll publish a fix with Jest 16 likely later this week.

0reactions
gaearoncommented, Sep 18, 2016

Moving the milestone because I want to release 0.4.2 today, and this is not fixed yet. @cpojer Could you provide any estimate on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jest mock not covering function or returning error
Here is the unit test solution: api.js : const csv = require('csvtojson'); const request = require('request'); export const getApiData = url ...
Read more >
The module factory of `jest.mock()` is not allowed to ... - GitHub
I'm using the snippet from #1960 to mock Picker in RN import React, {Component} from 'react'; jest.mock(`Picker`, () => { // ...etc }) ......
Read more >
How to mock imported functions with Jest - DEV Community ‍ ‍
To mock an imported function with Jest we use the jest.mock() function. jest.mock() is called with one required argument - the import path...
Read more >
Configuring Jest
This option tells Jest that all imported modules in your tests should be mocked automatically. All modules used in your tests will have...
Read more >
Unit testing | single-spa
This is necessary because the test environment cannot dynamically import ... Create a jest config file; Setup a mocks directory at the root;...
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