Reputation: 1816
I am trying to write test scripts using mocha js for my react code. The test helper file is like this browser.js
var baseDOM = '<!DOCTYPE html><html><head><meta charset="utf-8"></head><body></body></html>';
var jsdom = require('jsdom').jsdom;
global.document = jsdom(baseDOM);
global.window = document.defaultView;
if ( global.self != null) {
console.log(' global.self >>>>> ' + global.self);
} else {
global.self = global.this;
}
global.navigator = {
userAgent: 'node.js'
};
The test script is like this (component.specs.js)
'use strict';
import ReactDOM from 'react-dom';
import React from 'react';
import { expect } from 'chai';
import { mount, shallow, render } from 'enzyme';
import { App } from '../../js/app.js';
import sinon from 'sinon';
var dispatch = function() {
`enter code here` console.log('>>>>>>>> Mocking dispatch ');
};
describe('App', function () {
it('App calls check', () => {
sinon.spy(App.prototype, 'check');
const enzymeWrapper = mount(<App {...props} />);
expect(App.prototype.check.calledOnce).to.equal(true);
});
So, when I run npm test, I get the error as 'jsdom is not a function'. What is wrong with the code?
Upvotes: 1
Views: 234
Reputation: 31761
I'm not familiar with jsdom but based on the examples I don't think you're using it correctly. jsdom's example code:
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
What this means for you is that var jsdom = require('jsdom').jsdom;
should be var jsdom = require('jsdom').JSDOM;
. The example also uses new
.
That said, you may want to check out jest for testing node/react - it's very easy to setup and works well.
Upvotes: 1