admin管理员组文章数量:1302319
I'm pretty new to the test environment such as mocha and chai and I want to practice by following online tutorial but it seems that it does not like what I do and gives me an error saying:
global.document = _jsdom2.default.jsdom('<!doctype html><html><body></body></html>');
^
TypeError: _jsdom2.default.jsdom is not a function
My test_helper
looks like,
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 mand 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 ponentInstance = TestUtils.renderIntoDocument(
<Provider store={createStore(reducers,state)}>
<ComponentClass {...props}/>
</Provider>
);
return $(ReactDOM.findDOMNode(ponentInstance)); // produces HTML
}
// Build helper for simulating(or other) event
// Set up chai-jquery
chaiJquery(chai, chai.util, $);
export default { renderComponent, expect };
I was wondering that anyone has similar kind of issue like mine and know how to figure this out. Since I am not really familiar with this subject, I'm not sure how I can debug this.
Here is the error stack after I implemented const { JSDOM } = jsdom;
and 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._pile (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._pile (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! [email protected] test: `mocha --pilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script 'mocha --pilers 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 --pilers 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 plete 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! [email protected] test:watch: `npm run test -- --watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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 plete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_215Z-debug.log
I'm pretty new to the test environment such as mocha and chai and I want to practice by following online tutorial but it seems that it does not like what I do and gives me an error saying:
global.document = _jsdom2.default.jsdom('<!doctype html><html><body></body></html>');
^
TypeError: _jsdom2.default.jsdom is not a function
My test_helper
looks like,
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 mand 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 ponentInstance = TestUtils.renderIntoDocument(
<Provider store={createStore(reducers,state)}>
<ComponentClass {...props}/>
</Provider>
);
return $(ReactDOM.findDOMNode(ponentInstance)); // produces HTML
}
// Build helper for simulating(or other) event
// Set up chai-jquery
chaiJquery(chai, chai.util, $);
export default { renderComponent, expect };
I was wondering that anyone has similar kind of issue like mine and know how to figure this out. Since I am not really familiar with this subject, I'm not sure how I can debug this.
Here is the error stack after I implemented const { JSDOM } = jsdom;
and 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._pile (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._pile (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! [email protected] test: `mocha --pilers js:babel-core/register --require ./test/test_helper.js --recursive ./test "--watch"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script 'mocha --pilers 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 --pilers 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 plete 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! [email protected] test:watch: `npm run test -- --watch`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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 plete log of this run can be found in:
npm ERR! /Users/cheewoonkim/.npm/_logs/2017-05-16T01_20_20_215Z-debug.log
Share
edited Dec 17, 2017 at 5:22
Andrew Li
58k14 gold badges134 silver badges148 bronze badges
asked May 15, 2017 at 23:52
ckim16ckim16
1,7494 gold badges20 silver badges31 bronze badges
7
-
Should be
jsdom.JSDOM(...)
– Andrew Li Commented May 16, 2017 at 0:04 - @AndrewLi jsdome.JSDOM(...) does not work :( – ckim16 Commented May 16, 2017 at 0:06
- You have an extra e there – Andrew Li Commented May 16, 2017 at 0:06
- @AndrewLi Sorry that was my typo but it still doesn't work – ckim16 Commented May 16, 2017 at 0:07
- Has the error changed at all? – Andrew Li Commented May 16, 2017 at 0:08
2 Answers
Reset to default 10First of all, it should be jsdom.JSDOM
. Per the 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.
Second of all, JSDOM
is a constructor as noted above, so you must use the new
keyword:
import jsdom from 'jsdom';
global.document = new jsdom.JSDOM(...);
I would use named importing to import a specific named export to make this easier:
import { JSDOM } from 'jsdom';
global.document = new JSDOM(...);
Finally, use window
instead of defaultView
which doesn't exist in the new API. It returns undefined
and that's why jQuery won't work:
const $ = jquery(global.document.window);
A different syntax:
import jsdom from "jsdom";
import chai from "chai";
//...
global.doc = new jsdom.JSDOM("...");
global.window = doc.window;
The new
operator is important here since JSDOM cannot be invoked without it.
本文标签: javascriptJSDOM and jQuery not working togetherStack Overflow
版权声明:本文标题:javascript - JSDOM and jQuery not working together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741675619a2391864.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论