Skip to content

feat: add a global mocha variable #5089

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

CheadleCheadle
Copy link

@CheadleCheadle CheadleCheadle commented Feb 8, 2024

PR Checklist

Overview

Mocha now has the mochaVar (open to different name) property in the global scope, including its current name and version. This global property enables external testing utilities to statically identify both the version of Mocha in use and the specific testing framework being utilized.
mochavar

@JoshuaKGoldberg
Copy link
Member

Marking as draft pending triage of #5084. Thanks for sending it in the meantime! ❤️

@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as draft February 9, 2024 12:26
@coveralls
Copy link

Coverage Status

coverage: 94.343% (+0.008%) from 94.335%
when pulling 2bd1301 on CheadleCheadle:global-object
into f9e87d6 on mochajs:master.

@JoshuaKGoldberg JoshuaKGoldberg added the status: blocked Waiting for something else to be resolved label Mar 4, 2024
@JoshuaKGoldberg
Copy link
Member

Oop, I missed that #5084 was marked as accepting PRs. Un-drafting this 🚀

@JoshuaKGoldberg JoshuaKGoldberg removed the status: blocked Waiting for something else to be resolved label Apr 7, 2025
@JoshuaKGoldberg JoshuaKGoldberg marked this pull request as ready for review April 7, 2025 17:41
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very reasonable to me, thanks for the patience on it not being reviewed for a while! Just a couple small changes requested -really only one behavioral one- WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated permissions change, could you please revert?

Comment on lines +102 to +107
Object.defineProperty(globalThis, 'mochaVar', {
get: () => ({
name,
version
})
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for most JS environments, but some sandboxed ones disallow changing globalThis and might even throw. https://github.com/endojs/endo exists today; https://github.com/tc39/proposal-shadowrealm will hopefully exist eventually.

Let's wrap this in a try/catch the same way Error.stackTraceLimit = Infinity; is wrapped:

mocha/lib/cli/cli.js

Lines 43 to 47 in 3f13bbc

try {
Error.stackTraceLimit = Infinity; // configurable via --stack-trace-limit?
} catch (err) {
debug('unable to set Error.stackTraceLimit = Infinity', err);
}

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP or other posters - more information needed label Apr 7, 2025
@JoshuaKGoldberg JoshuaKGoldberg changed the title #5084 global variable feat: add a global mocha variable Apr 7, 2025
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 or other posters - more information needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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