I have jest mocks (mocked functions, modules and components) in my jestSetup.js
file (pointed to from package.json
), and I also have mocks in individual test files, including unit tests and snapshot tests.
In my individual snapshot tests, the mocks do not work anymore (since upgrading to RN 0.56) - the mocks exist (I can console.log them) but when the snapshot tests are run, the mocked functions are not called - instead the "real" functions are called. This is NOT the case for mocks defined the SAME way but placed in jestSetup.js
- the snapshots call the mocked functions as expected when they are defined in jestSetup.js
.
I have moved as many mocks as possible to jestSetup.js but unfortunately some tests require the functions to be unmocked (e.g. unit tests) so this cannot fix all my new errors.
Can anyone see a way to either enabled my mocks to work in individual test files or otherwise?
Profile.test.js
import 'react-native';
import renderer from 'react-test-renderer';
import React from 'react';
import sinon from 'sinon';
jest.mock(
'../../src/components/common/ProfilePicture/StarredPicture/StarredPicture',
() => () => { return ''; },
);
test('renders correctly', () => {
const stub = sinon.stub(console, 'error');
const tree = renderer.create(<Profile />).toJSON();
const tree = renderer
.create(<Profile />)
.toJSON();
expect(tree).toMatchSnapshot();
// call count is one to reflect one required proptypes.
expect(stub.callCount).toEqual(1);
package.json
"jest": {
"automock": false,
"preset": "react-native",
"setupFiles": [
"./src/jestSetup",
],
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
"transformIgnorePatterns": [
"node_modules/(?!@expo)/"
],
"collectCoverageFrom": [
"src/**/*.js"
]
}
Aucun commentaire:
Enregistrer un commentaire