import path from 'path';
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Expect {
pathMatching: (expected: string) => CustomMatcherResult;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R> {
pathMatching: (expected: string) => CustomMatcherResult;
}
}
}
/**
* Converts Windows paths that use \\ as separator to /
*/
export const ensurePosixPathSeparators = (origPath: string) =>
origPath.split(path.sep).join(path.posix.sep);
/**
* This custom matcher allows it to search for a path to be contained in a received result.
* It automatically converts the received path to the posix separator (/), which means the expected path
* should also be defined as such.
*/
function pathMatching(
this: jest.MatcherUtils,
received: string,
expected: string
): jest.CustomMatcherResult {
if (typeof received === 'string') {
const posixified = ensurePosixPathSeparators(received);
const pass = posixified.includes(ensurePosixPathSeparators(expected));
return {
pass,
message: () => `expected path "${expected}" to be contained in "${received}"`
};
} else {
throw new Error('The "pathMatching" matcher can only be used for string values.');
}
}
expect.extend({
pathMatching: pathMatching
});