javascript - how to use jsdom to test functions with 'document' -


i have small question.. trying test functions created (written in typescript), , using mocha/chai/jsdom. now, error while testing functions 'document' inside document.. message 'referenceerror: document not defined'. how can still test these functions 'document' in it?

for example:

[prompt.spec.ts]

import { expect } 'chai' import { jsdom } 'jsdom' import { functionx } './functions'  describe('functions', () => {   it('is possible execute functionx 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') htmldivelement  //  referenceerror: document not defined  } 

you can make jsdom's document available tests globally if prepare in advance.

import { jsdom } 'jsdom'; const { window } = new jsdom('<!doctype html><html><body></body></html>');  // save these 2 objects in global space libraries/tests // can hook them, using above doc definition. global.document = window.document; global.window = window; 

write separate file , add file parameter mocha right before spec files. like:

_mocha specs/_setup.js specs/*.js 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -