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.

[Bug] Unable to mock default exported Component when same module also has named export

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? I have an ES6 module that exports a React Component class by default, but also exports a plain JS function as a named export. When testing other packages that use this module, I want to mock both the default exported component and named exported function to keep my unit tests pure.

The module looks something like this:

import React, { Component } from 'react';

export default class MyComponent extends Component {
  render() {
    return <div>Hello</div>
  }
}

export function myUtilityFunction() { return 'foo' };

I would like to use the following syntax to mock the exports:

import React from 'react';
import MyComponent, { myUtilityFunction } from './module';

jest.mock('./module');
MyComponent.mockImplementation(() => 'MockComponent');
myUtilityFunction.mockImplementation(() => 'foo');

When I try to use this syntax, however, MyComponent does not appear to be mocked when used within other components. When I try to mock MyComponent like this and render it on its own, it renders out to null.

This behavior is very strange, because if I use the exact same syntax, but both imports are JavaScript functions, the mocking works as expected. See the StackOverflow issue I opened here that confirms that the syntax works when the imports are both functions.

Here is a GitHub repo demoing the problem, as well as several solutions I’ve tried: https://github.com/zpalexander/jest-enzyme-problem

What is the expected behavior?

I should be able to import the default exported component and the named exported function into my test file, and mock both using jest.mock('path') and the .mockImplementation() method.

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.

See this repo for an exact reproduction: https://github.com/zpalexander/jest-enzyme-problem

You can build the repo and run the tests with yarn install && yarn test

Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

81reactions
joaovieiracommented, Jun 14, 2018

For others, this helped me out:

jest.mock('./myModule', () => ({
  __esModule: true,
  namedExport: jest.fn(),
  default: jest.fn(),
}));

import myModule, { namedExport } from './myModule';
3reactions
zpalexandercommented, Nov 1, 2018

To follow up, the solution I ended up using was:

MyComponent.mockImplementation(() => {
  return {
    render: () => <div>MockComponent</div>
  };
});

The solution that @joaovieira posted works as well.

Hopefully Jest can eventually provide users with a mocking API that requires less boilerplate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking default export is failing but named export not
I am trying to mock this exports in my test using jest. Lets say file.test.js is test file. jest.mock('./path/file', ...
Read more >
Jest mock default and named export - remarkablemark
If your module only has a default export and no named exports, then __esModule: true isn't necessary since you can treat the mock...
Read more >
ES6 Class Mocks - Jest
If the class is not the default export from the module then you need to return an object with the key that is...
Read more >
expected the result of a dynamic import() call. instead received
If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default. Another,...
Read more >
Mocking ES and CommonJS modules with jest.mock() - Medium
We'll use default exports, but the same principle applies to named exports. The standard ES Module export / import syntax would be:
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