From 8e0f0e8e8c16d6bc75769a46a7a4cade1da26531 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Mon, 5 Jun 2023 19:44:06 -0700 Subject: [PATCH] Optionally restore the pre-5.0 behavior of boot() always creating a new instance This is needed by jasmine-npm (and likely other tools like it) that may need to create and use multiple envs in sequence. --- lib/jasmine-core.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/jasmine-core.js b/lib/jasmine-core.js index 51ea0d89..e20680c4 100644 --- a/lib/jasmine-core.js +++ b/lib/jasmine-core.js @@ -6,11 +6,11 @@ const jasmineRequire = require('./jasmine-core/jasmine.js'); module.exports = jasmineRequire; -const bootOnce = (function() { +const boot = (function() { let jasmine, jasmineInterface; - return function bootWithoutGlobals() { - if (!jasmineInterface) { + return function bootWithoutGlobals(reinitialize) { + if (!jasmineInterface || reinitialize === true) { jasmine = jasmineRequire.core(jasmineRequire); const env = jasmine.getEnv({ suppressLoadErrors: true }); jasmineInterface = jasmineRequire.interface(jasmine, env); @@ -22,12 +22,14 @@ const bootOnce = (function() { /** * Boots a copy of Jasmine and returns an object as described in {@link jasmine}. - * If boot is called multiple times, the same object is returned every time. + * If boot is called multiple times, the same object is returned every time + * unless true is passed. + * @param {boolean} [reinitialize=false] Whether to create a new copy of Jasmine if one already exists * @type {function} * @return {jasmine} */ -module.exports.boot = function() { - const {jasmine, jasmineInterface} = bootOnce(); +module.exports.boot = function(reinitialize) { + const {jasmine, jasmineInterface} = boot(reinitialize); for (const k in jasmineInterface) { global[k] = jasmineInterface[k]; @@ -39,13 +41,14 @@ module.exports.boot = function() { /** * Boots a copy of Jasmine and returns an object containing the properties * that would normally be added to the global object. If noGlobals is called - * multiple times, the same object is returned every time. + * multiple times, the same object is returned every time unless true is passed. * + * @param {boolean} [reinitialize=false] Whether to create a new copy of Jasmine if one already exists * @example * const {describe, beforeEach, it, expect, jasmine} = require('jasmine-core').noGlobals(); */ -module.exports.noGlobals = function() { - const {jasmineInterface} = bootOnce(); +module.exports.noGlobals = function(reinitialize) { + const {jasmineInterface} = boot(reinitialize); return jasmineInterface; };