我有这个组件:
import React from 'react';
import VideoTag from './VideoTag';
import JWPlayer from './JWPlayer';
class VideoWrapper extends React.Component {
//... component code
}
基于某些逻辑在内部呈现另一个组件(VideoTag 或 JWPlayer)但是当我尝试在一个 Jest 文件中测试它时我得到错误:
找不到模块'./VideoTag'
这三个组件在同一个目录中,这就是为什么当我转译它并在浏览器中看到它在运行时它实际上有效但看起来 Jest 在解析这些相对路径时遇到问题,这是 jest 文件:
jest.dontMock('../src/shared/components/Video/VideoWrapper.jsx');
import React from 'react';
import ReactDOM from 'react-dom';
TestUtils from 'react-addons-test-utils';
import VideoWrapper from '../src/shared/components/Video/VideoWrapper.jsx';
describe('VideoWrapper tests', () => {
it('Render JWPlayer', () => {
let vWrapper = TestUtils.renderIntoDocument(
< VideoWrapper model={model} />
);
});
});
错误在行:
import VideoWrapper from '../src/shared/components/Video/VideoWrapper.jsx';
我如何告诉 jest 如何处理相对路径?
最佳答案
我还必须将 moduleNameMapper
添加到我的 tsconfig 路径映射的 jest 配置中,以便让我的测试识别我设置的路径映射。像这样:
"moduleNameMapper": {
"^yourPath/(.*)": "<rootDir>\\yourPath\\$1"
}
希望这对下线的人有所帮助!
关于javascript - Jest : cannot find module required inside module to be tested (relative path),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385920/