JSDOM 和 jQuery 不能一起工作
JSDOM and jQuery not working together
我对 mocha 和 chai 等测试环境很陌生,我想按照在线教程进行练习,但它似乎不喜欢我所做的并给我一个错误提示:
global.document = _jsdom2.default.jsdom('<!doctype html><html><body></body></html>');
^
TypeError: _jsdom2.default.jsdom is not a function
我的test_helper
长得像,
import jsdom from 'jsdom';
import jquery from 'jquery';
import TestUtils from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
import chai, { expect } from 'chai';
import React from 'react';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from '../src/reducers';
import chaiJquery from 'chai-jquery';
// Set up testing environment to run like a browser in the command line
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = global.document.defaultView;
const $ = jquery(global.window);
// Build 'renderComponent' helper that should render a given react class
function renderComponent(ComponentClass, props, state) {
const componentInstance = TestUtils.renderIntoDocument(
<Provider store={createStore(reducers,state)}>
<ComponentClass {...props}/>
</Provider>
);
return $(ReactDOM.findDOMNode(componentInstance)); // produces HTML
}
// Build helper for simulating(or other) event
// Set up chai-jquery
chaiJquery(chai, chai.util, $);
export default { renderComponent, expect };
我想知道是否有人和我有类似的问题并且知道如何解决这个问题。由于我不太熟悉这个主题,所以我不确定如何调试它。
这是我实现 const { JSDOM } = jsdom;
和 new JSDOM(...)
后的错误堆栈
if ( !w.document ) {
^
TypeError: Cannot read property 'document' of undefined
at module.exports (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/jquery/dist/jquery.js:30:12)
at Object.<anonymous> (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/test/test_helper.js:17:11)
at Module._compile (module.js:571:32)
at loader (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at /Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/mocha/bin/_mocha:355:3
at Array.forEach (native)
at Object.<anonymous> (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/mocha/bin/_mocha:354:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
at bootstrap_node.js:538:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! budee_order_tracking@1.0.0 test: `mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the budee_order_tracking@1.0.0 test script 'mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the budee_order_tracking package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs budee_order_tracking
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls budee_order_tracking
npm ERR! There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_200Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! budee_order_tracking@1.0.0 test:watch: `npm run test -- --watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the budee_order_tracking@1.0.0 test:watch script 'npm run test -- --watch'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the budee_order_tracking package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm run test -- --watch
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs budee_order_tracking
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls budee_order_tracking
npm ERR! There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_215Z-debug.log
首先应该是jsdom.JSDOM
。根据 documentation:
Basic usage
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
To use jsdom
, you will primarily use the JSDOM
constructor, which is a named export of the jsdom
main module.
其次,JSDOM
如上所述是一个构造函数,所以你必须使用new
关键字:
import jsdom from 'jsdom';
global.document = new jsdom.JSDOM(...);
我会使用命名导入来导入特定的命名导出以简化操作:
import { JSDOM } from 'jsdom';
global.document = new JSDOM(...);
最后,使用 window
代替新 API 中 不存在 的 defaultView
。它 returns undefined
这就是 jQuery 不起作用的原因:
const $ = jquery(global.document.window);
不同的语法:
import jsdom from "jsdom";
import chai from "chai";
//...
global.doc = new jsdom.JSDOM("...");
global.window = doc.window;
new
运算符在这里很重要,因为没有它就无法调用 JSDOM。
我对 mocha 和 chai 等测试环境很陌生,我想按照在线教程进行练习,但它似乎不喜欢我所做的并给我一个错误提示:
global.document = _jsdom2.default.jsdom('<!doctype html><html><body></body></html>');
^
TypeError: _jsdom2.default.jsdom is not a function
我的test_helper
长得像,
import jsdom from 'jsdom';
import jquery from 'jquery';
import TestUtils from 'react-dom/test-utils';
import ReactDOM from 'react-dom';
import chai, { expect } from 'chai';
import React from 'react';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from '../src/reducers';
import chaiJquery from 'chai-jquery';
// Set up testing environment to run like a browser in the command line
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>');
global.window = global.document.defaultView;
const $ = jquery(global.window);
// Build 'renderComponent' helper that should render a given react class
function renderComponent(ComponentClass, props, state) {
const componentInstance = TestUtils.renderIntoDocument(
<Provider store={createStore(reducers,state)}>
<ComponentClass {...props}/>
</Provider>
);
return $(ReactDOM.findDOMNode(componentInstance)); // produces HTML
}
// Build helper for simulating(or other) event
// Set up chai-jquery
chaiJquery(chai, chai.util, $);
export default { renderComponent, expect };
我想知道是否有人和我有类似的问题并且知道如何解决这个问题。由于我不太熟悉这个主题,所以我不确定如何调试它。
这是我实现 const { JSDOM } = jsdom;
和 new JSDOM(...)
if ( !w.document ) {
^
TypeError: Cannot read property 'document' of undefined
at module.exports (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/jquery/dist/jquery.js:30:12)
at Object.<anonymous> (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/test/test_helper.js:17:11)
at Module._compile (module.js:571:32)
at loader (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at /Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/mocha/bin/_mocha:355:3
at Array.forEach (native)
at Object.<anonymous> (/Users/cheewoonkim/Desktop/nodeapp/meanmapsapp/node_modules/mocha/bin/_mocha:354:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:423:7)
at startup (bootstrap_node.js:147:9)
at bootstrap_node.js:538:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! budee_order_tracking@1.0.0 test: `mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the budee_order_tracking@1.0.0 test script 'mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the budee_order_tracking package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs budee_order_tracking
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls budee_order_tracking
npm ERR! There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_200Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! budee_order_tracking@1.0.0 test:watch: `npm run test -- --watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the budee_order_tracking@1.0.0 test:watch script 'npm run test -- --watch'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the budee_order_tracking package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! npm run test -- --watch
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs budee_order_tracking
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls budee_order_tracking
npm ERR! There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_215Z-debug.log
首先应该是jsdom.JSDOM
。根据 documentation:
Basic usage
const jsdom = require("jsdom"); const { JSDOM } = jsdom;
To use
jsdom
, you will primarily use theJSDOM
constructor, which is a named export of thejsdom
main module.
其次,JSDOM
如上所述是一个构造函数,所以你必须使用new
关键字:
import jsdom from 'jsdom';
global.document = new jsdom.JSDOM(...);
我会使用命名导入来导入特定的命名导出以简化操作:
import { JSDOM } from 'jsdom';
global.document = new JSDOM(...);
最后,使用 window
代替新 API 中 不存在 的 defaultView
。它 returns undefined
这就是 jQuery 不起作用的原因:
const $ = jquery(global.document.window);
不同的语法:
import jsdom from "jsdom";
import chai from "chai";
//...
global.doc = new jsdom.JSDOM("...");
global.window = doc.window;
new
运算符在这里很重要,因为没有它就无法调用 JSDOM。