javascript - Jest : cannot find module required inside module to be tested (relative path)

标签 javascript node.js unit-testing reactjs jestjs

我有这个组件:

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/

相关文章:

node.js - 为 docker 镜像生成 .env 文件

java - 如何对调用 super 的重写方法进行单元测试?

c# - 创建和运行测试

javascript - Node开发的站点请求突发怎么办?

javascript - 用第二个函数废除第一个函数

javascript - 每次点击时移动图像 Javascript

node.js - 如何在 package.json 中为包提供自定义 url

javascript - 如何验证单选按钮和文本字段javascript

javascript - 利用 Mongo 的 ES6 默认参数

php - 如何对执行命令行程序的 PHP 类方法进行单元测试?