question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Root-level before() and after() hooks not running on each suite

See original GitHub issue

Prerequisites

  • Checked that your issue hasn’t already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn’t just a feature that actually isn’t supported in the environment in question or a bug in your code.
  • ‘Smoke tested’ the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

The run cycle overview in the documentation states that root-level before()/after() hooks should run before and after every test suite, just like the beforeEach()/afterEach() hooks run before and after every test. However, only the beforeEach() and afterEach() hooks actually work as described; there is no way to register hooks to be run before and after every test suite.

Steps to Reproduce

// test.js
const assert = require('assert');
const runs = [];
before(function() {
  runs.push('root');
})

describe("Suite 1", function() {
  before(function() {
    runs.push('suite1');
  });
  it("root has run once", function() {
    assert.deepStrictEqual(runs, ['root','suite1']);
  });
  it("root has still run once", function() {
    assert.deepStrictEqual(runs, ['root','suite1']);
  });
});

describe("suite 2", function() {
  before(function() {
    runs.push('suite2');
  });
  it("root has run twice", function() {
    assert.deepStrictEqual(runs, ['root','suite1','root','suite2']);
    assert.equal(runs[0],2);
  });
});

Expected behavior: mocha test.js passes

Actual behavior:

  Suite 1
    ✓ root has run once
    ✓ root has still run once

  suite 2
    1) root has run twice


  2 passing (7ms)
  1 failing

  1) suite 2
       root has run twice:

      AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

  [
    'root',
    'suite1',
-   'root',
    'suite2'
  ]
      + expected - actual

       [
         "root"
         "suite1"
      +  "root"
         "suite2"
       ]
      
      at Context.<anonymous> (test.js:24:12)
      at processImmediate (internal/timers.js:439:21)

Reproduces how often: Always

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.2.1
  • The output of node --version: v12.10.0
  • Your operating system
    • name and version: MacOS 10.14.6
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
  • Your browser and version (if running browser tests): N/A
  • Any third-party Mocha-related modules (and their versions): N/A
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): N/A

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
stevenvachoncommented, Jul 22, 2020

Same with:

mocha.rootHooks({
  afterAll: () => {}, // after all suites
  afterEach: () => {} // after each test
});

There is no after for “after each suite”.

1reaction
ericzeredcommented, Nov 7, 2019

Hello, I think I have an issue like this : I’m using mocha with webdriverIO tests. I have several specs file (‘test.spec.ts’). Inside each spec file, I have several ‘describe’ and inside each ‘describe’, I have several ‘it’.

I’m looking for a way to execute something before each ‘describe’ (so ‘suite’ if I’m reading the doc correctly). So I put actions in “beforeSuite” in the “wdio.conf.js” file, but it’s executed only when the spec file start.

And I’m looking for a way to retry a ‘describe’ when a test is failing, and right now I’m only able to retry an ‘it’ or an entire specs file, not a ‘describe’ suite. What conf should I do ? Thanks a lot

Read more comments on GitHub >

github_iconTop Results From Across the Web

mocha.js - Root level `beforeEachFile` and `afterEachFile` - Stack ...
I am trying to do a root-level before that runs before each test file, and after each test file completes. So in other...
Read more >
Mocha — Global Setup and Teardown (before/after)
Mocha offer two ways for this: global hooks; root-level hooks in single test files that run before tests or beforeEach individual test. Global ......
Read more >
Be careful when running all specs together - Gleb Bahmutov
Be wary of placing before or beforeEach hooks at the root level, instead prefer moving them into describe and context suites. This isolates...
Read more >
Synchronous code, arrow functions and hooks - mocha
describe('hooks', function() { before(function() { // will run before all tests in this block }); after(function() { // will run after all ......
Read more >
the fun, simple, flexible JavaScript test framework - Mocha
Hooks will run in the order they are defined, as appropriate; all before() hooks run (once), then any beforeEach() hooks, tests, any afterEach()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found