Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

alexjeffburke/unexpected-events

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

unexpected-events

This unexpected plugin enables unit testing of EventEmitters.

Check a single event

To check an event is produced the 'for the first event on' assertion is provided - you specify the channel and the assertion to use when doing the match:

var EventEmitter = require('events').EventEmitter;
var expect = require('unexpected').clone();

expect.installPlugin(require('unexpected-events'));

describe('single event', function () {
    it('should see the event', function () {
        var emitter = new EventEmitter();

        // issue the event once listeners have a chance to attach
        process.nextTick(function () {
            emitter.emit('foo', 'bar', 'baz');
        });

        expect(emitter, 'for the first event on', 'foo', 'to equal', ['bar', 'baz']);
    });
});

In this case you can see an event on the channel foo with the values foo and bar being sent.

Two special forms of the assertion are provided in case you need to ignore a certain number of messages before doing the comparisons:

  • check the second event (i.e. skip one event)

    expect(emitter, 'for the second event on', 'foo', 'to equal', ['something']);
  • check the third event (i.e. skip two events)

    expect(emitter, 'for the third event on', 'foo', 'to equal', ['something']);

Readability

For ease of readability you can also remove the array around the event values by brandishing the optional values flag in the assertion:

var EventEmitter = require('events').EventEmitter;
var expect = require('unexpected').clone();

describe('basic test with varargs', function () {
    it('should match an event', function () {
        var emitter = new EventEmitter();

        // issue the event once listeners have a chance to attach
        process.nextTick(function () {
            emitter.emit('foo', 'bar', 'baz');
        });

        expect(emitter, 'for the event values on', 'foo', 'to equal', 'bar', 'baz');
    });
});

You'll also notice, as in the example above, you can drop the word first when comparing a single event.

For your convenience you understand.

Check multiple events

If you want to check a series of messages on a channel, there is an assertion for you too!

var EventEmitter = require('events').EventEmitter;
var expect = require('unexpected').clone();

expect.installPlugin(require('unexpected-events'));

describe('multiple test', function () {
    it('should see them all', function () {
        var emitter = new EventEmitter();

        // issue the event once listeners have a chance to attach
        process.nextTick(function () {
            emitter.emit('foo', 'bar');
            emitter.emit('foo', 'baz');
        });

        expect(emitter, 'for multiple events on', 'foo', 'to equal', [
            { args: ['bar'] },
            { args: ['baz'] }
        ]);
    });
});

License

Licensed under a standard 3-clause BSD license -- see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published