我'm trying to test a React Components behavior using Enzyme' s describeWithDOM()
和 mount()
. 但是当组件导入jQuery时,我收到此错误:
错误:jQuery需要一个带文档的窗口
我知道Enzyme在引擎盖下使用了jsdom,我一直认为jsdom负责窗户和文件 . 但我似乎无法找到如何让这些工作在一起 .
测试代码如下所示:
import chai, {expect} from 'chai';
import Select from './Select';
import React, {createElement} from 'react';
import {describeWithDOM, mount} from 'enzyme';
describe('UI Select', () => {
//more shallow tests here
describeWithDOM('User Actions', () => {
it('Toggles the .ui-options menu on button click', () => {
const wrapper = mount(<Select {...baseProps} />);
expect(wrapper.state().open).to.not.be.ok;
wrapper.find('button').simulate('click');
expect(wrapper.state().open).to.be.ok;
});
});
}
在onClick方法按钮中调用jquery函数: $('#inputSelector').focus()
我怎样才能让Enzyme和jsdom在测试中使用jquery?
1 回答
describeWithDOM
已在当前版本的Enzyme中删除,而是建议在所有测试之前显式初始化global.document
和global.window
,如图所示here . 我不使用jQuery,但我认为这应该提供它正在寻找的"window with a document" .Enzyme自己的测试使用的初始化代码在withDom.js文件中可用:
他们使用Mocha的
--require
选项来确保它在任何测试之前执行:mocha --require withDom.js --compilers js:babel-core/register --recursive test/*.js --reporter dot