docs-next/src/content/docs/explainers/nodejs-native-esm-support.mdx
:::note[New in v7.1.0] :::
Mocha supports writing your tests as ES modules, and not just using CommonJS. For example:
// test.mjs
import { add } from "./add.mjs";
import assert from "assert";
it("should add to numbers from an es module", () => {
assert.equal(add(3, 5), 8);
});
To enable this you don't need to do anything special.
Write your test file as an ES module.
In Node.js this means either ending the file with a .mjs extension, or, if you want to use the regular .js extension, by adding "type": "module" to your package.json.
More information can be found in the Node.js documentation.
.mocharc.js or .mocharc.cjs)--no-experimental-require-module to the NODE_OPTIONS environment variable.proxyquire, rewiremock or rewire, hold off on using ES modules for your test files.
You can switch to using testdouble, which does support ESM.