-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
base: main
Are you sure you want to change the base?
Conversation
Marking as draft pending triage of #5084. Thanks for sending it in the meantime! ❤️ |
Oop, I missed that #5084 was marked as accepting PRs. Un-drafting this 🚀 |
There was a problem hiding this 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?
There was a problem hiding this comment.
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?
Object.defineProperty(globalThis, 'mochaVar', { | ||
8000 | get: () => ({ | |
name, | ||
version | ||
}) | ||
}); |
There was a problem hiding this comment.
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:
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); | |
} |
👋 @CheadleCheadle just checking in, is this still something you have time for? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds a new global property, mochaVar, to provide access to Mocha’s package name and version, enabling external tools to detect the testing framework and its version.
- Introduces a global, read-only property "mochaVar" in lib/context.js.
- Adds an integration test to verify the global mochaVar’s properties in test/integration/global-variable.spec.js.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
test/integration/global-variable.spec.js | Adds tests to confirm that globalThis.mochaVar includes name and version |
lib/context.js | Defines globalThis.mochaVar as a read-only object exposing Mocha details |
@@ -0,0 +1,11 @@ | |||
'use strict'; | |||
var mochaPackageJson = require('../../package.json'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using 'const' instead of 'var' for importing the package.json file, as the value is not reassigned.
var mochaPackageJson = require('../../package.json'); | |
const mochaPackageJson = require('../../package.json'); |
Copilot uses AI. Check for mistakes.
var mochaPackageJson = require('../package.json'); | ||
var version = mochaPackageJson.version; | ||
var name = mochaPackageJson.name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace 'var' with 'const' for declaring 'mochaPackageJson' and consider doing the same for 'version' and 'name' to better express their immutability.
var mochaPackageJson = require('../package.json'); | |
var version = mochaPackageJson.version; | |
var name = mochaPackageJson.name; | |
const mochaPackageJson = require('../package.json'); | |
const version = mochaPackageJson.version; | |
const name = mochaPackageJson.name; |
Copilot uses AI. Check for mistakes.
PR Checklist
status: accepting prs
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.
