Utilizing the simple-git
package, I have implemented the following function:
import simpleGit from 'simple-git';
/**
* The function returns the ticket Id if present 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;
}
};
I am attempting to write a unit test for this function:
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 correctly identify ticket Id when one is present', 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);
});
});
Unfortunately, the unit test fails, indicating that it expected to receive true
but instead received false
on the final line.
I suspect that my usage of jest.mock
may be incorrect.