Jestjs 模拟特定测试组的模块中的常量属性

Jestjs 模拟特定测试组的模块中的常量属性,jestjs,react-testing-library,Jestjs,React Testing Library,我有一个MyComponent.tsx模块,它使用constants.ts模块根据IS\u IOS常量执行操作: import React from 'react'; import { Text, View } from 'react-native'; import { platform } from '../../constants'; const { IS_IOS } = platform; interface Props {} export const MyComponent = (

我有一个
MyComponent.tsx
模块,它使用
constants.ts
模块根据
IS\u IOS
常量执行操作:

import React from 'react';
import { Text, View } from 'react-native';

import { platform } from '../../constants';

const { IS_IOS } = platform;
interface Props {}

export const MyComponent = (props: Props) => {
  return (
    <View>
      <Text>Alpha Beta { String(IS_IOS) }</Text>
    </View>
  );
};
这是我进入控制台的结果:

  console.log
    <View>
      <Text>
        Alpha Beta 
        true
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)

  console.log
    <View>
      <Text>
        Alpha Beta 
        true
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)
正如我所说,这两种情况下的模拟:

  console.log
    <View>
      <Text>
        Alpha Beta 
        false
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)

  console.log
    <View>
      <Text>
        Alpha Beta 
        false
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)
console.log
α-β
假的
在debugDeep(node_modules/@testing library/react native/build/helpers/debugDeep.js:19:13)
console.log
α-β
假的
在debugDeep(node_modules/@testing library/react native/build/helpers/debugDeep.js:19:13)
我试过,试过添加
\uu esModule
——都不管用。如何在每次测试中模拟不同的模块


提前感谢您的时间

jest.resetModules
不能影响已导入的模块。它应该与测试本地的
require
相结合,以便特定于测试的
jest.mock
影响它

ES模块应使用magic
\uuu esModule
属性进行模拟,以使其不被处理为CommonJS模块:

jest.mock('../../../src/constants', () => ({
  __esModule: true,
  platform: { IS_IOS: false }
}));
const { MyComponent } = require('../../../src/MyComponent');

谢谢你的意见,但是我如何利用这些知识来解决我的问题呢?我无法将MyComponent的导入移动到我的测试中。您不必移动它们,尽管没有什么可以阻止您这样做。除了顶级导入之外,还应该使用它们。测试使用本地的
MyComponent
。这同样适用于依赖于测试特定模拟的任何其他导入。非常感谢!这正是我所需要的!我还必须这样编写:
{MyComponent:MyComponentLocal}
以符合eslint无阴影要求。
  console.log
    <View>
      <Text>
        Alpha Beta 
        false
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)

  console.log
    <View>
      <Text>
        Alpha Beta 
        false
      </Text>
    </View>

      at debugDeep (node_modules/@testing-library/react-native/build/helpers/debugDeep.js:19:13)
jest.mock('../../../src/constants', () => ({
  __esModule: true,
  platform: { IS_IOS: false }
}));
const { MyComponent } = require('../../../src/MyComponent');