单元测试 – 使用Jest测试Webpack构建的React组件
我遇到了一个问题,我需要在由Webpack构建的React应用程序上运行Jest测试.问题是处理Webpack通常用加载器处理的CSS文件和图像等的要求.我需要知道正确测试组件的最佳方法是什么.
React组件: import React from 'react'; // The CSS file is the problem as I want to actually test what it // returns after webpack has built it. import style from './boilerplate.css'; var Boilerplate = React.createClass({ render: function() { return ( <div> <h1 className={style.title}>react-boilerplate</h1> <p className={style.description}> A React and Webpack boilerplate. </p> </div> ); } }); export default Boilerplate; 开玩笑测试: jest.dontMock('./boilerplate.js'); var Boilerplate = require('./boilerplate.js'); var React = require('react/addons'); var TestUtils = React.addons.TestUtils; describe('boilerplate',function() { var component; beforeEach(function() { component = TestUtils.renderIntoDocument( <Boilerplate /> ); }); it('has a h1 title',function() { // I want to actually use 'findRenderedDOMComponentWithClass' // but cannot because I need to run the Webpack build to add a // CSS class to this element. var title = TestUtils.findRenderedDOMComponentWithTag(component,'h1'); expect(title.getDOMNode().textContent).toEqual('react-boilerplate'); }); it('has a paragraph with descriptive text',function() { var paragraph = TestUtils.findRenderedDOMComponentWithTag(component,'p'); expect(paragraph.getDOMNode().textContent).toEqual('A React and Webpack boilerplate.'); }); }); 我遇到了this question,这让我放心,我自己尝试了所有这些方法,但我遇到了所有解决方案的问题: 解决方案1: 问题:我想测试Webpack构建创建的一些功能.例如,我使用本地的,可互操作的CSS,我想测试从Webpack创建的require(‘whaterver.css’)返回的CSS类的Object.我还想使用React / TestUtils中的findRenderedDOMComponentWithClass,这意味着我需要通过Webpack构建CSS. 解决方案2: 问题:我们不能再使用Jest的自动模拟,因为我们现在使用Webpacks __webpack_require __(1).每次运行测试文件时,这也很慢. 解决方案3: 问题:与解决方案2相同.没有自动模拟. 我在这里走错路还是有人对此有任何答案?我错过了明显的吗?
我最近构建了
Jestpack,它将Jest与Webpack集成在一起意味着你可以使用所有Webpack的功能,包括CSS模块,文件加载,代码分割,CommonJS / AMD / ES2015导入等以及Jest的自动模拟.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |