使用TypeScript进行Jest模拟

Kiz*_*zer 5 javascript typescript reactjs jestjs

我有这个简单的测试文件:

describe('index', () => {
    it('should bootstrap the app', async () => {
        const root = <div />;
        jest.spyOn(ReactDOM, 'render');
        ...
        ReactDOM.render.mockImplementationOnce(() => {} );
        ...
        ReactDOM.render.mockRestore();
    } );
} );
Run Code Online (Sandbox Code Playgroud)

我收到以下打字稿错误:"TS2339:属性'mockImplementationOnce'在'Renderer'类型上不存在"

任何人都知道如何使TypeScript识别jest模拟方法?

Ale*_* L. 4

您可以使用类型断言来提示打字稿renderSpyInstance

const render = ReactDOM.render as any as SpyInstance;
render.mockImplementationOnce(() => { });
...
Run Code Online (Sandbox Code Playgroud)