Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug: --(f)grep causes mochaHooks to not run #5005

Open
4 tasks done
JakobJingleheimer opened this issue Aug 18, 2023 · 3 comments
Open
4 tasks done

馃悰 Bug: --(f)grep causes mochaHooks to not run #5005

JakobJingleheimer opened this issue Aug 18, 2023 · 3 comments
Labels
status: waiting for author waiting on response from OP - more information needed

Comments

@JakobJingleheimer
Copy link

JakobJingleheimer commented Aug 18, 2023

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_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

When a setup file is specified (--require './test/mocha.setup.mjs) that exports mochaHooks and --(f)grep is also specified, the setup file is run but mochaHooks are ignored.

I'm using the setup file (among other things) to set up global module mocks (courtesy of testdouble) in a beforeAll. When the mocks are not set, the tests explode (ex IndexedDB doesn't exist in node).

Steps to Reproduce

  1. Create a setup file with mochaHooks, like:
exports const mochaHooks = {
async beforeAll() {
  throw new Error('root beforeAll has run');
},
};
  1. Create a dummy test file with at least a describe + label ('example')
  2. Run mocha with --require and --(f)grep (ex npm run test:units -- --fgrep 'example'
  3. Notice the error was not thrown.
  4. Re-run the command without the --(f)grep
  5. Notice the error is thrown

Expected behavior: mochaHooks are run

Actual behavior: mochaHooks are ignored

Reproduces how often: 100%

Versions

  • The output of mocha --version and node_modules/.bin/mocha --version: 10.2.0
  • The output of node --version: 18.12.0
  • Your operating system
    • name and version: macOS Ventura
    • architecture (32 or 64-bit): arm64
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash?
  • Your browser and version (if running browser tests): N/A
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version): esbuild 0.18.12

Additional Information

@JakobJingleheimer
Copy link
Author

JakobJingleheimer commented Aug 18, 2023

I initially thought the --(f)grep was causing the setup file to get excluded (which might make sense and thus be designed behaviour), but the file itself is run, so I can't see a reason mochaHooks would not.

I searched through the mocha docs, but I couldn't find anything about the actual behaviour, so I think it's a bug.

@JakobJingleheimer
Copy link
Author

Actually, it appears to be all CLI flags passed like npm run foo -- --fgrep 'My Test'

@JoshuaKGoldberg JoshuaKGoldberg changed the title --(f)grep causes mockaHooks to not run 馃悰 Bug: --(f)grep causes mockaHooks to not run Dec 27, 2023
@JoshuaKGoldberg JoshuaKGoldberg changed the title 馃悰 Bug: --(f)grep causes mockaHooks to not run 馃悰 Bug: --(f)grep causes mochaHooks to not run Feb 6, 2024
@JoshuaKGoldberg JoshuaKGoldberg added the status: in triage a maintainer should (re-)triage (review) this issue label Feb 9, 2024
@JoshuaKGoldberg
Copy link
Member

馃 I can't reproduce this. Using the hello-world example from mochajs/mocha-examples#73:

mocha-examples/packages/hello-world $ npm run test -- --require hooks.js --grep e

> example-browser@1.0.0 test
> mocha test.spec.js --require hooks.js --grep e

  1) "before all" hook: beforeAll in "{root}"

  0 passing (1ms)
  1 failing

  1) "before all" hook: beforeAll in "{root}":
     Error: root beforeAll has run
      at Context.beforeAll (hooks.js:3:11)
      at process.processImmediate (node:internal/timers:478:21)
mocha-examples/packages/hello-world $ npm run test -- --require hooks.js --fgrep e

> example-browser@1.0.0 test
> mocha test.spec.js --require hooks.js --fgrep e

  1) "before all" hook: beforeAll in "{root}"

  0 passing (1ms)
  1 failing

  1) "before all" hook: beforeAll in "{root}":
     Error: root beforeAll has run
      at Context.beforeAll (hooks.js:3:11)
      at process.processImmediate (node:internal/timers:478:21)

Could you provide a standalone reproduction please?

@JoshuaKGoldberg JoshuaKGoldberg added status: waiting for author waiting on response from OP - more information needed and removed status: in triage a maintainer should (re-)triage (review) this issue labels Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed
Projects
None yet
Development

No branches or pull requests

2 participants