Jest:找不到要测试的模块内所需的模块(相对路径) [英] Jest: cannot find module required inside module to be tested (relative path)

查看:2094
本文介绍了Jest:找不到要测试的模块内所需的模块(相对路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个组件:

import React from 'react';
import VideoTag from './VideoTag';
import JWPlayer from './JWPlayer';

class VideoWrapper extends React.Component {
//... component code
}

基于某些逻辑呈现内部的另一个组件(VideoTag或JWPlayer),但当我尝试在jest文件中测试它时,我得到错误:

That based on some logic renders another component inside ( VideoTag or JWPlayer) but when I try to test it in a jest file i get the error:

无法找到模块'./VideoTag'

这三个组合在同一个目录中,这就是为什么它实际起作用的时候我转换它并在浏览器中看到它的运行但看起来Jest在解决这些相对路径时遇到问题,这是jest文件:

The three coponents are in the same directory that's why it actually works when I transpile it and see it in action in a browser but looks like Jest is having problems resolving those relative paths, this is the jest file:

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如何处理相对路径?

How do I tell jest how to handle relative paths?

推荐答案

问题不在于路径,它只是寻找扩展名为.js的模块,在jest配置中添加.jsx后它才起作用:

The problem were not the paths, It was looking for modules only with .js extension, it worked after adding the .jsx in the jest configuration:

"moduleFileExtensions": [
  "js",
  "jsx"
]

这篇关于Jest:找不到要测试的模块内所需的模块(相对路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆