mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.55k stars 3.01k forks source link

🐛 Bug: Some Settings Cannot Be Inherited (extends keyword) #4979

Open manuth opened 1 year ago

manuth commented 1 year ago

Prerequisites

Description

Currently, mocha will load default settings from lib/mocharc.json from this package and only after doing so process the configuration using yargs.

This breaks yargs extends mechanism as the values predefined in said mocharc.json file cannot be overridden.

This means that following settings cannot be specified using the extends config:

I assume that arrays still do work as, to my understanding, yargs will concatenate arrays taken from extends configs.

Steps to Reproduce

  1. Create a project with mocha tests written using the tdd ui
  2. In your project's root, create a file base.json with the following content:
    {
        "ui": "tdd"
    }
  3. In your project's root, create a file called .mocharc.json:
    {
        "timeout": 10000
    }
    1. Try to run your tdd tests by running the mocha command

Expected behavior:

mocha should be able to execute this test as the setting "ui": "tdd" is supposed to be inherited.

Actual behavior: [What actually happens]

mocha will instead show the following error message:

ReferenceError: suite is not defined

Reproduces how often: 100%

Versions

Additional Information

Notice that default values from this package's lib/mocharc.json file are applied to the current config file during the execution of import("mocha/lib/cli/options").loadOptions() in this piece of code:

https://github.com/mochajs/mocha/blob/37deed262d4bc0788d32c66636495d10038ad398/lib/cli/options.js#L246

Only after invoking loadOptions(), the extends setting is processed by yargs:

Related to this question: #3916 There is also a PR addressing this issue: #4407 Furthermore, I created a PR myself: #4980

JoshuaKGoldberg commented 7 months ago

In your project's root, create a file base.json

This confuses me. Nothing in the .mocharc.json references extending base.json. So it makes sense that the values in base.json wouldn't be used.

Was the repro meant to have .mocharc.json look like this?

{
  "extends": "./base.json",
  "timeout": 10000
}

The PR makes sense at first glance, and I can reproduce the buggy behavior by adding in the missing "extends". But I just want to be sure. 🙂

JoshuaKGoldberg commented 1 month ago

👋 ping @manuth, is this still on your radar?