[英]How can I run a unit test in a browser environment?
I am writing a test for some code that manipulates the DOM, and I wanted to run it in a browser environment and I don't know how.我正在为一些操作 DOM 的代码编写测试,我想在浏览器环境中运行它,但我不知道如何。
I wrote a mockup test using the testing library AVA , for my small library promisify-dom-selector :我使用测试库AVA为我的小型库promisify-dom-selector编写了一个模型测试:
const test = require('ava')
const promisifyDOMSelector = require('index.js')
test('pGetElementById works', t => {
const pGetElementById = promisifyDOMSelector(document.getElementById)
pGetElementById('load-later')
.then((el) => {
t.deepEqual(el, document.getElementById('load-later'))
})
const testEl = document.createElement('div')
testEl.id = 'load-later'
document.body.appendChild(testEl)
})
and wanted to run it in Headless Chrome .并想在Headless Chrome中运行它。
I've tried setting up Karma with the ava plugin karma-ava but it doesn't even work, here is the error message:我尝试使用 ava 插件karma-ava设置 Karma,但它甚至不起作用,这是错误消息:
I wouldn't go with the Karma route if possible, but I don't know of any other way!如果可能的话,我不会选择 Karma 路线,但我不知道还有其他方法! What can I do?我能做些什么?
If you don't need to actually test in a browser then the browser testing recipe includes some helpful pointers.如果您不需要在浏览器中进行实际测试,那么浏览器测试秘诀包括一些有用的提示。
https://github.com/avajs/karma-ava hasn't seen any work in over a year, so I'm not surprised if that doesn't work. https://github.com/avajs/karma-ava一年多没有看到任何工作,所以如果这不起作用,我并不感到惊讶。
If you do need to run your code in an actual browser I'd use http://www.nightmarejs.org/ or something with an HTML document you can control from AVA in order to test your library.如果您确实需要在实际浏览器中运行您的代码,我会使用http://www.nightmarejs.org/或带有 HTML 文档的东西,您可以从 AVA 控制以测试您的库。
Cypress has recently released Cypress Component Testing .赛普拉斯最近发布了赛普拉斯组件测试。 This supports major frameworks like React and Vue.这支持 React 和 Vue 等主要框架。 The tests will run in headless browser instead of jsdom in node env.测试将在无头浏览器而不是节点环境中的 jsdom 中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.