Skip to content

Commit

Permalink
mochajs#5084 global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
CheadleCheadle committed Feb 8, 2024
1 parent 53a4baf commit 02f5a97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
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);
});
});

0 comments on commit 02f5a97

Please sign in to comment.