我在jest
应用中使用react native
进行单元测试,这通常可以正常使用。
尝试使用realm
会破坏测试并出现此错误:
Runtime Error
- Error: Cannot find module 'throwOnWrongReactAPI' from 'react-native.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:197:17)
at Runtime._resolveModule (node_modules/jest-runtime/build/index.js:304:32)
at Runtime._getNormalizedModuleID (node_modules/jest-runtime/build/index.js:465:31)
at Runtime._shouldMock (node_modules/jest-runtime/build/index.js:511:27)
at Runtime.requireModuleOrMock (node_modules/jest-runtime/build/index.js:263:14)
at eval (node_modules/react-native/Libraries/react-native/react-native.js:178:32)
at Object.<anonymous> (node_modules/react-native/Libraries/react-native/react-native.js:185:492)
at Runtime._execModule (node_modules/jest-runtime/build/index.js:352:17)
at Runtime.requireModule (node_modules/jest-runtime/build/index.js:202:14)
at Runtime._generateMock (node_modules/jest-runtime/build/index.js:418:34)
1 test suite failed, 0 tests passed (0 total in 1 test suite, run time 6.482s)
这是测试文件:
jest.unmock('../classes/feed.js');
import Feed from '../classes/feed';
describe('Feed class', () => {
it('returns [1, 2] when calling _slice([0, 1, 2], 1)', () => {
expect(Feed._slice([0, 1, 2], 1)).toEqual([1, 2]);
});
});
这是文件../classes/feed.js
:
import Realm from 'realm';
class Feed {
_slice(a, b) {
return [1, 2];
}
};
export default new Feed();
这是我的package.json
:
"dependencies": {
"babel-preset-react": "^6.11.1",
"flux": "^2.1.1",
"react": "^15.1",
"react-native": "^0.28",
"react-native-collapsible": "^0.6.0",
"react-native-fbsdk": "^0.2.1",
"react-native-scrollable-tab-view": "git+https://github.com/skv-headless/react-native-scrollable-tab-view.git",
"react-native-vector-icons": "^2.0.2",
"realm": "^0.13.2"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-jest": "^13.0.0",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.9.0",
"jest-cli": "^13.0.0"
},
"jest": {
"globals": {
"__DEV__": true
}
}
我的.babelrc
:
{
"presets": ["es2015", "react"]
}
调用throwOnWrongReactAPI
这一事实表明模块react
已被弃用(请参阅RN v0.25.1 release notes),但堆栈跟踪没有帮助,而且我< em>确实在过去解决了这个问题。
我做错了什么?
答案 0 :(得分:2)
由runnung npm install --save-dev babel-preset-react-native
解决并将.babelrc
更改为
{
"presets": ["react-native"]
}