When you do Unit Testing you NEVER want to do calls to external services or your database so you abstract away the logic for this into one module which you then mock in all your unit tests. Of course, there are also “global Mocks” in Jest you can set once for all your tests, but that's another story.
You can see the whole codebase of the below example in the following repository on GitHub: Abszissex/medium-jest-mock
For our example, let's imagine we have some
main() method which calls some external service and returns its result.
For Unit Testing, in this
main() method we have to mock the external service calls, triggered via
callExternalService() for several reasons:
We don't want to suddenly see our tests failing just because some external service going down or having problems. Failing tests could e.g. block deployment pipelines and lead to unnecessary investigations for errors even though there are none on our side.
Calling the external service each time slows down test running. Of course, it takes longer to trigger some HTTP call instead of simply mocking some method, returning some plain value.
Calling a real service might introduce certain states which will only work in the first run, but might fail in the second one. Imagine the creation of a resource with some identifier. The second time you call it it will throw an exception that the resource already exists.
Unit Tests should only test the unit you are working on. You don't want to test another service with it. You should expect the creators of the external service to test their code.
The file structure of our example:
/ - externalService.js - index.js - index.spec.js - package.json - package-lock.json
Of course, before we can run the Jest tests make sure that you have installed it before via
npm i --save-dev jest.
Our example code:
In the above example, you can see that we test our
main() method for two different responses from our external service. Since we only return
false here we e.g. could expect that the external service is a simple
doesResourceExists() endpoint returning some boolean value to indicate if a resource might exist.
After we run our test cases (
npm run test), everything is green:
Result after running the
npm run test command in your console
Of course, the above example is purely for demonstrating purposes.
You should never ever make external calls when doing unit testing, due to several reasons mentioned at the beginning. It's not Unit Testing then anymore anyway.
Jest makes it super easy to mock constants/methods imported from other modules/files with just a few LoC.
Thanks for taking the time to read my article.
Do you want to get in touch?
If you want to contact me, please hit me up on LinkedIn.