Вот решение для модульного тестирования:
asyncHandlerUtil.js
:
const asyncHandlerUtil = (fn) => (req, res, next) => {
return Promise.resolve(fn(req, res, next)).catch(next);
};
module.exports = asyncHandlerUtil;
asyncHandlerUtil.test.js
:
const asyncHandlerUtil = require('./asyncHandlerUtil');
describe('62063369', () => {
it('should pass', async () => {
const mFn = jest.fn().mockResolvedValueOnce('mocked value');
const mReq = {};
const mRes = {};
const mNext = jest.fn();
const got = await asyncHandlerUtil(mFn)(mReq, mRes, mNext);
expect(got).toBe('mocked value');
expect(mFn).toBeCalledWith({}, {}, mNext);
});
});
Результаты модульного теста с отчетом о покрытии:
PASS stackoverflow/62063369/asyncHandlerUtil.test.js (12.14s)
62063369
✓ should pass (5ms)
---------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
asyncHandlerUtil.js | 100 | 100 | 100 | 100 |
---------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.218s