React Native:在应用程序构建后,从 jest 中的测试文件更改 redux 状态的变量。 由于我无法在测试文件中编写整个逻辑、组件和 props,因此我只需要在 React Native 应用程序中传递一个变量,并通过玩笑测试更改其值,因此方法是我在 redux 状态下添加一个变量,我想在应用程序运行时更改该变量。
问题甚至在开始测试之前就发生在导入中:
登录.test.js
import { by, device, element, waitFor } from 'detox';
import configureStore from '../../src/createStore';
describe('Example', () => {
const store = configureStore();
const { dispatch } = store;
beforeAll(async () => {
console.log(JSON.stringify({ store }, null, 2));
console.log(JSON.stringify({ state: store.getState() }, null, 2));
await device.launchApp();
});
jest.config.json
注意:transformIgnorePatterns 有react-native-localize
{
"maxWorkers": 1,
"testTimeout": 120000,
"verbose": true,
"rootDir": "../..",
"reporters": ["detox/runners/jest/reporter"],
"globalSetup": "detox/runners/jest/globalSetup",
"globalTeardown": "detox/runners/jest/globalTeardown",
"testEnvironment": "detox/runners/jest/testEnvironment",
"cacheDirectory": ".jest/cache",
"moduleFileExtensions": ["js", "jsx", "json", "svg"],
"modulePathIgnorePatterns": ["lib/"],
"preset": "react-native",
"testPathIgnorePatterns": ["\\.snap$", "<rootDir>/node_modules/"],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|ts)x?$",
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.svg$": "jest-transformer-svg"
},
"transformIgnorePatterns": [
"node_modules/(?!(@ptomasroos|axios|(jest-)?@?react-native|react-native-modal|react-navigation|react-navigation-tabs|react-navigation-redux-helpers|react-native-safari-view|react-native-linear-gradient|react-native-blur|react-native-animatable|react-native-wkwebview-reborn|react-native-safe-area-view|react-native-popup-menu|redux-persist|react-native-localize|react-native-reanimated)/)"
]
}
完整错误消息:
Invariant Violation: TurboModuleRegistry.getEnforcing(...): 'RNLocalize' could not be found. Verify that a module by this name is registered in the native binary.
6 | import React from 'react';
> 7 | import * as RNLocalize from 'react-native-localize';
| ^
at invariant (node_modules/invariant/invariant.js:40:15)
at Object.getEnforcing (node_modules/react-native/Libraries/TurboModule/TurboModuleRegistry.js:43:12)
at Object.getEnforcing (node_modules/react-native-localize/src/NativeRNLocalize.ts:18:36)
at Object.require (node_modules/react-native-localize/src/module.ts:1:1)
at Object.require (node_modules/react-native-localize/src/index.ts:1:1)
更新 React Native 库添加对新 Fabric 架构的支持后,我注意到同样的错误。
使用 Jest 运行的测试是在 NodeJS(或 JSDom)环境中进行的,因此所有本机模块都不可用。一些 React Native 库为这种情况提供了模拟。对于所有其他的,您可以像这样提供自己的模拟:
在
jest.config.json
{
setupFilesAfterEnv: ['<rootDir>/src/__mocks__/jestSetup.js'],
[your existing config]
}
这将在
每个测试文件之前运行
jestSetup.js
的内容。
然后在
jestSetup.js
中创建一个新的
src/__mocks__
文件
/*
* Turbo modules mocks.
*/
jest.mock('react-native/Libraries/TurboModule/TurboModuleRegistry', () => {
const turboModuleRegistry = jest.requireActual('react-native/Libraries/TurboModule/TurboModuleRegistry');
return {
...turboModuleRegistry,
getEnforcing: (name) => {
// List of TurboModules libraries to mock.
const modulesToMock = ['RNLocalize'];
if (modulesToMock.includes(name)) {
return null;
}
return turboModuleRegistry.getEnforcing(name);
},
};
});
请注意,这不是使用 Jest 提供模拟的唯一方法,但这应该适用于大多数项目。