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 badges2 Answers
Reset to default 4You 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
评论列表(0条)