简体   繁体   English

如何使用vanillajs和nodejs在Mocha / Chai / Sinon中测试addEventListener?

[英]How to test addEventListener in Mocha/Chai/Sinon with vanillajs and nodejs?

I have an issue with Mocha testing an eventlistener that checks for input. 我在Mocha测试用于检查输入的事件监听器时遇到问题。 My environment is with plain javascript, nodejs and mocha/chai/sinon for testing. 我的环境是使用普通的javascript,nodejs和mocha / chai / sinon进行测试。 I wrote some other tests that are working, basically checking functions but the test hits on "Cannot read property 'addEventListener' of null". 我编写了一些其他有效的测试,基本上检查了功能,但是测试命中“无法读取null的属性'addEventListener'”。

Since I'm quite new to testing I've dug around in the documentation, added JSDOM and such but no luck. 由于我对测试还很陌生,因此我在文档中进行了研究,因此添加了JSDOM等等,但是没有运气。 I simply don't know how to check for the eventlistener. 我根本不知道如何检查事件监听器。 Many of the threads already available are React environments which seem to have some sort of built in applications that helps them, but not me. React环境已经提供了许多可用的线程,其中似乎有某种内置的应用程序可以帮助他们,但我却没有。

So it's basically this, really narrowed down: 所以基本上是这样,真正缩小了范围:

const userInput = document.querySelector("#userInfo");
userInput.addEventListener("input", changeContent);

It checks for user input and in the changeContent method it just checks for length etc. Any takers on how I should do this? 它检查用户输入,并在changeContent方法中检查长度等。是否有人应如何做?

Edit: Slight workaround that "skips" my issue, but it would be nice to learn how to solve it in a test anyway. 编辑:可以“跳过”我的问题的小解决方法,但是无论如何要在测试中学习如何解决它会很好。 With this the test passes (because it ignores it). 测试通过(因为它忽略了它)。

Workaround, since the test points eventListener to null: 解决方法,因为测试将eventListener指向null:

if(userInput){
  userInput.addEventListener("input", changeContent)
}

You can use a spy to test if addEventListener is getting called. 您可以使用spy来测试是否调用了addEventListener There is no specific reason to test the implementation of addEventListener . 没有特定的理由来测试addEventListener的实现。

Also you have to fake the document.querySelector and force it to return an HTMLElement. 另外,您还必须伪造document.querySelector并强制其返回HTMLElement。

Long story short: 长话短说:

Use spy for addEventListener : https://sinonjs.org/releases/v7.3.2/spies/ 将间谍用于addEventListenerhttps : //sinonjs.org/releases/v7.3.2/spies/

Use fakes for document.querySelector : https://sinonjs.org/releases/v7.3.2/ document.querySelector使用伪造品: https : //sinonjs.org/releases/v7.3.2/

Example of spy for console.log : 监视 console.log 示例

// method implementend in your project
function log() {
   console.log('Hello world');

}

// log.spec.ts

...
describe('whatever test suite', () => {
    it('test log', () => {
        const consoleSpy = Sinon.spy(console, 'log');
        log();

        Sinon.assert.calledWith(consoleSpy, 'Hello world');
    });
});
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 mocha、chai 和 sinon 模拟和测试闭包 - How to mock, and test, closures with mocha, chai and sinon 如何使用Mocha,Chai和Sinon正确测试Express控制器方法 - How to properly test an Express controller method with Mocha, Chai, and Sinon 如何使用 mocha/chai 测试 ejs/nodejs function? - How to test ejs/nodejs function with mocha/chai? 如何使用 Mocha & Sinon 测试 document.addEventListener('keydown', cb)? - How to test document.addEventListener('keydown', cb) with Mocha & Sinon? 模拟和测试Mocha,Chai和Sinon的按键事件? - Simulate and test a keypress event with Mocha, Chai, Sinon? 如何在 Chai 中模拟或存根 'instanceof' | 诗浓 | 摩卡 - How to mock or stub 'instanceof' in Chai | Sinon | Mocha 如何对路由器功能NodeJS / Mocha / Chai进行单元测试 - How to carry unit test on router function NodeJS / Mocha / Chai 如果变量具有反应组件作为值,我如何使用 mocha/chai/sinon 进行单元测试? - How do i unit test using mocha/chai/sinon if a variable has a react component as value? 如何使用 mocha/sinon/chai 正确测试异步 stream 错误事件处理? - How do I correctly test asynchronous stream error event handling with mocha/sinon/chai? 如何测试使用Mocha,Sinn和Chai返回的函数? - How do I test a function which returns a function using Mocha, Sinon, and Chai?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM