To run our test, we'd normally just run npx mocha. However, when we try that here, we get a warning:
$ npx mocha
Warning: Could not find any test files matching pattern: test
No test files found
This is because, by default, Mocha will try to find a directory named test at the root of the project and run the tests contained inside it. Since we placed our test code next to their corresponding module code, we must inform Mocha of the location of these test files. We can do this by passing a glob matching our test files as the second argument to mocha. Try running the following:
$ npx mocha "src/**/*.test.js"
src/validators/users/errors/index.unit.test.js:1
(function (exports, require, module, __filename, __dirname) { import assert from 'assert';
^^^^^^
SyntaxError: Unexpected token import
....
We got another error. We had already encountered this when we worked with cucumber-js. This error occurs because Mocha is not using Babel to transpile our test code before running it. With cucumber-js, we used the --require-module flag to require the @babel/register package, which ??. We can do the same with Mocha using its --require flag:
$ npx mocha "src/**/*.test.js" --require @babel/register
generateValidationErrorMessage
should return the correct string when error.keyword is "required"
1 passing (32ms)
Note that the test description we passed into describe and it is displayed in the test output.
should return the correct string when error.keyword is "required"