You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -354,11 +356,11 @@ With its default "BDD"-style interface, Mocha provides the hooks `before()`, `af
354
356
```js
355
357
describe('hooks', function() {
356
358
before(function() {
357
-
// runs before all tests in this block
359
+
// runs once before the first test in this block
358
360
});
359
361
360
362
after(function() {
361
-
// runs after all tests in this block
363
+
// runs once after the last test in this block
362
364
});
363
365
364
366
beforeEach(function() {
@@ -868,7 +870,8 @@ Configuration
868
870
--package Path to package.json for config [string]
869
871
870
872
File Handling
871
-
--extension File extension(s) to load [array] [default: js]
873
+
--extension File extension(s) to load
874
+
[array] [default: ["js","cjs","mjs"]]
872
875
--file Specify file(s) to be loaded prior to root suite
873
876
execution [array] [default: (none)]
874
877
--ignore, --exclude Ignore file(s) or glob pattern(s)
@@ -1538,6 +1541,42 @@ Alias: `HTML`, `html`
1538
1541
1539
1542
**The HTML reporter is not intended for use on the command-line.**
1540
1543
1544
+
## Node.JS native ESM support
1545
+
1546
+
> _New in v7.1.0_
1547
+
1548
+
Mocha supports writing your tests as ES modules, and not just using CommonJS. For example:
1549
+
1550
+
```js
1551
+
// test.mjs
1552
+
import {add} from'./add.mjs';
1553
+
importassertfrom'assert';
1554
+
1555
+
it('should add to numbers from an es module', () => {
1556
+
assert.equal(add(3, 5), 8);
1557
+
});
1558
+
```
1559
+
1560
+
To enable this you don't need to do anything special. Write your test file as an ES module. In Node.js
1561
+
this means either ending the file with a `.mjs` extension, or, if you want to use the regular `.js` extension, by
1562
+
adding `"type": "module"` to your `package.json`.
1563
+
More information can be found in the [Node.js documentation](https://nodejs.org/api/esm.html).
1564
+
1565
+
> Mocha supports ES modules only from Node.js v12.11.0 and above. To enable this in versions smaller than 13.2.0, you need to add `--experimental-modules` when running
1566
+
> Mocha. From version 13.2.0 of Node.js, you can use ES modules without any flags.
1567
+
1568
+
### Current Limitations
1569
+
1570
+
Node.JS native ESM support still has status: **Stability: 1 - Experimental**
1571
+
1572
+
-[Watch mode](#-watch-w) does not support ES Module test files
1573
+
-[Custom reporters](#third-party-reporters) and [custom interfaces](#interfaces)
1574
+
can only be CommonJS files
1575
+
-[Required modules](#-require-module-r-module) can only be CommonJS files
1576
+
-[Configuration file](#configuring-mocha-nodejs) can only be a CommonJS file (`mocharc.js` or `mocharc.cjs`)
1577
+
- When using module-level mocks via libs like `proxyquire`, `rewiremock` or `rewire`, hold off on using ES modules for your test files
1578
+
- Node.JS native ESM support does not work with [esm][npm-esm] module
1579
+
1541
1580
## Running Mocha in the Browser
1542
1581
1543
1582
Mocha runs in the browser. Every release of Mocha will have new builds of `./mocha.js` and `./mocha.css` for use in the browser.
@@ -1609,17 +1648,17 @@ mocha.setup({
1609
1648
1610
1649
### Browser-specific Option(s)
1611
1650
1612
-
Browser Mocha supports many, but not all [cli options](#command-line-usage).
1651
+
Browser Mocha supports many, but not all [cli options](#command-line-usage).
1613
1652
To use a [cli option](#command-line-usage) that contains a "-", please convert the option to camel-case, (eg. `check-leaks` to `checkLeaks`).
1614
1653
1615
1654
#### Options that differ slightly from [cli options](#command-line-usage):
1616
1655
1617
-
`reporter`_{string|constructor}_
1656
+
`reporter`_{string|constructor}_
1618
1657
You can pass a reporter's name or a custom reporter's constructor. You can find **recommended** reporters for the browser [here](#reporting). It is possible to use [built-in reporters](#reporters) as well. Their employment in browsers is neither recommended nor supported, open the console to see the test results.
1619
1658
1620
1659
#### Options that _only_ function in browser context:
1621
1660
1622
-
`noHighlighting`_{boolean}_
1661
+
`noHighlighting`_{boolean}_
1623
1662
If set to `true`, do not attempt to use syntax highlighting on output test code.
1624
1663
1625
1664
### Reporting
@@ -1701,7 +1740,8 @@ tests as shown below:
1701
1740
1702
1741
In addition to supporting the deprecated [`mocha.opts`](#mochaopts) run-control format, Mocha now supports configuration files, typical of modern command-line tools, in several formats:
1703
1742
1704
-
-**JavaScript**: Create a `.mocharc.js` in your project's root directory, and export an object (`module.exports = {/* ... */}`) containing your configuration.
1743
+
-**JavaScript**: Create a `.mocharc.js` (or `mocharc.cjs` when using [`"type"="module"`](#nodejs-native-esm-support) in your `package.json`)
1744
+
in your project's root directory, and export an object (`module.exports = {/* ... */}`) containing your configuration.
1705
1745
-**YAML**: Create a `.mocharc.yaml` (or `.mocharc.yml`) in your project's root directory.
1706
1746
-**JSON**: Create a `.mocharc.json` (or `.mocharc.jsonc`) in your project's root directory. Comments — while not valid JSON — are allowed in this file, and will be ignored by Mocha.
1707
1747
-**package.json**: Create a `mocha` property in your project's `package.json`.
0 commit comments