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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bin/mocha.js
100644 → 100755
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?

Empty file.
21 changes: 21 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,24 @@ Context.prototype.retries = function (n) {
this.runnable().retries(n);
return this;
};

/**
* Defines a global read-only property `mochaVar` that provides access to Mocha's name and version.
* It is accessible globally within the Node.js environment or in the browser
* @example
* console.log(globalThis.mochaVar); // Outputs: { name: "mocha", version: "X.Y.Z" }
*
* @property {Object} mochaVar - The global property containing Mocha's name and version.
* @property {string} mochaVar.name - The name of the Mocha package.
* @property {string} mochaVar.version - The current version of the Mocha package.
*/
var mochaPackageJson = require('../package.json');
var version = mochaPackageJson.version;
var name = mochaPackageJson.name;

Object.defineProperty(globalThis, 'mochaVar', {
get: () => ({
name,
version
})
});
Comment on lines +102 to +107
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);
}

11 changes: 11 additions & 0 deletions test/integration/global-variable.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
var mochaPackageJson = require('../../package.json');
var version = mochaPackageJson.version;
var name = mochaPackageJson.name;

describe('Global "mocha" object', function () {
it('should have the properties name and version', function () {
expect(globalThis.mochaVar.name, 'to equal', name);
expect(globalThis.mochaVar.version, 'to equal', version);
});
});