使用笑话模拟时出现打字稿错误 [英] Typescript errors when using jest mocks
问题描述
我有一个先前创建的.js
文件,该文件模拟了出于jest
测试目的的某些功能.我正在将其迁移到.ts
文件:
I have a previously-created .js
file that mocks away some of our functions for jest
test purposes. I'm migrating that to a .ts
file:
Server.ts
const Server = jest.genMockFromModule('../Server');
Server.getAsync = Server.default.getAsync;
// other REST-ful functions here
export default Server;
我遇到以下错误:
类型'{}'不存在属性'getAsync'
Property 'getAsync' does not exist on type '{}'
类型"{}"上不存在属性默认"
Property 'default' does not exist on type '{}'
然后,在相应的测试文件中:
Then, in a corresponding test file:
MyComponent.test.ts
import Server from 'path/to/Server';
jest.mock('path/to/Server');
const dispatchMock = jest.fn();
const getStateMock = jest.fn();
describe('MyComponent.someFunction', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('Does the right stuff', () => {
Server.getAsync.mockReturnValueOnce(Promise.resolve([{ key: 'value' }]));
dispatchMock.mockImplementationOnce((promise) => promise);
dispatchMock.mockImplementationOnce();
return someFunction()(dispatchMock)
.then(() => {
expect(Server.getAsync).toHaveBeenCalledTimes(1);
expect(Server.getAsync.mock.calls[0][0]).toBe('something');
});
});
});
我在dispatchMock.mockImplementationOnce()
上出现错误
期望1个参数,但得到0.(方法) jest.MockInstance< {}>.mockImplementationOnce(fn:(... args:any [])=> 任何):jest.Mock< {}>
Expected 1 arguments, but got 0. (method) jest.MockInstance<{}>.mockImplementationOnce(fn: (...args: any[]) => any): jest.Mock<{}>
...在Server.getAsync.mockReturnValueOnce
类型'(URL:string, baseRoute ?:字符串|空,loadingGenerator ?:(isLoading:布尔值)=> {type:strin ...'.
Property 'mockReturnValueOnce' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.
...以及Server.getAsync.mock
类型'((url:string,baseRoute ?: 字符串| null,loadingGenerator ?:(isLoading:boolean)=> {类型: 结...".
Property 'mock' does not exist on type '(url: string, baseRoute?: string | null, loadingGenerator?: (isLoading: boolean) => { type: strin...'.
我已经为此努力了一段时间,所以任何帮助将不胜感激.
I've been pounding my head on this for a while so any help would be greatly appreciated.
更新
好吧,我在Server.ts
文件第一行的末尾添加了as any
,所以现在看起来像这样:
Okay, I added as any
to the end of the first line of my Server.ts
file so now it looks like:
const Server = jest.genMockFromModule('../Server') as any;
摆脱了第一组错误.虽然仍然遇到我的.test.ts
文件中的错误.
That got rid of the first set of errors. Still facing the errors in my .test.ts
file though.
更新2
我注意到,当我运行实际的笑话测试时,即使存在TypeError,它们也都通过了.这些问题似乎与实际测试无关.
I've noticed that when I run the actual jest tests, that they all pass even though there are TypeErrors. These issues don't appear to be related to actual tests.
推荐答案
我自己修复了此问题.我使用它的方法是将对Server.getAsync
的所有调用转换为特定的笑话模拟类型.
I fixed this myself. The way that I got it to work was to cast any calls to Server.getAsync
to the specific jest mock type.
let getAsyncMock = Server.getAsync as jest.Mock
或
let getAsyncMock = <jest.Mock>(Server.getAsync)
这消除了我的错误.
这篇关于使用笑话模拟时出现打字稿错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!