8000 feat: add a global mocha variable by CheadleCheadle · Pull Request #5089 · mochajs/mocha · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
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
8000
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
@JoshuaKGoldberg
Copy link
Member

👋 @CheadleCheadle just checking in, is this still something you have time for?

@JoshuaKGoldberg JoshuaKGoldberg added the stale this has been inactive for a while... label Jun 11, 2025
@voxpelli voxpelli requested a review from Copilot June 12, 2025 07:46
Copy link
@Copilot Copilot AI left a 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');
Copy link
Preview
Copilot AI Jun 12, 2025

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.

Suggested change
var mochaPackageJson = require('../../package.json');
const mochaPackageJson = require('../../package.json');

Copilot uses AI. Check for mistakes.

Comment on lines +98 to +100
var mochaPackageJson = require('../package.json');
var version = mochaPackageJson.version;
var name = mochaPackageJson.name;
Copy link
Preview
Copilot AI Jun 12, 2025

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.

Suggested change
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale this has been inactive for a while... 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
0