试图在 Jest 中模拟一个命名的 commonjs 模块
Trying to mock a named commonjs module in Jest
我已经搜索了几天如何完成此操作,但我尝试过的任何方法都没有奏效。我有一个与其他模块共享的 authenticate
模块,我想在我的单元测试中使用 jest 为它创建一个模拟。这是我目前拥有的:
authenticate.js
const got = require('got');
const authenticate = async (options) => {
let res = ...api request using got library to retrieve token...
return { data: res.access_token };
};
exports.authenticate = authenticate;
schedule.js
const schedule = async (options) {
// import authenticate library
const authenticate = require('./authenticate');
const login = await authenticate.authenticate(options);
const result;
...stuff to set result...
result = {
data: 'success'
};
return result;
};
exports.schedule = schedule;
shedule.test.js
const scheduler = require('./schedule');
test('[schedule]', async () => {
const options = {...};
jest.mock('./authenticate', () => ({
authenticate: jest.fn()
}));
const authenticate = require('./authenticate');
authenticate.authenticate.mockReturnValue({
data: mockOptions.access_token
});
const moduleUnderTest = await scheduler.schedule(options);
expect(moduleUnderTest).toEqual({ data: 'success' });
});
您不应该使用jest.mock(moduleName, factory, options) in function scope, it should be used in MODULE scope. Besides, for your case, I don't see any reason to use rewire模块。
例如
authenticate.js:
const got = require('got');
const authenticate = async (options) => {
let res = 'got';
return { data: res.access_token };
};
exports.authenticate = authenticate;
schedule.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;
schedule.test.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 Whosebug/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
我已经搜索了几天如何完成此操作,但我尝试过的任何方法都没有奏效。我有一个与其他模块共享的 authenticate
模块,我想在我的单元测试中使用 jest 为它创建一个模拟。这是我目前拥有的:
authenticate.js
const got = require('got');
const authenticate = async (options) => {
let res = ...api request using got library to retrieve token...
return { data: res.access_token };
};
exports.authenticate = authenticate;
schedule.js
const schedule = async (options) {
// import authenticate library
const authenticate = require('./authenticate');
const login = await authenticate.authenticate(options);
const result;
...stuff to set result...
result = {
data: 'success'
};
return result;
};
exports.schedule = schedule;
shedule.test.js
const scheduler = require('./schedule');
test('[schedule]', async () => {
const options = {...};
jest.mock('./authenticate', () => ({
authenticate: jest.fn()
}));
const authenticate = require('./authenticate');
authenticate.authenticate.mockReturnValue({
data: mockOptions.access_token
});
const moduleUnderTest = await scheduler.schedule(options);
expect(moduleUnderTest).toEqual({ data: 'success' });
});
您不应该使用jest.mock(moduleName, factory, options) in function scope, it should be used in MODULE scope. Besides, for your case, I don't see any reason to use rewire模块。
例如
authenticate.js:
const got = require('got');
const authenticate = async (options) => {
let res = 'got';
return { data: res.access_token };
};
exports.authenticate = authenticate;
schedule.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;
schedule.test.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 Whosebug/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