Вы НЕ ДОЛЖНЫ использовать jest.mock (moduleName, factory, options) в области действия функции, его следует использовать в области MODULE . Кроме того, для вашего случая я не вижу причин использовать модуль rewire .
Например
аутентифицировать. js:
const got = require('got');
const authenticate = async (options) => {
let res = 'got';
return { data: res.access_token };
};
exports.authenticate = authenticate;
расписание. js:
const schedule = async (options) => {
const authenticate = require('./authenticate');
const login = await authenticate.authenticate(options);
const result;
result = {
data: 'success'
};
return result;
};
exports.schedule = schedule;
расписание. Тест. js:
const scheduler = require('./schedule');
jest.mock('./authenticate', () => ({
authenticate: jest.fn(),
}));
describe('62074254', () => {
test('[schedule]', async () => {
const authenticate = require('./authenticate');
const mockOptions = { access_token: '123' };
authenticate.authenticate.mockReturnValue({
data: mockOptions.access_token,
});
const options = {};
const moduleUnderTest = await scheduler.schedule(options);
expect(moduleUnderTest).toEqual({ data: 'success' });
expect(authenticate.authenticate).toBeCalledWith({});
});
});
Результаты модульного тестирования с отчетом о покрытии:
PASS stackoverflow/62074254/schedule.test.js (10.742s)
62074254
✓ [schedule] (6ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
schedule.js | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 12.202s