From eeb7daedc246dfa3eebd72a284843668c52fdea1 Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Fri, 28 Aug 2020 15:42:39 -0700 Subject: [PATCH] implementation of global setup/teardown; closes #4308 --- .eslintignore | 1 + .wallaby.js | 3 +- docs/index.md | 526 +++-- lib/cli/run-helpers.js | 71 +- lib/cli/run.js | 14 +- lib/cli/watch-run.js | 79 +- lib/errors.js | 97 +- lib/mocha.js | 300 ++- lib/nodejs/parallel-buffered-runner.js | 18 + lib/nodejs/worker.js | 17 +- lib/plugin-loader.js | 286 +++ lib/runner.js | 253 ++- lib/utils.js | 31 +- package-lock.json | 1243 ++--------- package.json | 4 +- test/assertions.js | 275 ++- test/browser-specific/setup.js | 2 + .../global-setup-teardown-multiple.fixture.js | 20 + .../global-setup-teardown.fixture.js | 10 + .../global-fixtures/global-setup.fixture.js | 6 + .../global-teardown.fixture.js | 5 + .../root-hooks}/esm/package.json | 0 .../esm/root-hook-defs-esm.fixture.js | 0 .../root-hooks}/root-hook-defs-a.fixture.js | 0 .../root-hooks}/root-hook-defs-b.fixture.js | 0 .../root-hooks}/root-hook-defs-c.fixture.js | 0 .../root-hooks}/root-hook-defs-d.fixture.js | 0 .../root-hook-defs-esm-broken.fixture.js | 0 .../root-hook-defs-esm.fixture.mjs | 0 .../root-hooks}/root-hook-test-2.fixture.js | 0 .../root-hooks}/root-hook-test.fixture.js | 0 test/integration/helpers.js | 546 +++-- test/integration/hook-err.spec.js | 4 +- test/integration/hooks.spec.js | 4 +- test/integration/multiple-runs.spec.js | 31 +- test/integration/options/extension.spec.js | 4 +- test/integration/options/watch.spec.js | 196 +- test/integration/pending.spec.js | 4 +- .../plugins/global-fixtures.spec.js | 293 +++ .../root-hooks.spec.js} | 44 +- test/integration/retries.spec.js | 4 +- test/node-unit/cli/run-helpers.spec.js | 79 +- test/node-unit/mocha.spec.js | 37 +- test/node-unit/worker.spec.js | 28 +- test/reporters/xunit.spec.js | 28 +- test/setup.js | 2 + test/unit/mocha.spec.js | 436 +++- test/unit/plugin-loader.spec.js | 514 +++++ test/unit/runner.spec.js | 1842 +++++++++-------- test/unit/utils.spec.js | 49 + 50 files changed, 4422 insertions(+), 2984 deletions(-) create mode 100644 lib/plugin-loader.js create mode 100644 test/integration/fixtures/plugins/global-fixtures/global-setup-teardown-multiple.fixture.js create mode 100644 test/integration/fixtures/plugins/global-fixtures/global-setup-teardown.fixture.js create mode 100644 test/integration/fixtures/plugins/global-fixtures/global-setup.fixture.js create mode 100644 test/integration/fixtures/plugins/global-fixtures/global-teardown.fixture.js rename test/integration/fixtures/{options/require => plugins/root-hooks}/esm/package.json (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/esm/root-hook-defs-esm.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-a.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-b.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-c.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-d.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-esm-broken.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-defs-esm.fixture.mjs (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-test-2.fixture.js (100%) rename test/integration/fixtures/{options/require => plugins/root-hooks}/root-hook-test.fixture.js (100%) create mode 100644 test/integration/plugins/global-fixtures.spec.js rename test/integration/{options/require.spec.js => plugins/root-hooks.spec.js} (81%) create mode 100644 test/unit/plugin-loader.spec.js diff --git a/.eslintignore b/.eslintignore index a7a33c25c6..bf70c4ebbe 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2,6 +2,7 @@ coverage/ mocha.js *.fixture.js docs/_site +docs/api docs/_dist docs/example out/ diff --git a/.wallaby.js b/.wallaby.js index b23a244650..59083bb5f4 100644 --- a/.wallaby.js +++ b/.wallaby.js @@ -48,6 +48,7 @@ module.exports = () => { ); require('./test/setup'); }, - debug: true + debug: true, + runMode: 'onsave' }; }; diff --git a/docs/index.md b/docs/index.md index b8396ea39e..ebe5f86ed1 100644 --- a/docs/index.md +++ b/docs/index.md @@ -78,9 +78,9 @@ In your editor: ```js var assert = require('assert'); -describe('Array', function () { - describe('#indexOf()', function () { - it('should return -1 when the value is not present', function () { +describe('Array', function() { + describe('#indexOf()', function() { + it('should return -1 when the value is not present', function() { assert.equal([1, 2, 3].indexOf(4), -1); }); }); @@ -116,51 +116,65 @@ $ npm test ## Run Cycle Overview -A brief outline on the order Mocha's components are executed. -Worth noting that all hooks, `describe` and `it` callbacks are run in the order they are defined (i.e. found in the file). - -``` -run 'mocha spec.js' -| -spawn child process -| -|--------------> inside child process - process and apply options - | - run spec file/s - | - |--------------> per spec file - suite callbacks (e.g., 'describe') - | - 'before' root-level pre-hook - | - 'before' pre-hook - | - |--------------> per test - 'beforeEach' root-level pre-hook - | - 'beforeEach' pre-hook - | - test callbacks (e.g., 'it') - | - 'afterEach' post-hook - | - 'afterEach' root-level post-hook - |<-------------- per test end - | - 'after' post-hook - | - 'after' root-level post-hooks - |<-------------- per spec file end -|<-------------- inside child process end -``` +> Updated for v9.0.0. + +The following is a mid-level outline of Mocha's "flow of execution" when run in Node.js; the "less important" details have been omitted. + +In a browser, test files are loaded by `