How To Test A Unhandledrejection / Uncaughtexception Handler With Jest
I have handlers for unhandledRejections and uncaughtExceptions: bin.js ['unhandledRejection', 'uncaughtException'].forEach(event => { process.on(event, err => logger.error(
Solution 1:
My 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 = newError('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 = newError('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:
PASSexamples/52493145/bin.test.js52493145✓catchesunhandledrejections(5ms)✓catchesuncaughtexceptions(1ms)-----------|---------|----------|---------|---------|-------------------File|%Stmts|%Branch|%Funcs|%Lines|UncoveredLine#s -----------|---------|----------|---------|---------|-------------------Allfiles|100|100|100|100|
bin.js | 100 | 100 | 100 | 100 |
logger.js | 100 | 100 | 100 | 100 |
-----------|---------|----------|---------|---------|-------------------Test Suites:1passed,1totalTests:2passed,2totalSnapshots:0totalTime:2.73s,estimated4s
source code: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/52493145
Solution 2:
putting it inside try catch will help:
const error = new Error('mock error');
try {
awaitPromise.reject(error);
} catch(error){
expect(logger.error).toHaveBeenCalledWith(error);
}
Post a Comment for "How To Test A Unhandledrejection / Uncaughtexception Handler With Jest"