Вы должны реализовать заводскую функцию jest.mock (moduleName, factory, options) .
Например,
my-document.ts
:
// simulate the Typegoose class
class Typegoose {
public getModelForClass(cls) {
return cls.toString();
}
}
export class MyDocument extends Typegoose {}
export const MyDocumentModel = new MyDocument().getModelForClass(MyDocument);
script-under-test.ts
:
import { MyDocumentModel } from './my-document';
export async function createDocument(doc: any) {
return await MyDocumentModel.create(doc);
}
script-under-test.test.ts
:
import { MyDocumentModel } from './my-document';
import { createDocument } from './script-under-test';
jest.mock('./my-document', () => {
const mMyDocumentModel = { create: jest.fn() };
return { MyDocumentModel: mMyDocumentModel };
});
describe('creation', () => {
it('should create a new document', async () => {
const payload = { foo: 'bar' };
MyDocumentModel.create.mockResolvedValueOnce(document);
const result = await createDocument(payload);
expect(MyDocumentModel.create).toHaveBeenCalledWith(payload);
expect(result).toStrictEqual(document);
});
});
Результаты модульного теста со 100% покрытием:
PASS stackoverflow/61388982/ script-under-test.test.ts (10.103s)
creation
✓ should create a new document (5ms)
----------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
script-under-test.ts | 100 | 100 | 100 | 100 |
----------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.535s