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

#5084 global variable #5089

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
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
})
});
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);
});
});