我使用simple-git
包。我有以下功能:
import simpleGit from 'simple-git';
/**
* The function returns the ticket Id, is presents, in the branch name
* @returns ticket Id
*/
export const getTicketIdFromBranchName = async (ticketRegex: RegExp) => {
const git = simpleGit();
try {
const localBranches = await git.branchLocal();
const currentBranch = localBranches.current;
const currentBranchTicketMatches = currentBranch.match(ticketRegex);
if (currentBranchTicketMatches) {
return currentBranchTicketMatches[0];
}
return null;
} catch {
return null;
}
};
我尝试为这个函数创建一个单元测试:
import { getTicketIdFromBranchName } from '@/utils/git-info';
const TICKET_ID_REGEX = /((?<!([A-Z]{1,10})-?)[A-Z]+-d+)/.source;
describe('[utils/git-info]', () => {
it('getTicketIdFromBranchName | Function should proper ticket Id when there is one', async () => {
const ticketId = 'CLO-1234';
jest.mock('simple-git', () => {
const mGit = {
branchLocal: jest.fn(() => Promise.resolve({ current: `${ticketId} DUMMY TEST` })),
};
return jest.fn(() => mGit);
});
const result = await getTicketIdFromBranchName(new RegExp(TICKET_ID_REGEX));
expect(result === ticketId).toEqual(true);
});
});
但是单元测试失败了。我说我希望得到true
但它在最后一行中得到了false
。
我想我以错误的方式使用jest.mock
。
官方文档对jest.mock
的使用进行了关键描述。
注意:为了正确模拟,Jest 需要
jest.mock('moduleName')
与require/import
语句处于同一范围内。
在测试用例函数作用域内调用jest.mock('moduleName')
,但在模块作用域中import
git-info
模块。这就是为什么模拟不起作用的原因。
在测试用例函数中使用require('moduleName')
或await import('moduleName')
。require/import
和jest.mock()
语句的顺序无关紧要。
git-info.js
:
import simpleGit from 'simple-git';
/**
* The function returns the ticket Id, is presents, in the branch name
* @returns ticket Id
*/
export const getTicketIdFromBranchName = async (ticketRegex) => {
const git = simpleGit();
try {
const localBranches = await git.branchLocal();
const currentBranch = localBranches.current;
const currentBranchTicketMatches = currentBranch.match(ticketRegex);
if (currentBranchTicketMatches) {
return currentBranchTicketMatches[0];
}
return null;
} catch {
return null;
}
};
git-info.test.js
:
const TICKET_ID_REGEX = /((?<!([A-Z]{1,10})-?)[A-Z]+-d+)/.source;
describe('[utils/git-info]', () => {
it('getTicketIdFromBranchName | Function should proper ticket Id when there is one', async () => {
const { getTicketIdFromBranchName } = await import('./git-info');
const ticketId = 'CLO-1234';
jest.mock(
'simple-git',
() => {
const mGit = {
branchLocal: jest.fn(() => Promise.resolve({ current: `${ticketId} DUMMY TEST` })),
};
return jest.fn(() => mGit);
},
{ virtual: true }
);
const result = await getTicketIdFromBranchName(new RegExp(TICKET_ID_REGEX));
expect(result === ticketId).toEqual(true);
});
});
测试结果:
PASS stackoverflow/71808909/git-info.test.js (7.439 s)
[utils/git-info]
✓ getTicketIdFromBranchName | Function should proper ticket Id when there is one (6892 ms)
-------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------|---------|----------|---------|---------|-------------------
All files | 84.62 | 50 | 100 | 81.82 |
git-info.js | 84.62 | 50 | 100 | 81.82 | 19-21
-------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.215 s, estimated 9 s
软件包版本:"jest": "^26.6.3"