[英]Jest/Typescript: Mock class dependencies in Jest and Typescript
[英]Jest - mock a named class-export in typescript
我有一個節點模塊,它導出了一些類,其中一個是Client
,我用它來創建一個客戶端(有一些 API 作為方法)。
我正在嘗試使用 Jest 測試我的模塊,該模塊將此節點模塊用作依賴項。 但是,我無法成功模擬 Client 類中的一種方法(比如search()
)。
這是我的myModule
規范:
//index.spec.ts import * as nock from 'nock'; import * as externalModule from 'node-module-name'; import { createClient } from './../../src/myModule'; describe(() => { beforeAll(() => { nock.disableNetConnect(); }); it('test search method in my module', () => { jest.mock('node-module-name'); const mockedClient = <jest.Mock<externalModule.Client>>externalModule.Client; const myClient = createClient({/*params*/}); //returns instance of Client class present in node module by executing Client() constructor myClient.searchByName('abc'); //calls search API - I need to track calls to this API expect(mockedClient).toHaveBeenCalled(); expect(mockedClient.prototype.search).toHaveBeenCalledWith('abc'); }); });
但是,這根本不會創建模擬並觸發 nock 錯誤,因為搜索 API 會嘗試連接到 url(通過參數給出)。
我也嘗試過像下面這樣模擬 Client 類。 雖然成功地為 Client 類和搜索 API 創建了一個模擬(驗證search()
也通過控制台日志模擬),但當我嘗試檢查search()
是否已被調用時,它給了我一個錯誤。
externalModule.Client = jest.fn(() => { return { search: jest.fn(() => Promise.resolve('some response')) } }); //creates the mock successfully, but not sure how to track calls to 'search' property const client = myModule.createClient(/*params*/); client.searchByName('abc'); expect(externalModule.Client).toHaveBeenCalled(); //Successful expect(externalModule.Client.prototype.search).toHaveBeenCalled(); //returns error saying "jest.fn() value must be a mock function or spy, Received: undefined"
我不確定我做錯了什么。 先感謝您。
嘗試將jest.mock
移動到文件頂部
//index.spec.ts
const search = jest.fn();
jest.mock('node-module-name', () => ({
Client: jest.fn(() => ({ search }))
}));
import * as nock from 'nock';
import * as externalModule from 'node-module-name';
import { createClient } from './../../src/myModule';
describe(() => {
beforeAll(() => {
nock.disableNetConnect();
});
it('test search method in my module', () => {
const myClient = createClient({/*params*/});
myClient.searchByName('abc');
expect(externalModule.Client).toHaveBeenCalled();
expect(search).toHaveBeenCalledWith('abc');
externalModule.Client.mockClear();
search.mockClear();
});
});
創建search
常量並跟蹤它。
const search = jest.fn();
externalModule.Client = jest.fn(() => ({ search }));
const client = myModule.createClient(/*params*/);
client.searchByName('abc');
expect(externalModule.Client).toHaveBeenCalled();
expect(search).toHaveBeenCalled();
這就是我嘲笑它的方式。 我不得不更改命名並刪除一些代碼以避免暴露原始源代碼。
jest.mock('../foo-client', () => {
return { FooClient: () => ({ post: mockPost }) }
})
完整的代碼。
// foo-client.ts
export class FooClient {
constructor(private config: any)
post() {}
}
// foo-service.ts
import { FooClient } from './foo-client'
export class FooLabelService {
private client: FooClient
constructor() {
this.client = new FooClient()
}
createPost() {
return this.client.post()
}
}
// foo-service-test.ts
import { FooService } from '../foo-service'
const mockPost = jest.fn()
jest.mock('../foo-client', () => {
return { FooClient: () => ({ post: mockPost }) }
})
describe('FooService', () => {
let fooService: FooService
beforeEach(() => {
jest.resetAllMocks()
fooService = new FooService()
})
it('something should happened', () => {
mockPost.mockResolvedValue()
fooService.createPost()
})
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.