Должно работать.
index.ts
:
import { parse, UrlWithParsedQuery } from 'url';
export class Utils {
public static parseUrl(url: string): UrlWithParsedQuery {
return parse(url, true);
}
}
index.test.ts
:
import { Utils } from './';
import url from 'url';
describe('60884651', () => {
it('should parse url', () => {
const parseSpy = jest.spyOn(url, 'parse');
const actual = Utils.parseUrl('http://stackoverflow.com');
expect(actual.href).toBe('http://stackoverflow.com/');
expect(actual.protocol).toBe('http:');
expect(parseSpy).toBeCalledWith('http://stackoverflow.com', true);
parseSpy.mockRestore();
});
});
Результаты модульного теста со 100% покрытием:
PASS stackoverflow/60884651/index.test.ts
60884651
✓ should parse url (10ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.104s, estimated 10s