Compilers deprecation
If you’re here, you probably hit the deprecation notice. Sorry about that!
Will it break?
Section titled “Will it break?”This is a soft deprecation, which means you get nagged about it, but it won’t break (yet).
Make it go away
Section titled “Make it go away”To suppress this warning, execute mocha with the --no-deprecation flag (though you won’t get notice of any other deprecations you may encounter either).
… but why?
Section titled “… but why?”--compilers is redundant; we’ve yet to encounter a real-world situation in which the solution couldn’t be expressed using --require.
What should I use instead then?
Section titled “What should I use instead then?”Let’s say you want to compile using CoffeeScript. Ensure that you have the coffeescript package installed as a dev dependency:
npm install coffeescript --save-dev
Then update your package.json with the relevant require statement: --require coffeescript/register.
Here’s a list of popular compilers/transpilers:
- CoffeeScript:
--compilers coffee:coffee-script/registerbecomes--require coffeescript/register - Babel 6:
--compilers js:babel-core/registerbecomes--require babel-core/register - Babel 7:
--require babel-core/registerused if you are using Babel v6 becomes--require @babel/registerwith Babel v7. - TypeScript:
--compilers ts:ts-node/registerbecomes--require ts-node/register - LiveScript:
--compilers ls:livescriptbecomes--require livescript - (feel free to add more examples!)
You’ll have to handle file extensions as well. Mocha, by default, only loads .js files when given a directory (and the default directory is test). Therefore, to use a different file extension (such as .coffee or .ts), you will need to supply a glob instead of simply a directory. If this was how you ran Mocha pre-v4:
$ mocha --compilers coffee:coffee-script/register --recursive ./testThen this is how you’d accomplish the same thing (** roughly means “recursive”) in v4:
$ mocha --require coffeescript/register "test/**/*.js"When you wrap a glob in quotes, file discovery is handed to the glob package. It’s recommended to wrap in double-quotes, because the result should be the same regardless of your shell or environment (Windows/Linux/macOS, etc.).
glob is powerful. For instance, if your test dir has tests written in both JS and CoffeeScript, you could do this:
$ mocha --require coffeescript/register "test/**/*.{js,coffee}"How do I use this with --watch?
Section titled “How do I use this with --watch?”When using --watch, you will also need to specify the extension(s) to watch via --watch-extensions, e.g.:
$ mocha --require coffeescript/register --watch --watch-extensions js,coffee "test/**/*.{js,coffee}"