|
| 1 | +# 8.2.0 / 2020-10-15 |
| 2 | + |
| 3 | +The major feature added in v8.2.0 is addition of support for [_global fixtures_](https://mochajs.org/#global-fixtures). |
| 4 | + |
| 5 | +While Mocha has always had the ability to run setup and teardown via a hook (e.g., a `before()` at the top level of a test file) when running tests in serial, Mocha v8.0.0 added support for parallel runs. Parallel runs are _incompatible_ with this strategy; e.g., a top-level `before()` would only run for the file in which it was defined. |
| 6 | + |
| 7 | +With [global fixtures](https://mochajs.org/#global-fixtures), Mocha can now perform user-defined setup and teardown _regardless_ of mode, and these fixtures are guaranteed to run _once and only once_. This holds for parallel mode, serial mode, and even "watch" mode (the teardown will run once you hit Ctrl-C, just before Mocha finally exits). Tasks such as starting and stopping servers are well-suited to global fixtures, but not sharing resources--global fixtures do _not_ share context with your test files (but they do share context with each other). |
| 8 | + |
| 9 | +Here's a short example of usage: |
| 10 | + |
| 11 | +```js |
| 12 | +// fixtures.js |
| 13 | + |
| 14 | +// can be async or not |
| 15 | +exports.mochaGlobalSetup = async function() { |
| 16 | + this.server = await startSomeServer({port: process.env.TEST_PORT}); |
| 17 | + console.log(`server running on port ${this.server.port}`); |
| 18 | +}; |
| 19 | + |
| 20 | +exports.mochaGlobalTeardown = async function() { |
| 21 | + // the context (`this`) is shared, but not with the test files |
| 22 | + await this.server.stop(); |
| 23 | + console.log(`server on port ${this.server.port} stopped`); |
| 24 | +}; |
| 25 | + |
| 26 | +// this file can contain root hook plugins as well! |
| 27 | +// exports.mochaHooks = { ... } |
| 28 | +``` |
| 29 | + |
| 30 | +Fixtures are loaded with `--require`, e.g., `mocha --require fixtures.js`. |
| 31 | + |
| 32 | +For detailed information, please see the [documentation](https://mochajs.org/#global-fixtures) and this handy-dandy [flowchart](https://mochajs.org/#test-fixture-decision-tree-wizard-thing) to help understand the differences between hooks, root hook plugins, and global fixtures (and when you should use each). |
| 33 | + |
| 34 | +## :tada: Enhancements |
| 35 | + |
| 36 | +- [#4308](https://github.com/mochajs/mocha/issues/4308): Support run-once [global setup & teardown fixtures](https://mochajs.org/#global-fixtures) ([**@boneskull**](https://github.com/boneskull)) |
| 37 | +- [#4442](https://github.com/mochajs/mocha/issues/4442): Multi-part extensions (e.g., `test.js`) now usable with `--extension` option ([**@jordanstephens**](https://github.com/jordanstephens)) |
| 38 | +- [#4472](https://github.com/mochajs/mocha/issues/4472): Leading dots (e.g., `.js`, `.test.js`) now usable with `--extension` option ([**@boneskull**](https://github.com/boneskull)) |
| 39 | +- [#4434](https://github.com/mochajs/mocha/issues/4434): Output of `json` reporter now contains `speed` ("fast"/"medium"/"slow") property ([**@wwhurin**](https://github.com/wwhurin)) |
| 40 | +- [#4464](https://github.com/mochajs/mocha/issues/4464): Errors thrown by serializer in parallel mode now have error codes ([**@evaline-ju**](https://github.com/evaline-ju)) |
| 41 | + |
| 42 | +_For implementors of custom reporters:_ |
| 43 | + |
| 44 | +- [#4409](https://github.com/mochajs/mocha/issues/4409): Parallel mode and custom reporter improvements ([**@boneskull**](https://github.com/boneskull)): |
| 45 | + - Support custom worker-process-only reporters (`Runner.prototype.workerReporter()`); reporters should subclass `ParallelBufferedReporter` in `mocha/lib/nodejs/reporters/parallel-buffered` |
| 46 | + - Allow opt-in of object reference matching for "sufficiently advanced" custom reporters (`Runner.prototype.linkPartialObjects()`); use if strict object equality is needed when consuming `Runner` event data |
| 47 | + - Enable detection of parallel mode (`Runner.prototype.isParallelMode()`) |
| 48 | + |
| 49 | +## :bug: Fixes |
| 50 | + |
| 51 | +- [#4476](https://github.com/mochajs/mocha/issues/4476): Workaround for profoundly bizarre issue affecting `npm` v6.x causing some of Mocha's deps to be installed when `mocha` is present in a package's `devDependencies` and `npm install --production` is run the package's working copy ([**@boneskull**](https://github.com/boneskull)) |
| 52 | +- [#4465](https://github.com/mochajs/mocha/issues/4465): Worker processes guaranteed (as opposed to "very likely") to exit before Mocha does; fixes a problem when using `nyc` with Mocha in parallel mode ([**@boneskull**](https://github.com/boneskull)) |
| 53 | +- [#4419](https://github.com/mochajs/mocha/issues/4419): Restore `lookupFiles()` in `mocha/lib/utils`, which was broken/missing in Mocha v8.1.0; it now prints a deprecation warning (use `const {lookupFiles} = require('mocha/lib/cli')` instead) ([**@boneskull**](https://github.com/boneskull)) |
| 54 | + |
| 55 | +Thanks to [**@AviVahl**](https://github.com/AviVahl), [**@donghoon-song**](https://github.com/donghoon-song), [**@ValeriaVG**](https://github.com/ValeriaVG), [**@znarf**](https://github.com/znarf), [**@sujin-park**](https://github.com/sujin-park), and [**@majecty**](https://github.com/majecty) for other helpful contributions! |
| 56 | + |
1 | 57 | # 8.1.3 / 2020-08-28
|
2 | 58 |
|
3 | 59 | ## :bug: Fixes
|
|
0 commit comments