Skip to content

🚀 Feature: Add a global variable in tests indicating the test runner name and version #5084

Open
@JoshuaKGoldberg

Description

Feature Request Checklist

Overview

Mocha right now adds standard test hooks to globalThis: after, describe, it, and so on. But there's nothing indicating that it's specifically Mocha being run, and not some other test framework. Would it be reasonable to add some variable like mocha to the global scope?

With this global variable, testing utilities would be able to statically determine the test framework (Mocha) without being explicitly told it in their configuration. This can be useful for utilities that hook into framework-specific APIs.

Suggested Solution

Object.defineProperty(globalThis, "mocha", {
  get: () => ({
    name: "mocha",
    version: "12.34.56"
  }),
});

Alternatives

A process.env would work on the CLI / in Node.js, but not in all browser environments.

Adding some string to a pre-existing global like after might work too, but seems hacky. Right now after.toString() comes out to "function (name, fn) {\n suites[0].afterAll(name, fn);\n }".

Additional Info

See JoshuaKGoldberg/console-fail-test#10: https://github.com/JoshuaKGoldberg/console-fail-test is a utility that runs during tests and uses the test framework's APIs to call testing hooks. It can sort of dynamically detect Mocha, but the implementation is brittle.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    status: in triagea maintainer should (re-)triage (review) this issuetype: featureenhancement proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions