Reputation: 436
I'm trying to test the following method:
endSocketConn(socket) {
if(!socket) {
return;
}
socket.setTimeout(5000, () => {
socket.destroy();
});
socket.end('commandToSend');
}
The scenario I need to test is the one where if more than 5seconds are elapsed then socket.destroy() must be called one time
describe('Testing API | socketPool methods', async function () {
beforeEach(async function () {
let clock = sinon.useFakeTimers();
let socketMock = {
setTimeout: sinon.spy(),
destroy: sinon.spy(),
end: sinon.spy(),
};
});
afterEach(async function () {
clock && clock.restore();
});
describe('Testing endSocketConn() function', function () {
it('It should execute socket.destroy if timeout occurred after sending commandToSend', async function () {
let result;
let error = false;
try {
clock.tick(6000);
result = await file.endSocketConn(socketMock);
} catch (err) {
error = err;
}
expect(error).to.be.false;
sinon.assert.callCount(socketMock.setTimeout, 1);
sinon.assert.callCount(socketMock.end, 1);
sinon.assert.callCount(socketMock.destroy, 1);
// AssertError: expected spy to be called once but was called 0 times
});
});
Any idea why the timeout is not being properly simulated?
Upvotes: 0
Views: 1053
Reputation: 102277
You don't need to use sinon.useFakeTimers()
, because we can use the stub to implement a method that has nothing to do with the real setTimeout
.
E.g.
file.js
:
endSocketConn(socket) {
if (!socket) {
return;
}
socket.setTimeout(5000, () => {
socket.destroy();
});
socket.end('commandToSend');
},
};
export { file };
file.test.js
:
import sinon from 'sinon';
import { file } from './file';
describe('Testing API | EslSocketPoolHandler methods', function () {
let socketMock;
beforeEach(function () {
socketMock = {
setTimeout: sinon.stub(),
destroy: sinon.spy(),
end: sinon.spy(),
};
});
describe('Testing endSocketConn() function', function () {
it('It should execute socket.destroy if timeout occurred after sending commandToSend', function () {
socketMock.setTimeout.callsFake((ms, cb) => {
cb();
});
file.endSocketConn(socketMock);
sinon.assert.callCount(socketMock.setTimeout, 1);
sinon.assert.callCount(socketMock.end, 1);
sinon.assert.callCount(socketMock.destroy, 1);
});
});
});
unit test result:
Testing API | EslSocketPoolHandler methods
Testing endSocketConn() function
✓ It should execute socket.destroy if timeout occurred after sending commandToSend
1 passing (7ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 85.71 | 50 | 100 | 85.71 |
file.ts | 85.71 | 50 | 100 | 85.71 | 4
----------|---------|----------|---------|---------|-------------------
Upvotes: 1