I am trying to test a simple page having form with 2 input fields using ReactJs
and Mocha
.
When I run the test I am getting the error:
TypeError: undefined is not a function
at module.exports (/Users/johnp/Documents/myapp/work/projects/myapp-etd-portal/test/setup.js:5:35)
at Object.<anonymous> (/Users/johnp/Documents/myapp/work/projects/myapp-etd-portal/test/component/NameTest.js:1:28)
at Module._compile (module.js:460:26)
at normalLoader (/Users/johnp/Documents/myapp/work/projects/myapp-etd-portal/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:160:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/johnp/Documents/myapp/work/projects/myapp-etd-portal/node_modules/babel/node_modules/babel-core/lib/babel/api/register/node.js:173:7)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at /Users/johnp/Documents/myapp/work/projects/myapp-etd-portal/node_modules/mocha/lib/mocha.js:192:27
Below are my code files:
1.setup.js: basic set-up file
module.exports = function(markup) {
if (typeof document !== 'undefined') return
var jsdom = require("jsdom").jsdom
global.document = jsdom(markup || '')
global.window = document.createWindow()
var chai = require('chai');
chai.config.includeStack = true;
global.expect = chai.expect;
}
NameTest.js: actual test page.
NameTest.js
require('../../test/setup')('<!doctype html><html><body></body></html>')
var React = require('react')
var ReactAddons = require('react/addons')
var TestUtils = React.addons.TestUtils
describe('Name page component', function(){
before('render element', function() {
try {
var renderedComponent = TestUtils.renderToString(
<InputFieldItem />
);
} catch (e) {
throw new Error(e);
}
});
it('First & Last <input> fields should be of type "text"', function() {
this.formElement = React.findDOMNode(TestUtils.findRenderedDOMComponentWithTag(renderedComponent, 'form'))
this.firstNameElement = React.findDOMNode(TestUtils.scryRenderedDOMComponentsWithTag(renderedComponent, 'input')[0])
this.lastNameElement = React.findDOMNode(TestUtils.scryRenderedDOMComponentsWithTag(renderedComponent, 'input')[1])
});
});
I have my gulp task set up and I run the test using: gulp bundle && npm run test. It has been very frustrating to look for a solution and appreciate any help to resolve this issue.
As the stack trace shows, undefined is not a function
at setup.js:5:35
(line 5, column 35).
global.document = jsdom(markup || '')
global.window = document.createWindow() // <-- refers to this
Assigning something to node's global
object by doing global.document = ...
, is not the same as creating a document
variable in the global scope. To access something on node's global object you will always access it as a property, so do this:
global.window = global.document.createWindow();
开关document.createWindow()
的document.defaultView
按照该文档 ,它会工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.