React native 初始化后的模拟更改

React native 初始化后的模拟更改,react-native,jestjs,ts-jest,React Native,Jestjs,Ts Jest,我想在初始化模拟数据后更改测试中的模拟数据。 但当我试图改变它时,它没有改变。。。我试图“clearAllMocks()”或使用jest.spy,但没有任何改变 当我说更改时,我的意思是“iTablet”数据将更改 初始化会影响根项目上的许多其他测试 jest-modules-mock.js jest.mock('react-native-device-info', () => ({ isTablet: jest.fn() })); 组件 Welcome.tsx const de

我想在初始化模拟数据后更改测试中的模拟数据。 但当我试图改变它时,它没有改变。。。我试图“clearAllMocks()”或使用jest.spy,但没有任何改变

  • 当我说更改时,我的意思是“iTablet”数据将更改
初始化会影响根项目上的许多其他测试

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);
    });
}
Welcome.test.tsx
常数设备={
平板电脑:“平板电脑”,
手机:“手机”,
},
描述(“”,()=>{
测试('设备应该是移动的',()=>{
doMock('react-native-device-info',()=>({isTablet:()=>false}))
const Welcome=require('./Welcome')。默认值
const welcomeshalf=浅();
const imageUrl=welcomeshalf.props().source.uri;
expect(imageUrl).toBe(devices.mobile);
});
测试('设备应为平板电脑',()=>{
doMock('react-native-device-info',()=>({isTablet:()=>true}))
const Welcome=require('./Welcome')。默认值
const welcomeshalf=浅();
const imageUrl=welcomeshalf.props().source.uri;
expect(imageUrl).toBe(devices.tablet);
});
}

您可以使用模拟文件更改模拟的值。您可以创建和导出模拟函数,可以在测试中导入该函数,并更改实现或返回值。下面是此过程的示例

\uuuuu mocks\uuuu\react native device info.js
Welcome.test.js
从“酶”导入{shall};
从“./Welcome”导入Welcome;
从“./\uuuu mocks\uuuu/react native device info”导入{isTablet};
常数设备={
平板电脑:“平板电脑”,
手机:“手机”,
};
描述(“”,()=>{
测试('设备应该是移动的',()=>{
isTablet.mockReturnValue(false);
const welcomeshalf=浅();
//你的主张就在这里
});
测试('设备应为平板电脑',()=>{
isTablet.mockReturnValue(true);
const welcomeshalf=浅();
//你的主张就在这里
});
});

你好,谢谢,我仍然收到“背景图像›设备应为平板电脑预期(已接收)。toBe(预期)//Object.is预期相等:“平板电脑”预期为:“手机”您能在您的问题中添加更新的代码吗?我还想看看source.uri道具来自何处,因为在上面的示例中,我的代码应该可以工作,但我认为欢迎组件中遗漏了一些东西,这将改变情况。
export const isTablet = jest.fn();
import { shallow } from "enzyme";
import Welcome from "../Welcome";
import { isTablet } from "../__mocks__/react-native-device-info";

const devices = {
  tablet: 'tablet',
  mobile: 'mobile',
};

describe('<Welcome />', () => {
  test('devices should be mobile', () => {
    isTablet.mockReturnValue(false);
    const welcomeShallow = shallow(<Welcome devices={devices} />);
    // Your assertions go here
  });

  test('devices should be tablet', () => {
    isTablet.mockReturnValue(true);
    const welcomeShallow = shallow(<Welcome devices={devices} />);
    // Your assertions go here
  });
});