IssueRaiser

Introduce a more flexible and better typed way to mock APIs

## 🚀 Feature Proposal This introduces an easy-to-use, lightweight and concise way to (partially) mock typed APIs (Typescript modules, types, classes and above all interfaces) without introducing any breaking change to the API. ## Motivation Mocking interfaces on a per-test basis is not possible right now in jest. IMO it is good testing practices to NOT re-use mocks across tests as this quickly makes the mock become a hard-to-maintain object in its own right. Plus shared mocks introduce unwanted dependencies between tests. It is rather established practice to generate light-weight throw-away mocks for each test case that only mock a minimal set of API methods to document what is actually being used by the SUT (and throwing errors if something unexpected is being used). This is currently not well supported by jest - neither for modules nor for hashes/classes and not at all for interfaces. The mocking of interfaces is central to good programming practices, though, as APIs should always be implementations of interfaces and tests should mock the interface rather than a specific implementation as this will much better decouple the test from the underlying API and avoid false negatives when the implementation details change. ## Example ```ts // The interface to be mocked - no implementation must be known/available! // It is even possible to do TDD with deferred implementation of the API, e.g. // when the API is to be developed at a later time/in parallel by another team // o

Posted by ghost on Feb 8, 2019, 7:39 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,000 issues0 comments

github (Other)

Introduce a more flexible and better typed way to mock APIs

ghostFeb 8, 2019, 7:39 AM

## 🚀 Feature Proposal This introduces an easy-to-use, lightweight and concise way to (partially) mock typed APIs (Typescript modules, types, classes and above all interfaces) without introducing any breaking change to the API. ## Motivation Mocking interfaces on a per-test basis is not possible right now in jest. IMO it is good testing practices to NOT re-use mocks across tests as this quickly makes the mock become a hard-to-maintain object in its own right. Plus shared mocks introduce unwanted dependencies between tests. It is rather established practice to generate light-weight throw-away mocks for each test case that only mock a minimal set of API methods to document what is actually being used by the SUT (and throwing errors if something unexpected is being used). This is currently not well supported by jest - neither for modules nor for hashes/classes and not at all for interfaces. The mocking of interfaces is central to good programming practices, though, as APIs should always be implementations of interfaces and tests should mock the interface rather than a specific implementation as this will much better decouple the test from the underlying API and avoid false negatives when the implementation details change. ## Example ```ts // The interface to be mocked - no implementation must be known/available! // It is even possible to do TDD with deferred implementation of the API, e.g. // when the API is to be developed at a later time/in parallel by another team // o

Comments (0)

No comments yet. Start the discussion.

Comment on this issue