Using the Mocha framework
Mocha is an older but feature-rich testing framework that runs in Node.js and also the browser. In this section, we’ll exclusively use Mocha in Node.js. Unlike Jest, the notion of an environment does not exist. Nevertheless, a similar setup can be achieved, where browser APIs would be emulated by some npm package such as jsdom
.
To use Mocha, you need to install the mocha
package from npm:
$ npm install mocha --save-dev
This allows you to use the mocha
command-line utility. Ideally, run it with npx
as we did with the other tools:
$ npx mocha
At this point, not much is working. By default, Mocha follows a different convention from Jest. Here, we need to specify a different pattern or place our tests in a folder named test
.
What we definitely need to do is to include Babel for code transformations. This works a bit differently than with Jest. Instead of a dedicated plugin, we only integrate the @babel/register
package, which will automatically...