[jest.mock + Typescript] You are loading @emotion/react when it is already loaded. Running multiple instances may cause problems.
See original GitHub issueCurrent 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”:
- Install @emotion/react and @emotion/styled@next
npm install --save @emotion/react @emotion/styled@next
- Create a component “ComponentChild.tsx”
import styled from '@emotion/styled';
export const ComponentChild = styled.div`
background: red;
height: 300px;
`;
- Create a component “ComponentParent.tsx”
import { ComponentChild } from "./ComponentChild";
import styled from "@emotion/styled";
export const ComponentParent = styled(ComponentChild)`
background: blue;
`;
- 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;
- 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();
});
- 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:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
Thank you very very much @Andarist 😃