I want to assert that emit from the EventEmitter class was called with specific parameters by using Jest. I have a separate file where I create an instance of eventEmitter to be used, and on the other class I import it and at some point the event is emitted.
// commonEmitter.ts
const events = require('events');
export const commonEmitter = new events.EventEmitter();
// class.ts
import { commonEmitter } from (..)
export class MyClass {
(...)
method(){
commonEmitter.emit('eventName', { data: true});
}
}
// class.spec.ts
let commonEmitterMock: any
beforeEach(() => {
commonEmitterMock = createMock('emit');
});
it('testMyClass', async () => {
const method = new MyClass().method();
expect(commonEmitterMock).toHaveBeenCalledWith('eventName', { data: true})
}
With this implementation the emit event is never called.. Cant figure out why, any idea?
To test different branches of your http request events without giving up on a over-engineered code you can do the follow.
This is a stub version of the function I intend to test using Jest:
function myRequest(resolve, reject) {
http.request(url, options, (res: IncomingMessage) => {
response.on('data', (chunk) => {
// On data event code
})
response.on('end', () => {
// On end event code
resolve()
})
response.on('error', (err) => {
reject(err)
})
}
}
Firstly, we need to mock the http library an overwrite the request implementation to manually trigger the callback and inject our mocked response object:
...
const mockRes = {
write: jest.fn(),
on: jest.fn(),
end: jest.fn()
}
jest.mock('http', () => ({
request: jest.fn().mockImplementation((url, options, cb) => {
cb(mockRes)
})
})
Then, each of our jest test unit, we manually trigger the callbacks on each of the events we desire to test passing data to each of the specific callbacks:
it('should call request callback and reject for invalid content response', async () => {
const resolve = jest.fn()
const reject = jest.fn()
mockRes.on.mockImplementation((event, cb) => {
if (event === 'end') {
cb()
} else if (event === 'data') {
cb(new Error('invalid_json_string'))
}
})
// @ts-ignore
myRequest(resolve, reject)
// @ts-ignore
expect(mockRes.on).toHaveBeenCalledWith('data', expect.any(Function))
expect(mockRes.on).toHaveBeenCalledWith('end', expect.any(Function))
expect(reject).toHaveBeenCalledWith(expect.any(Error))
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With