From 02f5a97d866ab5ad9dac9705f0124d3e495401cf Mon Sep 17 00:00:00 2001 From: CheadleCheadle Date: Wed, 7 Feb 2024 22:26:14 -0800 Subject: [PATCH] #5084 global variable --- bin/mocha.js | 0 lib/context.js | 21 +++++++++++++++++++++ test/integration/global-variable.spec.js | 11 +++++++++++ 3 files changed, 32 insertions(+) mode change 100644 => 100755 bin/mocha.js create mode 100644 test/integration/global-variable.spec.js diff --git a/bin/mocha.js b/bin/mocha.js old mode 100644 new mode 100755 diff --git a/lib/context.js b/lib/context.js index 388d308272..a6ea6f83ca 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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 + }) +}); diff --git a/test/integration/global-variable.spec.js b/test/integration/global-variable.spec.js new file mode 100644 index 0000000000..ce26fec2ec --- /dev/null +++ b/test/integration/global-variable.spec.js @@ -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); + }); +});