admin管理员组文章数量:1415644
I have a small question.. I am trying to test some functions I created (written in Typescript), and I am using mocha/chai/jsdom. Now, I get an error while testing functions with 'document' inside the document.. I get the message 'ReferenceError: document is not defined'. How can I still test these functions with 'document' in it?
For example:
[prompt.spec.ts]
import { expect } from 'chai'
import { JSDOM } from 'jsdom'
import { functionX } from './functions'
describe('Functions', () => {
it('is possible to execute functionX with simple parameters', () => {
const jsdom = new JSDOM()
const htmlElement = jsdom.window.document.createElement('div')
expect(functionX(htmlElement, function() { return true; } )).to.equal(true)
})
})
[functions.ts]
export const functionX = (
body:HTMLElement, callback: (ok: boolean) => void
) => {
const doc = body.ownerDocument
const parent = doc.body
// ...
let container = document.querySelector('.container') as HTMLDivElement // ReferenceError: document is not defined
}
I have a small question.. I am trying to test some functions I created (written in Typescript), and I am using mocha/chai/jsdom. Now, I get an error while testing functions with 'document' inside the document.. I get the message 'ReferenceError: document is not defined'. How can I still test these functions with 'document' in it?
For example:
[prompt.spec.ts]
import { expect } from 'chai'
import { JSDOM } from 'jsdom'
import { functionX } from './functions'
describe('Functions', () => {
it('is possible to execute functionX with simple parameters', () => {
const jsdom = new JSDOM()
const htmlElement = jsdom.window.document.createElement('div')
expect(functionX(htmlElement, function() { return true; } )).to.equal(true)
})
})
[functions.ts]
export const functionX = (
body:HTMLElement, callback: (ok: boolean) => void
) => {
const doc = body.ownerDocument
const parent = doc.body
// ...
let container = document.querySelector('.container') as HTMLDivElement // ReferenceError: document is not defined
}
Share
Improve this question
asked Jul 25, 2017 at 18:47
user1806756user1806756
1551 gold badge2 silver badges11 bronze badges
1
- Should it be doc.querySelector ? I see where you have defined doc from the body, but I don't see document defined anywhere, even in global. – veratti Commented Jul 25, 2017 at 19:10
1 Answer
Reset to default 6You can make the JSDOM's document available to your tests globally if you prepare it in advance.
import { JSDOM } from 'jsdom';
const { window } = new JSDOM('<!doctype html><html><body></body></html>');
// Save these two objects in the global space so that libraries/tests
// can hook into them, using the above doc definition.
global.document = window.document;
global.window = window;
Write this into a separate file and add that file as a parameter to mocha right before your spec files. Something like:
_mocha Specs/_setup.js Specs/*.js
本文标签: javascripthow to use jsdom to test functions with 39document39Stack Overflow
版权声明:本文标题:javascript - how to use jsdom to test functions with 'document' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745242609a2649411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论