Skip to content

Commit 02f5a97

Browse files
mochajs#5084 global variable
1 parent 53a4baf commit 02f5a97

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

bin/mocha.js

100644100755
File mode changed.

lib/context.js

+21
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,24 @@ Context.prototype.retries = function (n) {
8484
this.runnable().retries(n);
8585
return this;
8686
};
87+
88+
/**
89+
* Defines a global read-only property `mochaVar` that provides access to Mocha's name and version.
90+
* It is accessible globally within the Node.js environment or in the browser
91+
* @example
92+
* console.log(globalThis.mochaVar); // Outputs: { name: "mocha", version: "X.Y.Z" }
93+
*
94+
* @property {Object} mochaVar - The global property containing Mocha's name and version.
95+
* @property {string} mochaVar.name - The name of the Mocha package.
96+
* @property {string} mochaVar.version - The current version of the Mocha package.
97+
*/
98+
var mochaPackageJson = require('../package.json');
99+
var version = mochaPackageJson.version;
100+
var name = mochaPackageJson.name;
101+
102+
Object.defineProperty(globalThis, 'mochaVar', {
103+
get: () => ({
104+
name,
105+
version
106+
})
107+
});
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
var mochaPackageJson = require('../../package.json');
3+
var version = mochaPackageJson.version;
4+
var name = mochaPackageJson.name;
5+
6+
describe('Global "mocha" object', function () {
7+
it('should have the properties name and version', function () {
8+
expect(globalThis.mochaVar.name, 'to equal', name);
9+
expect(globalThis.mochaVar.version, 'to equal', version);
10+
});
11+
});

0 commit comments

Comments
 (0)