13

我有unhandledRejections 和uncaughtExceptions 的处理程序:

bin.js

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

现在我想用以下方法测试它们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只是告诉我测试中有错误:

● 捕获未处理的拒绝

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)

有没有办法测试这个?

这可能是相关的:https ://github.com/facebook/jest/issues/5620

4

2 回答 2

2

我的测试策略是使用jest.spyOn(object, methodName)安装 spyprocess.on()logger.error方法。这样做之后,这些方法就没有副作用了。然后,您可以在隔离环境中测试您的代码逻辑。

除此之外,还有几点需要注意:

  • require('./bin')您应该在声明之前监视函数。因为当你加载bin.js模块时,代码会被执行。
  • 您应该在挂钩中使用jest.resetModules()beforeEach来重置模块注册表 - 所有必需模块的缓存。为什么?因为require()缓存了它的结果。因此,第一次需要一个模块时,它的初始化代码就会运行。之后,缓存只返回值,module.exports无需再次运行初始化代码。但是我们有两个测试用例,我们希望模块范围内的代码执行两次。

现在,这里是示例:

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

于 2020-12-21T04:55:38.690 回答
-2

把它放在 try catch 中会有所帮助:

const error = new Error('模拟错误');

尝试 {

await Promise.reject(error);

} 捕捉(错误){

   expect(logger.error).toHaveBeenCalledWith(error);

}

于 2020-02-07T17:28:36.777 回答