const winPath = require("path").win32;
jest.mock('path', () => {
return {
...(jest.requireActual('path') as typeof import('path')),
win32: {
normalize: jest.fn(),
}
}
})
describe('74717157', () => {
test('should pass', () => {
winPath.normalize.mockImplementation(() => 'override the original implementation')
expect(jest.isMockFunction(winPath.normalize)).toBeTruthy();
expect(winPath.normalize()).toBe('override the original implementation')
})
})
检测结果:
PASS stackoverflow/74717157/index.test.ts (10.557 s)
74717157
✓ should pass (1 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.116 s
1条答案
按热度按时间qni6mghb1#
jest.mock()
应该可以工作。下面的示例显示了如何模拟win32.normalize()
方法。检测结果: