Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mocha-parallel-tests-ui-option

mocha-parallel15MIT2.4.0TypeScript support: included

Run mocha tests in parallel

mocha, mocha-parallel-tests, parallel tests, unit tests, tests, threads, worker threads

readme

mocha-parallel-tests

Build Status

mocha-parallel-tests is a test runner for tests written with mocha testing framework which allows you to run them in parallel. mocha-parallel-tests executes each of your test files in a separate process while maintaining the output structure of mocha. Compared to the other tools which try to parallelize mocha tests execution, mocha-parallel-tests doesn't require you to write the code in a different way or use some specific APIs - just run your tests with mocha-parallel-tests instead of mocha and you will see the difference. Or if you prefer to use mocha programmatic API replace it with mocha-parallel-tests default export and you're done!

If you're using Node.JS >= 12 your tests execution will be even faster because mocha-parallel-tests supports running tests with Node.JS worker threads API: instead of creating fully fledged Node.JS processes mocha-parallel-tests runs your tests in lighter threads within the same process. This results in a faster tests processing and less memory consumption.

Installation

npm install --save-dev mocha mocha-parallel-tests

ATTENTION: mocha is a peer dependency of mocha-parallel-tests so you also need to install mocha. Currently mocha versions 3, 4, 5 and 6 are supported.

Usage

CLI

# mocha example
$ mocha -R xunit --timeout 10000 --slow 1000 test/*.spec.js

# mocha-parallel-tests example
$ mocha-parallel-tests -R xunit --timeout 10000 --slow 1000 test/*.spec.js

Most of mocha CLI options are supported. If you're missing some of the options support you're welcome to submit a PR: all options are applied in a same simple way.

Programmatic API

// mocha example
import Mocha from 'mocha';
const mocha = new Mocha();
mocha.addFile(`${__dirname}/index.spec.js`);
mocha.run();

// mocha-parallel-tests example
// if you're using TypeScript you don't need to install @types/mocha-parallel-tests
// because package comes with typings in it
import Mocha from 'mocha-parallel-tests'; // or `const Mocha = require('mocha-parallel-tests').default` if you're using CommonJS
const mocha = new Mocha();
mocha.addFile(`${__dirname}/index.spec.js`);
mocha.run();

Parallel limit

mocha-parallel-tests CLI executable has its own --max-parallel option which is the amount of tests executed at the same time. By default it's equal to the number of logical CPI cores (os.cpus().length) on your computer but you can also specify your own number or set it to 0, which means that all test files will be started executing at the same time. However this is not recommended especially on machines with low number of CPUs and big number of tests executed.

Differences with mocha

Main difference with mocha comes from the fact that all files are executed in separate processes/threads and don't share the scope. This means that even global variables values that you could've used to share the data between test suites will not be reliable. There's also some specific behaviour for some of the mocha CLI options like --bail: it's just applied to each test in its process. You can see the full list of differences here.

There's also a list of limitations that you can hit when you launch your tests with mocha-parallel-tests.

From the reporter perspective the main difference between tests executed with mocha and mocha-parallel-tests is another level of nesting which again comes from the fact that main process adds one more "suite" level and all tests results are merged into that:

mocha

<kbd> mocha spec reporter output </kbd>

mocha-parallel-tests

<kbd> mocha-parallel-tests spec reporter output </kbd>

Comparison with mocha.parallel

mocha.parallel is a tool which allows you to run mocha tests in parallel. While it seems pretty similar to mocha-parallel-tests there's a massive difference between them. Check this page for a full comparison.

changelog

2.3.0

  • new: mocha=7 is supported
  • fix: --file option is not supported (#222)
  • fix: security warnings
  • trivial: local development Node.JS version is updated to 13

2.2.2

  • fix: mocha-parallel-tests no longer reequire implicit tslib dependency (#262)

2.2.1

  • fix: TTY-related issue where users of Node.JS>=12 could see an error "TypeError: tty.getWindowSize is not a function" (#247)
  • chore: last tslint comments removed from the code, mocha-parallel-tests Typescript code and Javascript tests are now linted completely by eslint (#242)
  • chore: mocha-parallel-tests are now also executed in OSX via Travis CI

2.2.0

  • new: mocha-parallel-tests now forks light threads instead of fully fledged Node.JS processes in environments where worker_threads API is supported. Usually it's Node.JS >= 12. This results in a faster tests processing and less memory consumption.

2.1.2

  • fix: programmatic API doesn't emit particular suites when multiple suites share the name (#237)

2.1.1

  • fix: --reporter-options doesn't work with mocha@6 (#202)
  • fix: npm install audio warnings because of the outdated dependencies

2.1.0

  • new: mocha=6 is supported (#217)
  • fix: --retries throws an unhandled exception when test cases are defined in a loop (#194)
  • fix: -g alias for --grep is not supported (#203)
  • fix: --full-trace option is not supported (#202)
  • fix: --exit option leaves no trace for some asynchronously running tests (#202)
  • remove: crypto library usage
  • linting is all done by eslint now.

2.0.5

  • fix: --retries with hooks throws an unhandled exception (#194)

2.0.4

  • new: --grep option is now supported for both CLI and API (#187, #188)

2.0.3

  • fix: subprocess processing should not stop when unhandled rejection occurs (#173)
  • fix: subprocess processing should not stop when uncaught exception occurs (#172)
  • added a list of limitations that you can hit when you launch your tests with mocha-parallel-tests

2.0.2

  • fix: subprocess stderr is not shown if the subprocess crashes before sending the "start" event (#158)
  • fix: --retries and --bail option conflict with each other causing mohcha-parallel-tests own assertion error (#163)

2.0.1

  • fix: reporter doesn't show anything until all tests are finished (#145)
  • fix: wrong assertion error when subprocess crashes before sending test results (#147)
  • fix: --exit option was not working as expected (#146)
  • fix: wrong assertion error when --retries option is used and all retries fail (#143)
  • fix: events order is not following mocha style. This fixes mochawesome reporter behaviour (#113)
  • mocha-allure-reporter is supported (#80)

2.0.0

v2 is a completely new version of mocha-parallel-tests rewritten in TypeScript from scratch. Its main focus is to simplify parallel run of mocha tests: while previously they were executed in one single process now each file is executed in a separate process.

Some of the main changes are:

  • breaking change: each file is now executed in a separate process
  • breaking change: minimum supported node version is 8 because it's current LTS and because of performance reasons
  • breaking change: main exported file should now be imported as require("mocha-parallel-tests").default if you're using CommonJS modules
  • breaking change: --retry is not supported anymore: mocha-parallel-tests main target is to be 100%-compliant with mocha in terms of API and to not introduce its own options and APIs other than --max-parallel
  • breaking change: reporter output/stats now contains one more level for each file
  • change: --max-parallel option is os.cpus().length by default. You can also specify it manually or set it to 0 which means "immediately launch as many processes as the number of files"
  • new: --delay option is now supported for each subprocess
  • new: --retries option is now supported for each subprocess
  • new: supported peerDependencies versions of mocha are not 3, 4 and 5
  • new: all tests are now executed against all supported mocha versions
  • new: mocha-parallel-tests install should work fine on windows
  • new: TypeScript definitions are now provided in package.json

Read more about new release here: https://github.com/mocha-parallel/mocha-parallel-tests/wiki/v2-release-notes

1.2.10

  • fix: support --no-timeouts option (#120)

1.2.9

  • fix: support only latest (3.x) mocha versions (#98)
  • fix: support reporter.done() callback for all external reporters (mochawesome for example) in both CLI and node.js API (#113)

1.2.8

  • fix: do not fail if no target is set and --recursive option is used (#94)

1.2.7

  • fix: add support for xdescribe, xcontext, xspecify, xit and it.skip (#102)
  • most of packages dependencies upgrade (minor changes)

1.2.6

  • fix: tests with binary dependencies can now be used with mocha-parallel-tests (#100)

1.2.5

  • fix: if file contains multiple describe() sections in it, one of it fails and mocha-parallel-tests is executed with --retry option, only this failed section is re-executed (#77)

1.2.4

  • fix: --no-exit CLI option is supported (#85)

1.2.3

  • fix: pwd-based reporters (located somewhere inside your repo) are supported (#75)
  • fix: tap and other reporters which use mocha's runner object are supported (#78)
  • fix: mocha-jenkins-reporteris supported (#81)

1.2.2

  • fix: selenium-webdriver issue with number of tests is fixed (#69). Currently all selenium-webdriver tests should work as expected.

1.2.1

  • fix: --require'd files are working as expected together with --compilers option (#63)

1.2.0

  • new: using mocha-parallel-tests programmatically is now supported. You can also pass own mocha-parallel-tests options by calling setOwnOptions({maxParallel, retry}) method of mocha-parallel-tests instance. You should call it before run().

1.1.x

  • breaking change: mocha is now in peerDependencies part of mocha-parallel-tests which means that you need to add both mocha and mocha-parallel-tests in your project's package.json. This also means that you can run both of your tests: written for mocha@2.x and mocha@3.x (#55)
  • fix: support for only() and skip() helpers (#59)
  • fix: main package file now refers to CommonJS file, not ES6 module.

Version number is 1.1.0 because first mocha-parallel-tests releases had 1.0.x versioning scheme.

0.5.x

  • new: mutiple test suites (describe) inside one file run in parallel
  • fix: support mocha --compilers option (#53). 0.4.x lacked support for JavaScript files.
  • fix: support mocha --require option
  • fix: support mocha.opts config file (#44)
  • fix: global hooks stored inside separate files are now executed if target is directory (#41)
  • fix: global hooks stored inside separate files are now executed (#39)

0.4.x

  • new: support for mocha --compilers option introduced
  • fix: tests using --max-parallel=1 run without errors (#34)
  • fix: reporters with own options like xunit don't produce errors now (#31)
  • fix: --retry option is now working for mocha test hooks (before/after/etc)
  • mocha-parallel-tests code is now ES2015-compatible

0.3.x

  • fix: runner displays right tests execution time now (#24)
  • fix: exit codes are now the same as in original mocha runner (#27)
  • bump mocha version to 2.4.5. Check its changelog for more info. Previous version was 2.3.4.

0.2.x

  • new: --retry <num> to set number of retries for failing tests (0 by default)

0.1.x

  • fix: console.log|errors from tests do not cross with each other. Instead they are waiting until proper test starts executing :smiley:
  • fix: support stack trace of errors
  • fix: support for incoming array of files
  • fix: --max-parallel option introduced bug when some of the files were not tests: not all tests could run
  • new: --max-parallel <num> to set max number of running parallel tests

0.0.x

  • fix emits of end events
  • tests for package
  • changed readme and refactoring
  • reporter control added
  • first release :smiley: