admin管理员组文章数量:1403455
I have handlers for unhandledRejection
s and uncaughtException
s:
bin.js
['unhandledRejection', 'uncaughtException'].forEach(event => {
process.on(event, err => logger.error(err));
});
Now I want to test them with jest
:
bin.test.js
const bin = require('../bin');
test('catches unhandled rejections', async () => {
const error = new Error('mock error');
await Promise.reject(error);
expect(logger.error).toHaveBeenCalledWith(error);
});
test('catches uncaught exceptions', () => {
const error = new Error('mock error');
throw error;
expect(logger.error).toHaveBeenCalledWith(error);
});
But jest
just tells me that there are errors in the tests:
● catches unhandled rejections
mock error 8 | // 9 | test('catches unhandled rejections', async () => { > 10 | const error = new Error('mock error'); | ^ 11 | await Promise.reject(error); 12 | expect(logger.error).toHaveBeenCalledWith(error); 13 | }); at Object.<anonymous>.test (test/bin.test.js:10:17)
● catches uncaught exceptions
mock error 14 | 15 | test('catches uncaught exceptions', () => { > 16 | const error = new Error('mock error'); | ^ 17 | throw error; 18 | expect(logger.error).toHaveBeenCalledWith(error); 19 | }); at Object.<anonymous>.test (test/bin.test.js:16:17)
is there a way to test this?
This might be related:
I have handlers for unhandledRejection
s and uncaughtException
s:
bin.js
['unhandledRejection', 'uncaughtException'].forEach(event => {
process.on(event, err => logger.error(err));
});
Now I want to test them with jest
:
bin.test.js
const bin = require('../bin');
test('catches unhandled rejections', async () => {
const error = new Error('mock error');
await Promise.reject(error);
expect(logger.error).toHaveBeenCalledWith(error);
});
test('catches uncaught exceptions', () => {
const error = new Error('mock error');
throw error;
expect(logger.error).toHaveBeenCalledWith(error);
});
But jest
just tells me that there are errors in the tests:
● catches unhandled rejections
mock error 8 | // https://github./facebook/jest/issues/5620 9 | test('catches unhandled rejections', async () => { > 10 | const error = new Error('mock error'); | ^ 11 | await Promise.reject(error); 12 | expect(logger.error).toHaveBeenCalledWith(error); 13 | }); at Object.<anonymous>.test (test/bin.test.js:10:17)
● catches uncaught exceptions
mock error 14 | 15 | test('catches uncaught exceptions', () => { > 16 | const error = new Error('mock error'); | ^ 17 | throw error; 18 | expect(logger.error).toHaveBeenCalledWith(error); 19 | }); at Object.<anonymous>.test (test/bin.test.js:16:17)
is there a way to test this?
This might be related: https://github./facebook/jest/issues/5620
Share Improve this question edited Dec 21, 2020 at 5:00 Lin Du 103k136 gold badges334 silver badges566 bronze badges asked Sep 25, 2018 at 7:48 LukasLukas 10.4k17 gold badges82 silver badges128 bronze badges 2- I think this is doable but I need to know how logger gets built – jcollum Commented Sep 27, 2018 at 21:31
-
1
@jcollum like this
const logger = winston.createLogger({/* ...some options */});
– Lukas Commented Oct 2, 2018 at 13:21
2 Answers
Reset to default 5My test strategy is to install spy onto process.on()
and logger.error
methods using jest.spyOn(object, methodName). After doing this, these methods have no side effects. Then, you can test your code logic in an isolated environment.
Besides, there are a few things to note:
- You should spy the functions before
require('./bin')
statement. Because when you load thebin.js
module, the code will be executed. - You should use jest.resetModules() in the
beforeEach
hook to resets the module registry - the cache of all required modules. Why? becauserequire()
caches its results. So, the first time a module is required, then its initialization code runs. After that, the cache just returns the value ofmodule.exports
without running the initialization code again. But we have two test cases, we want the code in module scope to be executed twice.
Now, here is the example:
bin.js
:
const logger = require('./logger');
['unhandledRejection', 'uncaughtException'].forEach((event) => {
process.on(event, (err) => logger.error(err));
});
logger.js
:
const logger = console;
module.exports = logger;
bin.test.js
:
const logger = require('./logger');
describe('52493145', () => {
beforeEach(() => {
jest.resetModules();
});
afterEach(() => {
jest.restoreAllMocks();
});
test('catches unhandled rejections', () => {
const error = new Error('mock error');
jest.spyOn(process, 'on').mockImplementation((event, handler) => {
if (event === 'unhandledRejection') {
handler(error);
}
});
jest.spyOn(logger, 'error').mockReturnValueOnce();
require('./bin');
expect(process.on).toBeCalledWith('unhandledRejection', expect.any(Function));
expect(logger.error).toHaveBeenCalledWith(error);
});
test('catches uncaught exceptions', () => {
const error = new Error('mock error');
jest.spyOn(process, 'on').mockImplementation((event, handler) => {
if (event === 'uncaughtException') {
handler(error);
}
});
jest.spyOn(logger, 'error').mockReturnValueOnce();
require('./bin');
expect(process.on).toBeCalledWith('uncaughtException', expect.any(Function));
expect(logger.error).toHaveBeenCalledWith(error);
});
});
unit test result:
PASS examples/52493145/bin.test.js
52493145
✓ catches unhandled rejections (5 ms)
✓ catches uncaught exceptions (1 ms)
-----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
bin.js | 100 | 100 | 100 | 100 |
logger.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 2.73 s, estimated 4 s
source code: https://github./mrdulin/jest-v26-codelab/tree/main/examples/52493145
putting it inside try catch will help:
const error = new Error('mock error');
try {
await Promise.reject(error);
} catch(error){
expect(logger.error).toHaveBeenCalledWith(error);
}
本文标签: javascriptHow to test a unhandledRejectionuncaughtException handler with jestStack Overflow
版权声明:本文标题:javascript - How to test a unhandledRejectionuncaughtException handler with jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744387702a2603825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论