Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jsdom使用';文件';_Javascript_Typescript_Mocha.js_Chai_Jsdom - Fatal编程技术网

Javascript 如何使用jsdom使用';文件';

Javascript 如何使用jsdom使用';文件';,javascript,typescript,mocha.js,chai,jsdom,Javascript,Typescript,Mocha.js,Chai,Jsdom,我有一个小问题。。我正在尝试测试我创建的一些函数(用Typescript编写),我正在使用mocha/chai/jsdom。现在,我在测试文档中包含“document”的函数时出错。。我收到消息“ReferenceError:文档未定义”。我如何使用“文档”测试这些函数 例如: [提示.规格] import { expect } from 'chai' import { JSDOM } from 'jsdom' import { functionX } from './functions' d

我有一个小问题。。我正在尝试测试我创建的一些函数(用Typescript编写),我正在使用mocha/chai/jsdom。现在,我在测试文档中包含“document”的函数时出错。。我收到消息“ReferenceError:文档未定义”。我如何使用“文档”测试这些函数

例如:

[提示.规格]

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)
  })
})
[功能.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

}

如果事先准备好JSDOM文档,则可以使其在全局测试中可用

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;

如果事先准备好JSDOM文档,则可以使其在全局测试中可用

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;

应该是doc.querySelector吗?我看到您从正文中定义文档的地方,但我看不到在任何地方定义的文档,即使是在全局中。它应该是doc.querySelector吗?我看到了您从主体中定义文档的地方,但我没有看到在任何地方定义文档,即使是在全局中。