Поскольку вы создаете сервер в области видимости модуля, код будет выполняться немедленно, когда вы require
или import
server.js
. Вам нужно поставить заглушку http.createServer
, прежде чем вам понадобится этот модуль.
Для тестирования метода .on(error, callback)
вы должны использовать mockImplementation
или mockImplementationOnce
, поэтому, когда смоделированный сервер вызывает смоделированный .on('error', callback)
, вы получите исходный обратный вызов в вашем тестовом случае. Что означает handler
эквивалентно callback
. Когда вы вызываете handler(mError)
, объект ложной ошибки будет передан в исходный callback
. Затем вы можете использовать это mError
проверить ваш код logi c.
Вот решение для модульного теста:
server.js
:
const http = require('http');
const server = http.createServer();
server.listen(8080, '127.0.0.1').on('error', (err) => {
console.log(err);
});
module.exports = server;
server.test.js
:
const http = require('http');
describe('60435647', () => {
it('should handle error', () => {
const mError = new Error('network');
const mServer = {
listen: jest.fn().mockReturnThis(),
on: jest.fn().mockImplementationOnce((event, handler) => {
// handler is the original callback, the mError variable will be passed into the original callback.
handler(mError);
}),
};
const createServerSpy = jest.spyOn(http, 'createServer').mockImplementationOnce(() => mServer);
const logSpy = jest.spyOn(console, 'log');
require('./server');
expect(createServerSpy).toBeCalledTimes(1);
expect(mServer.listen).toBeCalledWith(8080, '127.0.0.1');
expect(mServer.on).toBeCalledWith('error', expect.any(Function));
expect(logSpy).toBeCalledWith(mError);
});
});
Результаты модульных испытаний со 100% покрытием:
PASS stackoverflow/60435647/server.test.js
60435647
✓ should handle error (459ms)
console.log node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866
Error: network
at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/60435647/server.test.js:5:20)
at Object.asyncJestTest (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
at resolve (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
at new Promise (<anonymous>)
at mapper (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:26:19)
at promise.then (/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/node_modules/jest-jasmine2/build/queueRunner.js:73:41)
at process._tickCallback (internal/process/next_tick.js:68:7)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
server.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.772s, estimated 6s