【发布时间】:2019-07-17 15:13:13
【问题描述】:
我正在对 React 中的一些代码创建一个单元测试。但是有些操作似乎不起作用。
假设我在开始时有一个 testing.js:
const images = require.context('../../../media/promotions', true);
这是我正在尝试的单元测试( testing.test.js ):
import React from 'react';
import { shallow } from 'enzyme';
import { Testing } from '../../src/components/Testing';
let wrapper, instance;
beforeAll(() => {
wrapper = shallow(
<Testing />
);
instance = wrapper.instance();
});
describe('Testing', () => {
it('renders without crashing', () => {
expect(wrapper).toMatchSnapshot();
});
})
但是应用程序崩溃了,这是我得到的错误:
TypeError: require.context is not a function
【问题讨论】:
-
require.context是一个 webpack 功能,在不使用 webpack 的 Jest 测试中不可用 -
@PatrickHund 我正在寻找 React Js 的答案,希望能得到更新;我已经看了一下那个问题,但我没有找到任何有用的答案
-
我觉得跟 React 没什么关系,是 webpack 和 Jest 的问题。 React 只是一个用于渲染用户界面的库,它与模块加载无关。
标签: javascript reactjs unit-testing testing frontend