在:
const jsdom = require("jsdom");
const initialVirtualConsole = jsdom.createVirtualConsole();
我明白了:
TypeError: jsdom.createVirtualConsole is not a function
at Query.<anonymous> (/my_jsdom_script.js:112:55)
at emitNone (events.js:72:20)
at Query.emit (events.js:166:7)
at Query.Sequence.end (/Users/xxx/Sites/node_modules/mysql/lib/protocol/sequences/Sequence.js:99:12)
at Query._handleFinalResultPacket (/Users/xxx/Sites/node_modules/mysql/lib/protocol/sequences/Query.js:144:8)
at Query.EofPacket (/Users/xxx/Sites/node_modules/mysql/lib/protocol/sequences/Query.js:128:8)
at Protocol._parsePacket (/Users/xxx/Sites/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/xxx/Sites/node_modules/mysql/lib/protocol/Parser.js:73:12)
at Protocol.write (/Users/xxx/Sites/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/Users/xxx/Sites/node_modules/mysql/lib/Connection.js:96:28)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at Socket.Readable.push (_stream_readable.js:110:10)
at TCP.onread (net.js:523:20)
我想在本地机器上运行jsdom的测试,看看jsdom在我的本地环境中的表现。
在https://github.com/tmpvar/jsdom/tree/master/test中运行测试时,有没有人可以指导一个菜鸟?
答案 0 :(得分:1)
这是beforeEach()
块中的内容(我使用mocha over node.js进行测试)
import jsdom from 'jsdom'
const virtualConsole = jsdom.createVirtualConsole()
const document = jsdom.jsdom(undefined, {
virtualConsole: virtualConsole.sendTo(console)
})
virtualConsole.on('log', (...args) => {
// do something with `args` sent to logs
// e.g, assign result to a variable and run your assertions over it
})
window = document.defaultView
global.window = window
global.document = document
查看测试以获取更多示例:https://github.com/tmpvar/jsdom/blob/master/test/jsdom/virtual-console.js