React native:在应用程序构建后,在jest中从测试文件中更改redux状态中的变量。由于我无法在测试文件中编写整个逻辑,组件和props,我需要在react本地应用中传递一个变量,并在jest测试中更改其值,因此方法是我在redux状态中添加了一个变量,并且我希望在应用程序运行时更改该变量。
在开始测试之前,问题就发生在导入中:
login.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)
型
1条答案
按热度按时间uqdfh47h1#
在更新React Native库添加对新Fabric架构的支持后,我注意到了同样的错误。
使用Jest运行的测试是在NodeJS(或JSDom)环境中,因此所有的本地模块都不可用。一些React Native库提供了这种情况下的mock。对于所有其他库,您可以提供自己的mock,如下所示:
单位:
jest.config.json
字符串
这将在每个测试文件之前运行
jestSetup.js
的内容。然后在
src/__mocks__
中创建新的jestSetup.js
文件型
请注意,这并不是为mock提供Jest的唯一方法,但这一种方法应该适用于大多数项目。