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.

Weird `jest.mock` behaviour that forces code duplication

See original GitHub issue

This code works:

jest.mock('../../Spinner', () => () => <div>loading...</div>)

This code does not work:

const MockSpinner = () => <div>loading...</div>
jest.mock('../../Spinner', () => MockSpinner)

Error:

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Importing the mock spinner component from another file also fails with error:

import Spinner from '__mocks__/Spinner'

jest.mock('../../Spinner', () => Spinner)

Error:

babel-plugin-jest-hoist: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.

How can I prevent code duplication when mocking jest components? 😦

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

2reactions
developer239commented, Sep 2, 2018

I also tried __mocks__ folder:

// none of these work
__mocks__/src/components/Spinner
__mocks__/components/Spinner
__mocks__/Spinner

Then in my test file:

jest.mock('../../Spinner')

Results in error:

Invariant Violation: SpinnerComponent(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
0reactions
github-actions[bot]commented, Apr 30, 2022

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

Jest: avoiding manual mock code duplication - Stack Overflow
I have the module mocked in one of my repos, by creating a manual mock in the mocks directory, and that works fine....
Read more >
The only 3 steps you need to mock an API call in Jest
The unmocked code. We're going to be testing this getFirstAlbumTitle() function, which fetches an array of albums from an API and returns the ......
Read more >
Integrated Tests Are A Scam - The Code Whisperer
If you test mostly through integrated tests, then you'd have to write the same tests over and over again for every date field,...
Read more >
Effective Python Testing With Pytest
This requires duplicating test code, and doing so can sometimes obscure the behavior that you're trying to test.
Read more >
The last guide to the useEffect Hook you'll ever need
For example, the official React docs show that you can avoid the duplicated code that results from lifecycle methods with one useEffect ...
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