【发布时间】:2017-07-25 18:47:22
【问题描述】:
我有一个小问题.. 我正在尝试测试我创建的一些函数(用 Typescript 编写),并且我正在使用 mocha/chai/jsdom。现在,在文档中使用“文档”测试函数时出现错误。我收到消息“参考错误:文档未定义”。我怎样才能在其中包含“文档”来测试这些功能?
例如:
[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)
})
})
[函数.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
}
【问题讨论】:
-
应该是 doc.querySelector 吗?我看到您在正文中定义 doc 的位置,但我没有看到任何地方定义的文档,即使在全局中也是如此。
标签: javascript typescript mocha.js chai jsdom