Jest: Mocking and asserting typescript interfaces

Created on 30 Jan 2019  路  3Comments  路  Source: facebook/jest

馃殌 Feature Proposal

My proposal is to add some automated way of mocking so created object would have all the required properties. Asserting if an object is an instance of an Interface would be very useful as well.

Motivation

Jest has comprehensive mocking tools ready to use of the box. It is advertised as supporting typescript, but mocking interfaces seems to be a key feature that is missing in this area.

Example

const productMock = jest.mockInterface<IProduct>();
expect(productMock).instanceOf(IProduct);
Feature Request

Most helpful comment

I created a library to do this - https://github.com/marchaos/jest-mock-extended, which allows you to mock interfaces or classes along with some other mock extensions whilst maintaining type safety.

All 3 comments

Do you have an idea how this could be implemented? With a TypeScript compiler plugin it might be possible, but given that Jest uses Babel to compile TS and knows no actual type information, I don't really see a way to do this.

Sounds like this could be implemented in user space as a helper library for testing TS stuff. I don't see a place for this in the core at the moment.

I created a library to do this - https://github.com/marchaos/jest-mock-extended, which allows you to mock interfaces or classes along with some other mock extensions whilst maintaining type safety.

Was this page helpful?
0 / 5 - 0 ratings