如何用玩笑测试unhandledRejection/uncaughtException处理程序 [英] How to test a unhandledRejection / uncaughtException handler with jest

查看:151
本文介绍了如何用玩笑测试unhandledRejection/uncaughtException处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有unhandledRejection s和uncaughtException s的处理程序:

I have handlers for unhandledRejections and uncaughtExceptions:

bin.js

['unhandledRejection', 'uncaughtException'].forEach(event => {
  process.on(event, err => logger.error(err));
});

现在我想用jest对其进行测试:

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);
});

但是jest只是告诉我测试中存在错误:

But jest just tells me that there are errors in the tests:

●捕获未处理的拒绝

● catches unhandled rejections

mock error

   8 | // https://github.com/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)

●捕获未捕获的异常

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?

这可能与以下内容有关: https://github.com/facebook/jest/issues/5620

This might be related: https://github.com/facebook/jest/issues/5620

推荐答案

我的测试策略是使用

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 the bin.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? because require() caches its results. So, the first time a module is required, then its initialization code runs. After that, the cache just returns the value of module.exports without running the initialization code again. But we have two test cases, we want the code in module scope to be executed twice.

现在,这是示例:

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);
  });
});

单元测试结果:

 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

源代码: https://github.com/mrdulin/jest-v26-codelab/tree/main/examples/52493145

这篇关于如何用玩笑测试unhandledRejection/uncaughtException处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆