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.mock + Typescript] You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems.

See original GitHub issue

Current behaviour:

Inside a unit test, after using jest.mock(‘./MyComponent’) we get the error message added at #1677

console.warn node_modules/@emotion/react/dist/react.cjs.dev.js:490
    You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems. This can happen if multiple versions are used, or if multiple builds of the same version are used.

To reproduce: Option 1 - Cloning:

git clone https://github.com/bmvantunes/emotion-v11-bug-jest.git
cd emotion-v11-bug-jest
npm test

Option 2 - Step by step after “npx create-react-app my-app --typescript”:

  1. Install @emotion/react and @emotion/styled@next
npm install --save @emotion/react @emotion/styled@next
  1. Create a component “ComponentChild.tsx”
import styled from '@emotion/styled';

export const ComponentChild = styled.div`
  background: red;
  height: 300px;
`;
  1. Create a component “ComponentParent.tsx”
import { ComponentChild } from "./ComponentChild";
import styled from "@emotion/styled";

export const ComponentParent = styled(ComponentChild)`
  background: blue;
`;
  1. Change your “App.tsx” to be:
import React from 'react';
import { ComponentParent } from './ComponentParent';
import { ComponentChild } from './ComponentChild';

const App: React.FC = () => {
  return (
    <>
      <ComponentParent />
      <ComponentChild />
      <p>Test</p>
    </>
  );
}

export default App;
  1. Then in your “App.test.tsx”:
import React from 'react';
import { render } from '@testing-library/react';
import App from './App';

jest.mock('./ComponentParent');

test('renders learn react link', () => {
  const { getByText } = render(<App />);
  const test = getByText(/Test/i);
  expect(test).toBeInTheDocument();
});
  1. Run test command
npm test

Expected behaviour:

No warning should be seen in the console

Versions React - latest typescript - 3.7.4 react: 16.12.0 @emotion/react - 11.0.0-next.11 @emotion/styled - 11.0.0-next.11

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Andaristcommented, Mar 14, 2020

I’ve reported it to the Jest team and prepared a PR to mute this warning in Jest for the time being: https://github.com/emotion-js/emotion/pull/1806 . Should get released in a couple of days.

1reaction
bmvantunescommented, Mar 14, 2020

Thank you very very much @Andarist 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: does not match the corresponding name on disk/ emotion
... console.warn('You are loading @emotion/react when it is already loaded. Running ' + 'multiple instances may cause problems. This can ...
Read more >
Mock Functions - Jest
An array that contains all the object instances that have been instantiated from this mock function using new . For example: A mock...
Read more >
gatsby-background-image
is a React component which for background-images provides, what Gatsby's own does for the rest of your images and even more: Testing…
Read more >
Jest mock referenceerror cannot access before initialization
I suppose you have basic knowledge of using these two. js modules for database ... the browser is not able to load the...
Read more >
Configure Storybook
Storybook is configured via a folder called .storybook , which contains various configuration files. Note that you can change the folder that Storybook...
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