Replies: 2 comments
-
Thank you for such detailed explanation. Seeing that jest-mock-extended can provide more advantages we'll try and change from ts-mockito to these new API and see how it goes. Thanks again! |
Beta Was this translation helpful? Give feedback.
0 replies
-
Closed with #6 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In almost all of the projects that we use archimedes-js we also use ts-mockito for mocking commands, queries, etc.
A few months ago I discovered and used in a few projects jest-mock-extended library, which provides type-safe mocking extensions for Jest, allowing us to use the Jest APIs for all of our mocks.
jest-mock-extended is downloaded almost 160.000 times per week and has a more active development than ts-mockito.
Today I had a conversation with @cesalberca about replacing ts-mockito with jest-mock-extended in new projects and he suggested creating a new discussion here for all.
Code example:
Advantages of jest-mock-extended over ts-mockito:
Disadvantages of jest-mock-extended over ts-mockito:
Most of the time with jest-mock-extended expecting an argument matching is not necessary, because we can mock directly the function call and later assert that was called correctly as shown on the first jest-mock-extended test. But if we need it, the behaviour of jest-mock-extended is the same as ts-mockito and we could use any of the available jest matchers, like, containsValue('value') or create our own deepEqual method as I did on the second jest-mock-extended test.
toHaveBeenCalledTimes(n) and toHaveBeenCalledWith(value) into one single assertion... There is something similar, but not as semantic: expect(firstQryMock.execute).toHaveBeenNthCalledWith(1, { foo: false }). We could wrap it with our own assertion to give it a better naming.
These disadvantages are more limitations of the Jest API than of the library itself... And all of them have an easy workaround.
After using it in a few projects, I noticed that the tests code is cleaner and the debugging is easier because of better assertions error messages and the IDEs integration with the Jest APIs.
What do you think about using jest-mock-extended in new projects?
Beta Was this translation helpful? Give feedback.
All reactions