How to not send sms or emails when running mocha test suite

26 Views Asked by At

I'm running end-to-end tests with Mocha, but I do not want SMS or emails to be sent by the test suite. What is the best practice for skipping over these functions if testing, and running them when in production?

2

There are 2 best solutions below

0
Dipen Bhikadya On

You need dependency injection to use mock APIs during testing.

A simplest approach for a smaller project is to set an environment variable when running the test (e.g., ENV=TEST), and change the application code to skip the api calling or call a mock API (which may be logging the calls) when the environment variable value matches the expected value.

0
E. Mancebo On

You have to mock the implementation of the SMS/E-mail from the library that you're using. Mocha suggest in their documentation to use Simon.JS, Simple-Mock, and Nock.