我有一个我想测试的模块。该模块正在使用其他外部模块,并且开玩笑很难解决它们。
它正在解决基本路径: ./ node_modules /反应标签的输入/ DIST模块/
而不是: ./ node_modules /反应标签的输入/ node_modules /反应-DND
下面是正在发生的事情的简化版本:
错误:
Cannot find module 'react-dnd' from 'reactTags.js'
at Resolver.resolveModule (node_modules/jest-cli/node_modules/jest-resolve/build/index.js:160:17)
at Object.<anonymous> (node_modules/react-tag-input/dist-modules/reactTags.js:13:17)
at Object.<anonymous> (src/tagger/eventtagger.js:5:404)
这是我的测试文件: 的 caseevent.test.js
import EventTagger from './eventtagger.js';
eventtagger.js
import ReactTags from 'react-tag-input';
的package.json
"jest": {
"moduleFileExtensions": [
"js",
"jsx"
],
"modulePaths": [
"<rootDir>"
],
},
"dependencies": {
"babel-jest": "^17.0.2",
"flux": "^2.0.1",
"install": "^0.8.1",
"jest": "^17.0.3",
"lodash.flow": "^3.3.0",
"npm": "^3.10.6",
"object-assign": "^1.0.0",
"react": "^15.4.0",
"react-dnd": "git+https://github.com/jcrogel/react-dnd.git",
"react-dnd-html5-backend": "^2.0.0",
"react-dom": "^15.4.0",
"react-tag-input": "^4.4.0",
"react-tagcloud": "^0.6.1"
},
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.14.0",
"babel-eslint": "^4.1.3",
"babel-loader": "^6.2.5",
"babel-preset-es2015": "^6.0.15",
"babel-preset-react": "^6.0.15",
"babel-preset-stage-0": "^6.0.15",
"babel-register": "^6.9.0",
"chai": "^3.5.0",
"enzyme": "^2.6.0",
"jest-cli": "^17.0.3",
"jsdom": "^9.4.0",
"lodash": "4.14.1",
"react-addons-test-utils": "^15.4.0",
"react-native": "^0.38.0",
"react-test-renderer": "15.4.0",
"sinon": "^1.17.4",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1",
"whatwg-fetch": "^2.0.1"
}
我错过了什么?如何让jest-resolve找到或模拟更深层次的组件?
答案 0 :(得分:1)
我也有同样的问题。我尝试过的一些事情:
jest.unmock(模块)位于测试文件的顶部 在Jest安装文件中填充模块 jest.mock(jest.fn()=&gt; {console.log(&#39;做某事&#39;)) 最后,这似乎有效,但我无法找到相关文档:
在测试文件的顶部:
/ ** * @providesModule moduleName * /
这似乎认出了我需要的模块。 Jest应该隐含地看到开箱即用的模块,但我还没有那种经验。