最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unit testing event driven javascript - Stack Overflow

programmeradmin2浏览0评论

I'm writing a server-side application with node.js and would like to include unit tests. One thing I'm struggling with is a good way to unit test EventEmitter and setInterval/setTimout

What options do I have to unit test asynchronous server side javascript?

I'm aware I can just attach another listener to the EventEmitter that is the testing function but then how do I garantuee that the testing function ever runs? part of the unit test is to ensure every part of the unit test runs.

I could use setTimeout myself but that seems like a hackish solution.

If it helps here is some exampler code I'm trying to test.

...
function init(param) {
    ...
    // run update entities periodically
    setInterval(this._updateEntities.bind(this, param.containerFull),
        1000 / param.fps);
    ...
}
...
EntityUpdater.prototype = {
    ...
    "_updateEntities": function updateEntitiesfunc(fn) {
        this._tickEmitter.emit(
            "tick",
            new dataContainer.DataContainer(this.getEntityCount())
            .on(
                "full", fn.bind(this)
            )
        );
    },
    ...
}
...

(emit will trigger a event)

[Edit]

I started reading some of the EvevntEmitter tests at and it helps me see how to go about this.

I'm writing a server-side application with node.js and would like to include unit tests. One thing I'm struggling with is a good way to unit test EventEmitter and setInterval/setTimout

What options do I have to unit test asynchronous server side javascript?

I'm aware I can just attach another listener to the EventEmitter that is the testing function but then how do I garantuee that the testing function ever runs? part of the unit test is to ensure every part of the unit test runs.

I could use setTimeout myself but that seems like a hackish solution.

If it helps here is some exampler code I'm trying to test.

...
function init(param) {
    ...
    // run update entities periodically
    setInterval(this._updateEntities.bind(this, param.containerFull),
        1000 / param.fps);
    ...
}
...
EntityUpdater.prototype = {
    ...
    "_updateEntities": function updateEntitiesfunc(fn) {
        this._tickEmitter.emit(
            "tick",
            new dataContainer.DataContainer(this.getEntityCount())
            .on(
                "full", fn.bind(this)
            )
        );
    },
    ...
}
...

(emit will trigger a event)

[Edit]

I started reading some of the EvevntEmitter tests at https://github./ry/node/tree/master/test/simple and it helps me see how to go about this.

Share Improve this question edited Dec 10, 2010 at 22:03 Raynos asked Dec 10, 2010 at 20:54 RaynosRaynos 170k57 gold badges357 silver badges398 bronze badges 3
  • Have you taken a look at this blog post? debuggable./posts/… – Tmdean Commented Dec 10, 2010 at 21:25
  • Yes I read the blog, it is helpful but only covers a few points. I only just noticed the test wont actaully exit until the loop is finished. This still leaves an easy for one time setTimeout & one time events. – Raynos Commented Dec 10, 2010 at 21:45
  • I only now noticed the exit block is with respect to the test rather then the server. Its a way to run a block of code after all async actions finish – Raynos Commented Dec 10, 2010 at 21:56
Add a ment  | 

2 Answers 2

Reset to default 4

Personally what helped me the most was reading the tests for node.js themselves (I only read about half of them).

That gave me a good feeling of how to test the asynchronous code.

Thanks to @tmdean for pointing out another example of how to test async code.

I would remend you check out Jasmine for your tests. It's built to run tests outside a browser environment, and it stubs calls such as setTimeout, providing you with a fake clock with which you can move time forward at your leisure for testing anything time-related.

发布评论

评论列表(0)

  1. 暂无评论