I created a new project with create-react-library. I am trying to add a unit test using Jest and Enzyme. My test works for a simple component, but Jest fails to test a component that references semantic-ui-react.
This test works:
// src/VanillaComponent.test.js
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
const VanillaComponent = () => <div>Test</div>;
test('Enzyme can render a vanilla component', () => {
expect(shallow(<VanillaComponent />)).toBeDefined();
});
This test does not work:
// src/SemanticComponent.test.js
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { Button } from 'semantic-ui-react';
Enzyme.configure({ adapter: new Adapter() });
const SemanticComponent = () => <Button>Test</Button>;
test('Enzyme can render a semantic-ui-react component', () => {
expect(shallow(<SemanticComponent />)).toBeDefined();
});
When I run npm test
on the test above, I get the following error:
● Test suite failed to run
Cannot find module 'semantic-ui-react' from 'SemanticComponent.test.js'
> 4 | import { Button } from 'semantic-ui-react';
| ^
Relevant parts of package.json
file (lots of unrelated content removed):
{
"scripts": {
"test": "run-s test:unit test:lint test:build",
"test:build": "run-s build",
"test:lint": "eslint .",
"test:unit": "cross-env CI=1 react-scripts test --env=jsdom",
"test:watch": "react-scripts test --env=jsdom"
},
"peerDependencies": {
"react": "^16.0.0",
"semantic-ui-react": "^0.88.2"
},
"devDependencies": {
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "^3.4.1"
},
}
How can I get this second test to run? Am I missing some Jest configuration?