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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
JoshuaKGoldberg opened this issue Jan 19, 2024 · 3 comments · May be fixed by #5089
Open
3 tasks done
Labels
status: in triage a maintainer should (re-)triage (review) this issue type: feature enhancement proposal

Comments

@JoshuaKGoldberg
Copy link
Member

JoshuaKGoldberg commented Jan 19, 2024

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.

@JoshuaKGoldberg JoshuaKGoldberg added the type: feature enhancement proposal label Jan 19, 2024
@JoshuaKGoldberg JoshuaKGoldberg changed the title 🚀 Feature: Add a global variable in tests indicating the test runner name (Mocha) 🚀 Feature: Add a global variable in tests indicating the test runner name and version Feb 7, 2024
CheadleCheadle added a commit to CheadleCheadle/mocha that referenced this issue Feb 8, 2024
@CheadleCheadle CheadleCheadle linked a pull request Feb 8, 2024 that will close this issue
3 tasks
@CheadleCheadle
Copy link

CheadleCheadle commented Feb 8, 2024

Hey @JoshuaKGoldberg,
I've submitted a pull request for this. Any feedback is much appreciated!

@JoshuaKGoldberg
Copy link
Member Author

Ah, thanks @CheadleCheadle - but this issue hasn't been marked as accepted 😦. We generally only accept feature-adding PRs for issues marked as status: accepting prs. I filed this just a feature request, and am waiting the other maintainers to triage it.

We've been talking a bit on having a separate label to indicate "this is still in triage" for issues. I filed #5092 to make it. Sorry that the docs aren't super clear on this yet!

@JoshuaKGoldberg JoshuaKGoldberg added the status: in triage a maintainer should (re-)triage (review) this issue label Feb 9, 2024
@CheadleCheadle
Copy link

Thanks @JoshuaKGoldberg for the clarification on the PR requirements. We'll put a pin in this one for now!

@JoshuaKGoldberg JoshuaKGoldberg modified the milestone: v11.0.0 Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: in triage a maintainer should (re-)triage (review) this issue type: feature enhancement proposal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants