Вы можете jest.spyOn (object, methodName) смоделировать path.resolve
метод.
Например main.ts
:
import path from 'path';
export function main(filepath) {
return path.resolve(filepath);
}
main.test.ts
:
import { main } from './main';
import path from 'path';
describe('61419093', () => {
it('should pass', () => {
const resolveSpy = jest.spyOn(path, 'resolve').mockReturnValueOnce('/fakepath');
const actual = main('/root/avatar.jpg');
expect(actual).toBe('/fakepath');
expect(resolveSpy).toBeCalledWith('/root/avatar.jpg');
resolveSpy.mockRestore();
});
});
Результаты модульного тестирования со 100% покрытием:
PASS stackoverflow/61419093/main.test.ts (12.631s)
61419093
✓ should pass (4ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
main.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.426s