【发布时间】:2021-02-08 03:07:44
【问题描述】:
我想在初始化后更改测试中的模拟数据。 但是当我试图改变它时,它并没有改变......我试图'clearAllMocks()'或使用 jest.spy 但没有任何改变。
- 当我说改变时,我的意思是“isTablet”数据会改变
影响根项目上许多其他测试的初始化。
jest-modules-mock.js
jest.mock('react-native-device-info', () => ({ isTablet: jest.fn() }));
组件
Welcome.tsx
const deviceType: BgImageByDevice = isTablet() ? 'tablet' : 'mobile';
export default ({ devices }: Welcome): ReactElement => {
const blabla = devices[deviceType]
}
测试
Welcome.test.tsx
const devices = {
tablet: 'tablet',
mobile: 'mobile',
},
describe('<Welcome />', () => {
test('devices should be mobile', () => {
jest.doMock('react-native-device-info',() => ({ isTablet: () => false }))
const Welcome = require('./Welcome').default
const welcomeShallow = shallow(<Welcome devices={devices} />);
const imageUrl = welcomeShallow.props().source.uri;
expect(imageUrl).toBe(devices.mobile);
});
test('devices should be tablet', () => {
jest.doMock('react-native-device-info',() => ({ isTablet: () => true }))
const Welcome = require('./Welcome').default
const welcomeShallow = shallow(<Welcome devices={devices} />);
const imageUrl = welcomeShallow.props().source.uri;
expect(imageUrl).toBe(devices.tablet);
});
}
【问题讨论】:
标签: react-native jestjs ts-jest