If you're open to using the
esm module, you don't need to use the .mjs extension, or Babel for that matter.
Once you've installed esm, you just have to do something like: mocha -r esm [test glob pattern]
Now, for CircleCI, I like to have two test scripts in my package.json, one for CI and one for local dev, like so:
"test": "NODE_ENV=test mocha -r esm --watch ./tests/**/*.test.js",
"test:ci": "NODE_ENV=test mocha -r esm ./tests/**/*.test.js",
That way you can run tests in watch mode for local dev and have them run normally on CI (obviously you don't want tests running in watch mode on CI as they won't exit and the build will never complete).
Here's a sample CircleCI config you can leverage:
version: 2
defaults: &defaults
working_directory: ~/repo
docker:
- image: circleci/node:10.14.2
jobs:
core:
<<: *defaults
working_directory: ~/repo
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- run: npm lint
- run: npm run test:ci # notice, we use the test:ci script here not the test in watch mode script
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- persist_to_workspace:
root: ~/repo
paths: .
workflows:
version: 2
core:
jobs:
- core:
filters:
branches:
only:
- master