I have a global object which has the ability to assign functions for events, like:
obj.on('event', () => {});
After exact public API is called these events are fired as well.
Now I need to write async tests with mocha.js/chai.js and run it in the node.js environment.
I stuck in a situation when two events subscriptions should be tested at once.
All code is written in TypeScript and later transpiled into JavaScript.
The code in a global object:
public someEvent(val1: string, val2: Object) {
// some stuff here...
this.emit('event_one', val1);
this.emit('event_two', val1, val2);
}
The code in a test file (my latest realization):
// prerequisites are here...
describe('test some public API', () => {
it('should receive a string and an object', (done) => {
// counting number of succesfull calls
let steps = 0;
// function which will finish the test
const finish = () => {
if ((++steps) === 2) {
done();
}
};
// mock values
const testObj = {
val: 'test value'
};
const testStr = 'test string';
// add test handlers
obj.on('event_one', (key) => {
assert.equal(typeof key, 'string');
finish();
});
obj.on('event_two', (key, event) => {
assert.equal(typeof key, 'string');
expect(event).to.be.an.instanceOf(Object);
finish();
});
// fire the event
obj.someEvent(testStr, testObj);
});
});
So, my question is - is there any built-in functionality to make this test to look more elegant?
Another question is how to provide some meaningful error information instead of default error string?
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.