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.
This commit is contained in:
Steve Gravrock
2023-06-05 19:44:06 -07:00
parent d745d6b5f0
commit 8e0f0e8e8c

View File

@@ -6,11 +6,11 @@
const jasmineRequire = require('./jasmine-core/jasmine.js'); const jasmineRequire = require('./jasmine-core/jasmine.js');
module.exports = jasmineRequire; module.exports = jasmineRequire;
const bootOnce = (function() { const boot = (function() {
let jasmine, jasmineInterface; let jasmine, jasmineInterface;
return function bootWithoutGlobals() { return function bootWithoutGlobals(reinitialize) {
if (!jasmineInterface) { if (!jasmineInterface || reinitialize === true) {
jasmine = jasmineRequire.core(jasmineRequire); jasmine = jasmineRequire.core(jasmineRequire);
const env = jasmine.getEnv({ suppressLoadErrors: true }); const env = jasmine.getEnv({ suppressLoadErrors: true });
jasmineInterface = jasmineRequire.interface(jasmine, env); 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}. * 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} * @type {function}
* @return {jasmine} * @return {jasmine}
*/ */
module.exports.boot = function() { module.exports.boot = function(reinitialize) {
const {jasmine, jasmineInterface} = bootOnce(); const {jasmine, jasmineInterface} = boot(reinitialize);
for (const k in jasmineInterface) { for (const k in jasmineInterface) {
global[k] = jasmineInterface[k]; global[k] = jasmineInterface[k];
@@ -39,13 +41,14 @@ module.exports.boot = function() {
/** /**
* Boots a copy of Jasmine and returns an object containing the properties * 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 * 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 * @example
* const {describe, beforeEach, it, expect, jasmine} = require('jasmine-core').noGlobals(); * const {describe, beforeEach, it, expect, jasmine} = require('jasmine-core').noGlobals();
*/ */
module.exports.noGlobals = function() { module.exports.noGlobals = function(reinitialize) {
const {jasmineInterface} = bootOnce(); const {jasmineInterface} = boot(reinitialize);
return jasmineInterface; return jasmineInterface;
}; };