When I try to test a reaction component, I get errors from other components that are not imported into the module under test.
I would expect these errors to occur if I imported the module, as I am currently reorganizing a lot of code and have not yet bypassed these files.
It is almost as if Jest was running all my components before testing. Here is one of the test files that cause this problem:
import React from 'react';
import { LoginPage } from 'components';
describe('Login Page', () => {
it('should render', () => {
expect(shallow(<LoginPage />)).toMatchSnapshot();
});
it('should use background passed into props', () => {
const image = 'bg.png';
const expected = {
backgroundImage: image
};
const wrapper = shallow(<LoginPage background={image} />);
expect(wrapper.prop('style')).toEqual(expected);
});
});
Loginpage.js
import React from 'react';
import { LoginForm } from 'components';
import './LoginPage.css';
type Props = { background: string , logInRequest: Function};
const LoginPage = ({ background, logInRequest }: Props) => (
<div className="login-page" style={{backgroundImage: background}}>
<LoginForm submit={logInRequest}/>
</div>
);
Here setupTests.js
import Enzyme, { shallow, mount, render } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import localStorage from 'mock-local-storage';
Enzyme.configure({ adapter: new Adapter() });
global.requestAnimationFrame = function(callback) {
setTimeout(callback, 0);
};
global.shallow = shallow;
global.mount = mount;
global.render = render;
console.log = () => ({});
Stack trace:
at invariant (node_modules/invariant/invariant.js:42:15)
at wrapWithConnect (node_modules/react-redux/lib/components/connectAdvanced.js:101:29)
at Object.<anonymous> (src/containers/ApplicationList/ApplicationList.js:8:42)
at Object.<anonymous> (src/containers/index.js:9:41)
at Object.<anonymous> (src/components/CustomerDashboard/CustomerDashboard.js:2:19)
at Object.<anonymous> (src/components/index.js:14:43)
at Object.<anonymous> (src/components/LoginPage/LoginPage.test.js:2:19)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
From reading the stack trace, I can assume that Jest checks the export list inside components/index.js
and containers/index.js
.
? containers/ApplicationList
LoginPage
, .
, CustomerDashboard
, , , LoginPage
import LoginPage from './LoginPage
, LoginPage
, import { LoginPage } from 'components'
?