javascript - Jest mock with TypeScript - Stack Overflow

I have this simple test file:describe('index', () => {it('should bootstrap the app�

I have this simple test file:

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

I get the following typescript error: "TS2339: property 'mockImplementationOnce' does not exist on type 'Renderer'"

Anyone knows how I can make TypeScript recognize jest mock methods?

I have this simple test file:

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

I get the following typescript error: "TS2339: property 'mockImplementationOnce' does not exist on type 'Renderer'"

Anyone knows how I can make TypeScript recognize jest mock methods?

Share Improve this question edited Jun 19, 2018 at 16:50 Kizer asked Jun 18, 2018 at 7:24 KizerKizer 1,6662 gold badges17 silver badges24 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can use type assertion to hint typescript that render is a SpyInstance

const render = ReactDOM.render as any as SpyInstance;
render.mockImplementationOnce(() => { });
...

Instead of using ReactDOM.render which doesn't have the proper type, use the returned value of jest.spyOn(ReactDOM, 'render') which is a Jest mock function (cf. spyOn() doc) i.e. with the expected type for TypeScript, including both methods mockImplementationOnce() and mockRestore().

const reactRenderMock = jest.spyOn(ReactDOM, 'render');
// ...
reactRenderMock.mockImplementationOnce(() => {} );
// ...
reactRenderMock.render.mockRestore();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744957930a4603307.html

相关推荐

  • javascript - Jest mock with TypeScript - Stack Overflow

    I have this simple test file:describe('index', () => {it('should bootstrap the app�

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信