Running unit tests as an npm script

Typing out the full mocha command each time can be tiresome. Therefore, we should create an npm script just like we did with the E2E tests. Add the following to the scripts object inside our package.json file:

"test:unit": "mocha 'src/**/*.test.js' --require @babel/register",

Furthermore, let's also update our existing test npm script to run all our tests (both unit and E2E):

"test": "yarn run test:unit && yarn run test:e2e",

Now, we can run our unit tests by running yarn run test:unit, and run all our tests with yarn run test. We have now completed our first unit test, so let's commit the changes and move on to writing even more tests:

$ git add -A && \ git commit -m "Implement first unit test for generateValidationErrorMessage" ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.