diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 4504ab8a..e058f6a5 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -90,7 +90,7 @@ var getJasmineRequireObj = (function(jasmineGlobal) { j$ ); j$.ReportDispatcher = jRequire.ReportDispatcher(j$); - j$.RunnableResources = jRequire.RunnableResources(j$); + j$.RunableResources = jRequire.RunableResources(j$); j$.Spec = jRequire.Spec(j$); j$.Spy = jRequire.Spy(j$); j$.SpyFactory = jRequire.SpyFactory(j$); @@ -1113,15 +1113,15 @@ getJasmineRequireObj().Env = function(j$) { new j$.MockDate(global) ); - const runnableResources = new j$.RunnableResources(function() { - const r = currentRunnable(); + const runableResources = new j$.RunableResources(function() { + const r = currentRunable(); return r ? r.id : null; }); let topSuite; let currentSpec = null; const currentlyExecutingSuites = []; - const focusedRunnables = []; + const focusedRunables = []; let currentDeclarationSuite = null; let hasFailures = false; let deprecator; @@ -1231,7 +1231,7 @@ getJasmineRequireObj().Env = function(j$) { return currentlyExecutingSuites[currentlyExecutingSuites.length - 1]; } - function currentRunnable() { + function currentRunable() { return currentSpec || currentSuite(); } @@ -1319,27 +1319,27 @@ getJasmineRequireObj().Env = function(j$) { }; this.setDefaultSpyStrategy = function(defaultStrategyFn) { - runnableResources.setDefaultSpyStrategy(defaultStrategyFn); + runableResources.setDefaultSpyStrategy(defaultStrategyFn); }; this.addSpyStrategy = function(name, fn) { - runnableResources.customSpyStrategies()[name] = fn; + runableResources.customSpyStrategies()[name] = fn; }; this.addCustomEqualityTester = function(tester) { - runnableResources.customEqualityTesters().push(tester); + runableResources.customEqualityTesters().push(tester); }; this.addMatchers = function(matchersToAdd) { - runnableResources.addCustomMatchers(matchersToAdd); + runableResources.addCustomMatchers(matchersToAdd); }; this.addAsyncMatchers = function(matchersToAdd) { - runnableResources.addCustomAsyncMatchers(matchersToAdd); + runableResources.addCustomAsyncMatchers(matchersToAdd); }; this.addCustomObjectFormatter = function(formatter) { - runnableResources.customObjectFormatters().push(formatter); + runableResources.customObjectFormatters().push(formatter); }; j$.Expectation.addCoreMatchers(j$.matchers); @@ -1359,8 +1359,8 @@ getJasmineRequireObj().Env = function(j$) { const expectationFactory = function(actual, spec) { return j$.Expectation.factory({ - matchersUtil: runnableResources.makeMatchersUtil(), - customMatchers: runnableResources.customMatchers(), + matchersUtil: runableResources.makeMatchersUtil(), + customMatchers: runableResources.customMatchers(), actual: actual, addExpectationResult: addExpectationResult }); @@ -1418,7 +1418,7 @@ getJasmineRequireObj().Env = function(j$) { function routeLateFailure(expectationResult) { // Report the result on the nearest ancestor suite that hasn't already // been reported done. - for (let r = currentRunnable(); r; r = r.parentSuite) { + for (let r = currentRunable(); r; r = r.parentSuite) { if (!r.reportedDone) { if (r === topSuite) { expectationResult.globalErrorType = 'lateError'; @@ -1437,14 +1437,14 @@ getJasmineRequireObj().Env = function(j$) { const asyncExpectationFactory = function(actual, spec, runableType) { return j$.Expectation.asyncFactory({ - matchersUtil: runnableResources.makeMatchersUtil(), - customAsyncMatchers: runnableResources.customAsyncMatchers(), + matchersUtil: runableResources.makeMatchersUtil(), + customAsyncMatchers: runableResources.customAsyncMatchers(), actual: actual, addExpectationResult: addExpectationResult }); function addExpectationResult(passed, result) { - if (currentRunnable() !== spec) { + if (currentRunable() !== spec) { recordLateExpectation(spec, runableType, result); } return spec.addExpectationResult(passed, result); @@ -1511,8 +1511,8 @@ getJasmineRequireObj().Env = function(j$) { * @param {Object} [options] Optional extra options, as described above */ this.deprecated = function(deprecation, options) { - const runnable = currentRunnable() || topSuite; - deprecator.addDeprecationWarning(runnable, deprecation, options); + const runable = currentRunable() || topSuite; + deprecator.addDeprecationWarning(runable, deprecation, options); }; function queueRunnerFactory(options, args) { @@ -1541,7 +1541,7 @@ getJasmineRequireObj().Env = function(j$) { options.onException = options.onException || function(e) { - (currentRunnable() || topSuite).handleException(e); + (currentRunable() || topSuite).handleException(e); }; options.deprecated = self.deprecated; @@ -1674,23 +1674,23 @@ getJasmineRequireObj().Env = function(j$) { * @name Env#execute * @since 2.0.0 * @function - * @param {(string[])=} runnablesToRun IDs of suites and/or specs to run + * @param {(string[])=} runablesToRun IDs of suites and/or specs to run * @param {Function=} onComplete Function that will be called after all specs have run * @return {Promise} */ - this.execute = function(runnablesToRun, onComplete) { + this.execute = function(runablesToRun, onComplete) { if (this._executedBefore) { topSuite.reset(); } this._executedBefore = true; - runnableResources.initForRunnable(topSuite.id); + runableResources.initForRunable(topSuite.id); installGlobalErrors(); - if (!runnablesToRun) { - if (focusedRunnables.length) { - runnablesToRun = focusedRunnables; + if (!runablesToRun) { + if (focusedRunables.length) { + runablesToRun = focusedRunables; } else { - runnablesToRun = [topSuite.id]; + runablesToRun = [topSuite.id]; } } @@ -1701,12 +1701,12 @@ getJasmineRequireObj().Env = function(j$) { const processor = new j$.TreeProcessor({ tree: topSuite, - runnableIds: runnablesToRun, + runnableIds: runablesToRun, queueRunnerFactory: queueRunnerFactory, failSpecWithNoExpectations: config.failSpecWithNoExpectations, nodeStart: function(suite, next) { currentlyExecutingSuites.push(suite); - runnableResources.initForRunnable(suite.id, suite.parentSuite.id); + runableResources.initForRunable(suite.id, suite.parentSuite.id); reporter.suiteStarted(suite.result, next); suite.startTimer(); }, @@ -1715,7 +1715,7 @@ getJasmineRequireObj().Env = function(j$) { throw new Error('Tried to complete the wrong suite'); } - runnableResources.clearForRunnable(suite.id); + runableResources.clearForRunable(suite.id); currentlyExecutingSuites.pop(); if (result.status === 'failed') { @@ -1780,7 +1780,7 @@ getJasmineRequireObj().Env = function(j$) { await reportChildrenOfBeforeAllFailure(topSuite); } - runnableResources.clearForRunnable(topSuite.id); + runableResources.clearForRunable(topSuite.id); currentlyExecutingSuites.pop(); let overallStatus, incompleteReason; @@ -1789,7 +1789,7 @@ getJasmineRequireObj().Env = function(j$) { topSuite.result.failedExpectations.length > 0 ) { overallStatus = 'failed'; - } else if (focusedRunnables.length > 0) { + } else if (focusedRunables.length > 0) { overallStatus = 'incomplete'; incompleteReason = 'fit() or fdescribe() was found'; } else if (totalSpecsDefined === 0) { @@ -1914,36 +1914,36 @@ getJasmineRequireObj().Env = function(j$) { * @param {boolean} allow Whether to allow respying */ this.allowRespy = function(allow) { - runnableResources.spyRegistry.allowRespy(allow); + runableResources.spyRegistry.allowRespy(allow); }; this.spyOn = function() { - return runnableResources.spyRegistry.spyOn.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOn.apply( + runableResources.spyRegistry, arguments ); }; this.spyOnProperty = function() { - return runnableResources.spyRegistry.spyOnProperty.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOnProperty.apply( + runableResources.spyRegistry, arguments ); }; this.spyOnAllFunctions = function() { - return runnableResources.spyRegistry.spyOnAllFunctions.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOnAllFunctions.apply( + runableResources.spyRegistry, arguments ); }; this.createSpy = function(name, originalFn) { - return runnableResources.spyFactory.createSpy(name, originalFn); + return runableResources.spyFactory.createSpy(name, originalFn); }; this.createSpyObj = function(baseName, methodNames, propertyNames) { - return runnableResources.spyFactory.createSpyObj( + return runableResources.spyFactory.createSpyObj( baseName, methodNames, propertyNames @@ -1967,8 +1967,8 @@ getJasmineRequireObj().Env = function(j$) { } function ensureIsNotNested(method) { - const runnable = currentRunnable(); - if (runnable !== null && runnable !== undefined) { + const runable = currentRunable(); + if (runable !== null && runable !== undefined) { throw new Error( "'" + method + "' should only be used in 'describe' function" ); @@ -2024,7 +2024,7 @@ getJasmineRequireObj().Env = function(j$) { const suite = suiteFactory(description); suite.isFocused = true; - focusedRunnables.push(suite.id); + focusedRunables.push(suite.id); unfocusAncestor(); addSpecsToSuite(suite, specDefinitions); @@ -2064,9 +2064,9 @@ getJasmineRequireObj().Env = function(j$) { function unfocusAncestor() { const focusedAncestor = findFocusedAncestor(currentDeclarationSuite); if (focusedAncestor) { - for (let i = 0; i < focusedRunnables.length; i++) { - if (focusedRunnables[i] === focusedAncestor) { - focusedRunnables.splice(i, 1); + for (let i = 0; i < focusedRunables.length; i++) { + if (focusedRunables[i] === focusedAncestor) { + focusedRunables.splice(i, 1); break; } } @@ -2102,7 +2102,7 @@ getJasmineRequireObj().Env = function(j$) { return spec; function specResultCallback(result, next) { - runnableResources.clearForRunnable(spec.id); + runableResources.clearForRunable(spec.id); currentSpec = null; if (result.status === 'failed') { @@ -2114,7 +2114,7 @@ getJasmineRequireObj().Env = function(j$) { function specStarted(spec, next) { currentSpec = spec; - runnableResources.initForRunnable(spec.id, suite.id); + runableResources.initForRunable(spec.id, suite.id); reporter.specStarted(spec.result, next); } }; @@ -2186,7 +2186,7 @@ getJasmineRequireObj().Env = function(j$) { timeout ); currentDeclarationSuite.addChild(spec); - focusedRunnables.push(spec.id); + focusedRunables.push(spec.id); unfocusAncestor(); return spec.metadata; }; @@ -2200,12 +2200,12 @@ getJasmineRequireObj().Env = function(j$) { * @param {*} value The value of the property */ this.setSpecProperty = function(key, value) { - if (!currentRunnable() || currentRunnable() == currentSuite()) { + if (!currentRunable() || currentRunable() == currentSuite()) { throw new Error( "'setSpecProperty' was used when there was no current spec" ); } - currentRunnable().setSpecProperty(key, value); + currentRunable().setSpecProperty(key, value); }; /** @@ -2226,7 +2226,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.debugLog = function(msg) { - const maybeSpec = currentRunnable(); + const maybeSpec = currentRunable(); if (!maybeSpec || !maybeSpec.debugLog) { throw new Error("'debugLog' was called when there was no current spec"); @@ -2236,23 +2236,23 @@ getJasmineRequireObj().Env = function(j$) { }; this.expect = function(actual) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'expect' was used when there was no current spec, this could be because an asynchronous test timed out" ); } - return currentRunnable().expect(actual); + return currentRunable().expect(actual); }; this.expectAsync = function(actual) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'expectAsync' was used when there was no current spec, this could be because an asynchronous test timed out" ); } - return currentRunnable().expectAsync(actual); + return currentRunable().expectAsync(actual); }; this.beforeEach = function(beforeEachFunction, timeout) { @@ -2321,7 +2321,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.fail = function(error) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'fail' was used when there was no current spec, this could be because an asynchronous test timed out" ); @@ -2336,12 +2336,12 @@ getJasmineRequireObj().Env = function(j$) { message += error; } else { // pretty print all kind of objects. This includes arrays. - const pp = runnableResources.makePrettyPrinter(); + const pp = runableResources.makePrettyPrinter(); message += pp(error); } } - currentRunnable().addExpectationResult(false, { + currentRunable().addExpectationResult(false, { matcherName: '', passed: false, expected: '', @@ -8533,15 +8533,15 @@ getJasmineRequireObj().interface = function(jasmine, env) { return jasmineInterface; }; -getJasmineRequireObj().RunnableResources = function(j$) { - class RunnableResources { - constructor(getCurrentRunnableId) { - this.byRunnableId_ = {}; - this.getCurrentRunnableId_ = getCurrentRunnableId; +getJasmineRequireObj().RunableResources = function(j$) { + class RunableResources { + constructor(getCurrentRunableId) { + this.byRunableId_ = {}; + this.getCurrentRunableId_ = getCurrentRunableId; this.spyFactory = new j$.SpyFactory( () => { - if (this.getCurrentRunnableId_()) { + if (this.getCurrentRunableId_()) { return this.customSpyStrategies(); } else { return {}; @@ -8558,8 +8558,8 @@ getJasmineRequireObj().RunnableResources = function(j$) { }); } - initForRunnable(runnableId, parentId) { - const newRes = (this.byRunnableId_[runnableId] = { + initForRunable(runableId, parentId) { + const newRes = (this.byRunableId_[runableId] = { customEqualityTesters: [], customMatchers: {}, customAsyncMatchers: {}, @@ -8569,7 +8569,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { spies: [] }); - const parentRes = this.byRunnableId_[parentId]; + const parentRes = this.byRunableId_[parentId]; if (parentRes) { newRes.defaultSpyStrategy = parentRes.defaultSpyStrategy; @@ -8587,46 +8587,45 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - clearForRunnable(runnableId) { + clearForRunable(runableId) { this.spyRegistry.clearSpies(); - delete this.byRunnableId_[runnableId]; + delete this.byRunableId_[runableId]; } spies() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Spies must be created in a before function or a spec' ).spies; } defaultSpyStrategy() { - if (!this.getCurrentRunnableId_()) { + if (!this.getCurrentRunableId_()) { return undefined; } - return this.byRunnableId_[this.getCurrentRunnableId_()] - .defaultSpyStrategy; + return this.byRunableId_[this.getCurrentRunableId_()].defaultSpyStrategy; } setDefaultSpyStrategy(fn) { - this.forCurrentRunnable_( + this.forCurrentRunable_( 'Default spy strategy must be set in a before function or a spec' ).defaultSpyStrategy = fn; } customSpyStrategies() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom spy strategies must be added in a before function or a spec' ).customSpyStrategies; } customEqualityTesters() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom Equalities must be added in a before function or a spec' ).customEqualityTesters; } customMatchers() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Matchers must be added in a before function or a spec' ).customMatchers; } @@ -8640,7 +8639,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } customAsyncMatchers() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Async Matchers must be added in a before function or a spec' ).customAsyncMatchers; } @@ -8654,7 +8653,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } customObjectFormatters() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom object formatters must be added in a before function or a spec' ).customObjectFormatters; } @@ -8664,7 +8663,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } makeMatchersUtil() { - if (this.getCurrentRunnableId_()) { + if (this.getCurrentRunableId_()) { return new j$.MatchersUtil({ customTesters: this.customEqualityTesters(), pp: this.makePrettyPrinter() @@ -8674,8 +8673,8 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - forCurrentRunnable_(errorMsg) { - const resources = this.byRunnableId_[this.getCurrentRunnableId_()]; + forCurrentRunable_(errorMsg) { + const resources = this.byRunableId_[this.getCurrentRunableId_()]; if (!resources && errorMsg) { throw new Error(errorMsg); @@ -8685,7 +8684,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - return RunnableResources; + return RunableResources; }; getJasmineRequireObj().SkipAfterBeforeAllErrorPolicy = function(j$) { diff --git a/spec/core/RunableResourcesSpec.js b/spec/core/RunableResourcesSpec.js new file mode 100644 index 00000000..225aaab2 --- /dev/null +++ b/spec/core/RunableResourcesSpec.js @@ -0,0 +1,496 @@ +describe('RunableResources', function() { + describe('#spies', function() { + behavesLikeAPerRunableMutableArray( + 'spies', + 'Spies must be created in a before function or a spec', + false + ); + }); + + describe('#customSpyStrategies', function() { + behavesLikeAPerRunableMutableObject( + 'customSpyStrategies', + 'Custom spy strategies must be added in a before function or a spec' + ); + }); + + describe('#customEqualityTesters', function() { + behavesLikeAPerRunableMutableArray( + 'customEqualityTesters', + 'Custom Equalities must be added in a before function or a spec' + ); + }); + + describe('#customObjectFormatters', function() { + behavesLikeAPerRunableMutableArray( + 'customObjectFormatters', + 'Custom object formatters must be added in a before function or a spec' + ); + }); + + describe('#customMatchers', function() { + behavesLikeAPerRunableMutableObject( + 'customMatchers', + 'Matchers must be added in a before function or a spec' + ); + }); + + describe('#addCustomMatchers', function() { + it("adds all properties to the current runable's matchers", function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + + function toBeFoo() {} + function toBeBar() {} + function toBeBaz() {} + + runableResources.addCustomMatchers({ toBeFoo }); + expect(runableResources.customMatchers()).toEqual({ toBeFoo }); + + runableResources.addCustomMatchers({ toBeBar, toBeBaz }); + expect(runableResources.customMatchers()).toEqual({ + toBeFoo, + toBeBar, + toBeBaz + }); + }); + }); + + describe('#customAsyncMatchers', function() { + behavesLikeAPerRunableMutableObject( + 'customAsyncMatchers', + 'Async Matchers must be added in a before function or a spec' + ); + }); + + describe('#addCustomAsyncMatchers', function() { + it("adds all properties to the current runable's matchers", function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + + function toBeFoo() {} + function toBeBar() {} + function toBeBaz() {} + + runableResources.addCustomAsyncMatchers({ toBeFoo }); + expect(runableResources.customAsyncMatchers()).toEqual({ toBeFoo }); + + runableResources.addCustomAsyncMatchers({ toBeBar, toBeBaz }); + expect(runableResources.customAsyncMatchers()).toEqual({ + toBeFoo, + toBeBar, + toBeBaz + }); + }); + }); + + describe('#defaultSpyStrategy', function() { + it('returns undefined for a newly initialized resource', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + + expect(runableResources.defaultSpyStrategy()).toBeUndefined(); + }); + + it('returns the value previously set by #setDefaultSpyStrategy', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + const fn = () => {}; + runableResources.setDefaultSpyStrategy(fn); + + expect(runableResources.defaultSpyStrategy()).toBe(fn); + }); + + it('is per-runable', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + runableResources.setDefaultSpyStrategy(() => {}); + currentRunableId = 2; + runableResources.initForRunable(2); + + expect(runableResources.defaultSpyStrategy()).toBeUndefined(); + }); + + it('does not require a current runable', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + expect(runableResources.defaultSpyStrategy()).toBeUndefined(); + }); + + it("inherits the parent runable's value", function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + const fn = () => {}; + runableResources.setDefaultSpyStrategy(fn); + currentRunableId = 2; + runableResources.initForRunable(2, 1); + + expect(runableResources.defaultSpyStrategy()).toBe(fn); + }); + }); + + describe('#setDefaultSpyStrategy', function() { + it('throws a user-facing error when there is no current runable', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + expect(function() { + runableResources.setDefaultSpyStrategy(); + }).toThrowError( + 'Default spy strategy must be set in a before function or a spec' + ); + }); + }); + + describe('#makePrettyPrinter', function() { + it('returns a pretty printer configured with the current customObjectFormatters', function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function cof() {} + runableResources.customObjectFormatters().push(cof); + spyOn(jasmineUnderTest, 'makePrettyPrinter').and.callThrough(); + const pp = runableResources.makePrettyPrinter(); + + expect(jasmineUnderTest.makePrettyPrinter).toHaveBeenCalledOnceWith([ + cof + ]); + expect(pp).toBe( + jasmineUnderTest.makePrettyPrinter.calls.first().returnValue + ); + }); + }); + + describe('#makeMatchersUtil', function() { + describe('When there is a current runable', function() { + it('returns a MatchersUtil configured with the current resources', function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function cof() {} + runableResources.customObjectFormatters().push(cof); + function ceq() {} + runableResources.customEqualityTesters().push(ceq); + const expectedPP = {}; + const expectedMatchersUtil = {}; + spyOn(jasmineUnderTest, 'makePrettyPrinter').and.returnValue( + expectedPP + ); + spyOn(jasmineUnderTest, 'MatchersUtil').and.returnValue( + expectedMatchersUtil + ); + + const matchersUtil = runableResources.makeMatchersUtil(); + + expect(matchersUtil).toBe(expectedMatchersUtil); + expect(jasmineUnderTest.makePrettyPrinter).toHaveBeenCalledOnceWith([ + cof + ]); + // We need === equality on the pp passed to MatchersUtil + expect(jasmineUnderTest.MatchersUtil).toHaveBeenCalledOnceWith( + jasmine.objectContaining({ + customTesters: [ceq] + }) + ); + expect(jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].pp).toBe( + expectedPP + ); + }); + }); + + describe('When there is no current runable', function() { + it('returns a MatchersUtil configured with defaults', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + const expectedMatchersUtil = {}; + spyOn(jasmineUnderTest, 'MatchersUtil').and.returnValue( + expectedMatchersUtil + ); + + const matchersUtil = runableResources.makeMatchersUtil(); + + expect(matchersUtil).toBe(expectedMatchersUtil); + // We need === equality on the pp passed to MatchersUtil + expect(jasmineUnderTest.MatchersUtil).toHaveBeenCalledTimes(1); + expect(jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].pp).toBe( + jasmineUnderTest.basicPrettyPrinter_ + ); + expect( + jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].customTesters + ).toBeUndefined(); + }); + }); + }); + + describe('.spyFactory', function() { + describe('When there is no current runable', function() { + it('is configured with default strategies and matchersUtil', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + spyOn(jasmineUnderTest, 'Spy'); + const matchersUtil = {}; + spyOn(runableResources, 'makeMatchersUtil').and.returnValue( + matchersUtil + ); + + runableResources.spyFactory.createSpy('foo'); + + expect(jasmineUnderTest.Spy).toHaveBeenCalledWith( + 'foo', + is(matchersUtil), + jasmine.objectContaining({ + customStrategies: {}, + defaultStrategyFn: undefined + }) + ); + }); + }); + + describe('When there is a current runable', function() { + it("is configured with the current runable's strategies and matchersUtil", function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function customStrategy() {} + function defaultStrategy() {} + runableResources.customSpyStrategies().foo = customStrategy; + runableResources.setDefaultSpyStrategy(defaultStrategy); + spyOn(jasmineUnderTest, 'Spy'); + const matchersUtil = {}; + spyOn(runableResources, 'makeMatchersUtil').and.returnValue( + matchersUtil + ); + + runableResources.spyFactory.createSpy('foo'); + + expect(jasmineUnderTest.Spy).toHaveBeenCalledWith( + 'foo', + is(matchersUtil), + jasmine.objectContaining({ + customStrategies: { foo: customStrategy }, + defaultStrategyFn: defaultStrategy + }) + ); + }); + }); + + function is(expected) { + return { + asymmetricMatch: function(actual) { + return actual === expected; + }, + jasmineToString: function(pp) { + return ''; + } + }; + } + }); + + describe('.spyRegistry', function() { + it("writes to the current runable's spies", function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function foo() {} + const spyObj = { foo }; + runableResources.spyRegistry.spyOn(spyObj, 'foo'); + + expect(runableResources.spies()).toEqual([ + jasmine.objectContaining({ + restoreObjectToOriginalState: jasmine.any(Function) + }) + ]); + expect(jasmineUnderTest.isSpy(spyObj.foo)).toBeTrue(); + + runableResources.spyRegistry.clearSpies(); + expect(spyObj.foo).toBe(foo); + }); + }); + + describe('#clearForRunable', function() { + it('removes resources for the specified runable', function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + expect(function() { + runableResources.spies(); + }).not.toThrow(); + runableResources.clearForRunable(1); + expect(function() { + runableResources.spies(); + }).toThrowError('Spies must be created in a before function or a spec'); + }); + + it('clears spies', function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function foo() {} + const spyObj = { foo }; + runableResources.spyRegistry.spyOn(spyObj, 'foo'); + expect(spyObj.foo).not.toBe(foo); + + runableResources.clearForRunable(1); + expect(spyObj.foo).toBe(foo); + }); + + it('does not remove resources for other runables', function() { + const runableResources = new jasmineUnderTest.RunableResources(() => 1); + runableResources.initForRunable(1); + function cof() {} + runableResources.customObjectFormatters().push(cof); + runableResources.clearForRunable(2); + expect(runableResources.customObjectFormatters()).toEqual([cof]); + }); + }); + + function behavesLikeAPerRunableMutableArray( + methodName, + errorMsg, + inherits = true + ) { + it('is initially empty', function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + + expect(runableResources[methodName]()).toEqual([]); + }); + + it('is mutable', function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + function newItem() {} + runableResources[methodName]().push(newItem); + expect(runableResources[methodName]()).toEqual([newItem]); + }); + + it('is per-runable', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + runableResources[methodName]().push(() => {}); + runableResources.initForRunable(2); + currentRunableId = 2; + expect(runableResources[methodName]()).toEqual([]); + }); + + it('throws a user-facing error when there is no current runable', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + expect(function() { + runableResources[methodName](); + }).toThrowError(errorMsg); + }); + + if (inherits) { + it('inherits from the parent runable', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + function parentItem() {} + runableResources[methodName]().push(parentItem); + runableResources.initForRunable(2, 1); + currentRunableId = 2; + function childItem() {} + runableResources[methodName]().push(childItem); + expect(runableResources[methodName]()).toEqual([parentItem, childItem]); + + currentRunableId = 1; + expect(runableResources[methodName]()).toEqual([parentItem]); + }); + } + } + + function behavesLikeAPerRunableMutableObject(methodName, errorMsg) { + it('is initially empty', function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + + expect(runableResources[methodName]()).toEqual({}); + }); + + it('is mutable', function() { + const currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + function newItem() {} + runableResources[methodName]().foo = newItem; + expect(runableResources[methodName]()).toEqual({ foo: newItem }); + }); + + it('is per-runable', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + runableResources[methodName]().foo = function() {}; + runableResources.initForRunable(2); + currentRunableId = 2; + expect(runableResources[methodName]()).toEqual({}); + }); + + it('throws a user-facing error when there is no current runable', function() { + const runableResources = new jasmineUnderTest.RunableResources( + () => null + ); + expect(function() { + runableResources[methodName](); + }).toThrowError(errorMsg); + }); + + it('inherits from the parent runable', function() { + let currentRunableId = 1; + const runableResources = new jasmineUnderTest.RunableResources( + () => currentRunableId + ); + runableResources.initForRunable(1); + function parentItem() {} + runableResources[methodName]().parentName = parentItem; + runableResources.initForRunable(2, 1); + currentRunableId = 2; + function childItem() {} + runableResources[methodName]().childName = childItem; + expect(runableResources[methodName]()).toEqual({ + parentName: parentItem, + childName: childItem + }); + + currentRunableId = 1; + expect(runableResources[methodName]()).toEqual({ + parentName: parentItem + }); + }); + } +}); diff --git a/spec/core/RunnableResourcesSpec.js b/spec/core/RunnableResourcesSpec.js deleted file mode 100644 index a33f95f6..00000000 --- a/spec/core/RunnableResourcesSpec.js +++ /dev/null @@ -1,503 +0,0 @@ -describe('RunnableResources', function() { - describe('#spies', function() { - behavesLikeAPerRunnableMutableArray( - 'spies', - 'Spies must be created in a before function or a spec', - false - ); - }); - - describe('#customSpyStrategies', function() { - behavesLikeAPerRunnableMutableObject( - 'customSpyStrategies', - 'Custom spy strategies must be added in a before function or a spec' - ); - }); - - describe('#customEqualityTesters', function() { - behavesLikeAPerRunnableMutableArray( - 'customEqualityTesters', - 'Custom Equalities must be added in a before function or a spec' - ); - }); - - describe('#customObjectFormatters', function() { - behavesLikeAPerRunnableMutableArray( - 'customObjectFormatters', - 'Custom object formatters must be added in a before function or a spec' - ); - }); - - describe('#customMatchers', function() { - behavesLikeAPerRunnableMutableObject( - 'customMatchers', - 'Matchers must be added in a before function or a spec' - ); - }); - - describe('#addCustomMatchers', function() { - it("adds all properties to the current runnable's matchers", function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - - function toBeFoo() {} - function toBeBar() {} - function toBeBaz() {} - - runnableResources.addCustomMatchers({ toBeFoo }); - expect(runnableResources.customMatchers()).toEqual({ toBeFoo }); - - runnableResources.addCustomMatchers({ toBeBar, toBeBaz }); - expect(runnableResources.customMatchers()).toEqual({ - toBeFoo, - toBeBar, - toBeBaz - }); - }); - }); - - describe('#customAsyncMatchers', function() { - behavesLikeAPerRunnableMutableObject( - 'customAsyncMatchers', - 'Async Matchers must be added in a before function or a spec' - ); - }); - - describe('#addCustomAsyncMatchers', function() { - it("adds all properties to the current runnable's matchers", function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - - function toBeFoo() {} - function toBeBar() {} - function toBeBaz() {} - - runnableResources.addCustomAsyncMatchers({ toBeFoo }); - expect(runnableResources.customAsyncMatchers()).toEqual({ toBeFoo }); - - runnableResources.addCustomAsyncMatchers({ toBeBar, toBeBaz }); - expect(runnableResources.customAsyncMatchers()).toEqual({ - toBeFoo, - toBeBar, - toBeBaz - }); - }); - }); - - describe('#defaultSpyStrategy', function() { - it('returns undefined for a newly initialized resource', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - - expect(runnableResources.defaultSpyStrategy()).toBeUndefined(); - }); - - it('returns the value previously set by #setDefaultSpyStrategy', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - const fn = () => {}; - runnableResources.setDefaultSpyStrategy(fn); - - expect(runnableResources.defaultSpyStrategy()).toBe(fn); - }); - - it('is per-runnable', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - runnableResources.setDefaultSpyStrategy(() => {}); - currentRunnableId = 2; - runnableResources.initForRunnable(2); - - expect(runnableResources.defaultSpyStrategy()).toBeUndefined(); - }); - - it('does not require a current runnable', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - expect(runnableResources.defaultSpyStrategy()).toBeUndefined(); - }); - - it("inherits the parent runnable's value", function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - const fn = () => {}; - runnableResources.setDefaultSpyStrategy(fn); - currentRunnableId = 2; - runnableResources.initForRunnable(2, 1); - - expect(runnableResources.defaultSpyStrategy()).toBe(fn); - }); - }); - - describe('#setDefaultSpyStrategy', function() { - it('throws a user-facing error when there is no current runnable', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - expect(function() { - runnableResources.setDefaultSpyStrategy(); - }).toThrowError( - 'Default spy strategy must be set in a before function or a spec' - ); - }); - }); - - describe('#makePrettyPrinter', function() { - it('returns a pretty printer configured with the current customObjectFormatters', function() { - const runnableResources = new jasmineUnderTest.RunnableResources(() => 1); - runnableResources.initForRunnable(1); - function cof() {} - runnableResources.customObjectFormatters().push(cof); - spyOn(jasmineUnderTest, 'makePrettyPrinter').and.callThrough(); - const pp = runnableResources.makePrettyPrinter(); - - expect(jasmineUnderTest.makePrettyPrinter).toHaveBeenCalledOnceWith([ - cof - ]); - expect(pp).toBe( - jasmineUnderTest.makePrettyPrinter.calls.first().returnValue - ); - }); - }); - - describe('#makeMatchersUtil', function() { - describe('When there is a current runnable', function() { - it('returns a MatchersUtil configured with the current resources', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => 1 - ); - runnableResources.initForRunnable(1); - function cof() {} - runnableResources.customObjectFormatters().push(cof); - function ceq() {} - runnableResources.customEqualityTesters().push(ceq); - const expectedPP = {}; - const expectedMatchersUtil = {}; - spyOn(jasmineUnderTest, 'makePrettyPrinter').and.returnValue( - expectedPP - ); - spyOn(jasmineUnderTest, 'MatchersUtil').and.returnValue( - expectedMatchersUtil - ); - - const matchersUtil = runnableResources.makeMatchersUtil(); - - expect(matchersUtil).toBe(expectedMatchersUtil); - expect(jasmineUnderTest.makePrettyPrinter).toHaveBeenCalledOnceWith([ - cof - ]); - // We need === equality on the pp passed to MatchersUtil - expect(jasmineUnderTest.MatchersUtil).toHaveBeenCalledOnceWith( - jasmine.objectContaining({ - customTesters: [ceq] - }) - ); - expect(jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].pp).toBe( - expectedPP - ); - }); - }); - - describe('When there is no current runnable', function() { - it('returns a MatchersUtil configured with defaults', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - const expectedMatchersUtil = {}; - spyOn(jasmineUnderTest, 'MatchersUtil').and.returnValue( - expectedMatchersUtil - ); - - const matchersUtil = runnableResources.makeMatchersUtil(); - - expect(matchersUtil).toBe(expectedMatchersUtil); - // We need === equality on the pp passed to MatchersUtil - expect(jasmineUnderTest.MatchersUtil).toHaveBeenCalledTimes(1); - expect(jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].pp).toBe( - jasmineUnderTest.basicPrettyPrinter_ - ); - expect( - jasmineUnderTest.MatchersUtil.calls.argsFor(0)[0].customTesters - ).toBeUndefined(); - }); - }); - }); - - describe('.spyFactory', function() { - describe('When there is no current runnable', function() { - it('is configured with default strategies and matchersUtil', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - spyOn(jasmineUnderTest, 'Spy'); - const matchersUtil = {}; - spyOn(runnableResources, 'makeMatchersUtil').and.returnValue( - matchersUtil - ); - - runnableResources.spyFactory.createSpy('foo'); - - expect(jasmineUnderTest.Spy).toHaveBeenCalledWith( - 'foo', - is(matchersUtil), - jasmine.objectContaining({ - customStrategies: {}, - defaultStrategyFn: undefined - }) - ); - }); - }); - - describe('When there is a current runnable', function() { - it("is configured with the current runnable's strategies and matchersUtil", function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => 1 - ); - runnableResources.initForRunnable(1); - function customStrategy() {} - function defaultStrategy() {} - runnableResources.customSpyStrategies().foo = customStrategy; - runnableResources.setDefaultSpyStrategy(defaultStrategy); - spyOn(jasmineUnderTest, 'Spy'); - const matchersUtil = {}; - spyOn(runnableResources, 'makeMatchersUtil').and.returnValue( - matchersUtil - ); - - runnableResources.spyFactory.createSpy('foo'); - - expect(jasmineUnderTest.Spy).toHaveBeenCalledWith( - 'foo', - is(matchersUtil), - jasmine.objectContaining({ - customStrategies: { foo: customStrategy }, - defaultStrategyFn: defaultStrategy - }) - ); - }); - }); - - function is(expected) { - return { - asymmetricMatch: function(actual) { - return actual === expected; - }, - jasmineToString: function(pp) { - return ''; - } - }; - } - }); - - describe('.spyRegistry', function() { - it("writes to the current runnable's spies", function() { - const runnableResources = new jasmineUnderTest.RunnableResources(() => 1); - runnableResources.initForRunnable(1); - function foo() {} - const spyObj = { foo }; - runnableResources.spyRegistry.spyOn(spyObj, 'foo'); - - expect(runnableResources.spies()).toEqual([ - jasmine.objectContaining({ - restoreObjectToOriginalState: jasmine.any(Function) - }) - ]); - expect(jasmineUnderTest.isSpy(spyObj.foo)).toBeTrue(); - - runnableResources.spyRegistry.clearSpies(); - expect(spyObj.foo).toBe(foo); - }); - }); - - describe('#clearForRunnable', function() { - it('removes resources for the specified runnable', function() { - const runnableResources = new jasmineUnderTest.RunnableResources(() => 1); - runnableResources.initForRunnable(1); - expect(function() { - runnableResources.spies(); - }).not.toThrow(); - runnableResources.clearForRunnable(1); - expect(function() { - runnableResources.spies(); - }).toThrowError('Spies must be created in a before function or a spec'); - }); - - it('clears spies', function() { - const runnableResources = new jasmineUnderTest.RunnableResources(() => 1); - runnableResources.initForRunnable(1); - function foo() {} - const spyObj = { foo }; - runnableResources.spyRegistry.spyOn(spyObj, 'foo'); - expect(spyObj.foo).not.toBe(foo); - - runnableResources.clearForRunnable(1); - expect(spyObj.foo).toBe(foo); - }); - - it('does not remove resources for other runnables', function() { - const runnableResources = new jasmineUnderTest.RunnableResources(() => 1); - runnableResources.initForRunnable(1); - function cof() {} - runnableResources.customObjectFormatters().push(cof); - runnableResources.clearForRunnable(2); - expect(runnableResources.customObjectFormatters()).toEqual([cof]); - }); - }); - - function behavesLikeAPerRunnableMutableArray( - methodName, - errorMsg, - inherits = true - ) { - it('is initially empty', function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - - expect(runnableResources[methodName]()).toEqual([]); - }); - - it('is mutable', function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - function newItem() {} - runnableResources[methodName]().push(newItem); - expect(runnableResources[methodName]()).toEqual([newItem]); - }); - - it('is per-runnable', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - runnableResources[methodName]().push(() => {}); - runnableResources.initForRunnable(2); - currentRunnableId = 2; - expect(runnableResources[methodName]()).toEqual([]); - }); - - it('throws a user-facing error when there is no current runnable', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - expect(function() { - runnableResources[methodName](); - }).toThrowError(errorMsg); - }); - - if (inherits) { - it('inherits from the parent runnable', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - function parentItem() {} - runnableResources[methodName]().push(parentItem); - runnableResources.initForRunnable(2, 1); - currentRunnableId = 2; - function childItem() {} - runnableResources[methodName]().push(childItem); - expect(runnableResources[methodName]()).toEqual([ - parentItem, - childItem - ]); - - currentRunnableId = 1; - expect(runnableResources[methodName]()).toEqual([parentItem]); - }); - } - } - - function behavesLikeAPerRunnableMutableObject(methodName, errorMsg) { - it('is initially empty', function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - - expect(runnableResources[methodName]()).toEqual({}); - }); - - it('is mutable', function() { - const currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - function newItem() {} - runnableResources[methodName]().foo = newItem; - expect(runnableResources[methodName]()).toEqual({ foo: newItem }); - }); - - it('is per-runnable', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - runnableResources[methodName]().foo = function() {}; - runnableResources.initForRunnable(2); - currentRunnableId = 2; - expect(runnableResources[methodName]()).toEqual({}); - }); - - it('throws a user-facing error when there is no current runnable', function() { - const runnableResources = new jasmineUnderTest.RunnableResources( - () => null - ); - expect(function() { - runnableResources[methodName](); - }).toThrowError(errorMsg); - }); - - it('inherits from the parent runnable', function() { - let currentRunnableId = 1; - const runnableResources = new jasmineUnderTest.RunnableResources( - () => currentRunnableId - ); - runnableResources.initForRunnable(1); - function parentItem() {} - runnableResources[methodName]().parentName = parentItem; - runnableResources.initForRunnable(2, 1); - currentRunnableId = 2; - function childItem() {} - runnableResources[methodName]().childName = childItem; - expect(runnableResources[methodName]()).toEqual({ - parentName: parentItem, - childName: childItem - }); - - currentRunnableId = 1; - expect(runnableResources[methodName]()).toEqual({ - parentName: parentItem - }); - }); - } -}); diff --git a/src/core/Env.js b/src/core/Env.js index debe99e2..fc8ccf0c 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -26,15 +26,15 @@ getJasmineRequireObj().Env = function(j$) { new j$.MockDate(global) ); - const runnableResources = new j$.RunnableResources(function() { - const r = currentRunnable(); + const runableResources = new j$.RunableResources(function() { + const r = currentRunable(); return r ? r.id : null; }); let topSuite; let currentSpec = null; const currentlyExecutingSuites = []; - const focusedRunnables = []; + const focusedRunables = []; let currentDeclarationSuite = null; let hasFailures = false; let deprecator; @@ -144,7 +144,7 @@ getJasmineRequireObj().Env = function(j$) { return currentlyExecutingSuites[currentlyExecutingSuites.length - 1]; } - function currentRunnable() { + function currentRunable() { return currentSpec || currentSuite(); } @@ -232,27 +232,27 @@ getJasmineRequireObj().Env = function(j$) { }; this.setDefaultSpyStrategy = function(defaultStrategyFn) { - runnableResources.setDefaultSpyStrategy(defaultStrategyFn); + runableResources.setDefaultSpyStrategy(defaultStrategyFn); }; this.addSpyStrategy = function(name, fn) { - runnableResources.customSpyStrategies()[name] = fn; + runableResources.customSpyStrategies()[name] = fn; }; this.addCustomEqualityTester = function(tester) { - runnableResources.customEqualityTesters().push(tester); + runableResources.customEqualityTesters().push(tester); }; this.addMatchers = function(matchersToAdd) { - runnableResources.addCustomMatchers(matchersToAdd); + runableResources.addCustomMatchers(matchersToAdd); }; this.addAsyncMatchers = function(matchersToAdd) { - runnableResources.addCustomAsyncMatchers(matchersToAdd); + runableResources.addCustomAsyncMatchers(matchersToAdd); }; this.addCustomObjectFormatter = function(formatter) { - runnableResources.customObjectFormatters().push(formatter); + runableResources.customObjectFormatters().push(formatter); }; j$.Expectation.addCoreMatchers(j$.matchers); @@ -272,8 +272,8 @@ getJasmineRequireObj().Env = function(j$) { const expectationFactory = function(actual, spec) { return j$.Expectation.factory({ - matchersUtil: runnableResources.makeMatchersUtil(), - customMatchers: runnableResources.customMatchers(), + matchersUtil: runableResources.makeMatchersUtil(), + customMatchers: runableResources.customMatchers(), actual: actual, addExpectationResult: addExpectationResult }); @@ -331,7 +331,7 @@ getJasmineRequireObj().Env = function(j$) { function routeLateFailure(expectationResult) { // Report the result on the nearest ancestor suite that hasn't already // been reported done. - for (let r = currentRunnable(); r; r = r.parentSuite) { + for (let r = currentRunable(); r; r = r.parentSuite) { if (!r.reportedDone) { if (r === topSuite) { expectationResult.globalErrorType = 'lateError'; @@ -350,14 +350,14 @@ getJasmineRequireObj().Env = function(j$) { const asyncExpectationFactory = function(actual, spec, runableType) { return j$.Expectation.asyncFactory({ - matchersUtil: runnableResources.makeMatchersUtil(), - customAsyncMatchers: runnableResources.customAsyncMatchers(), + matchersUtil: runableResources.makeMatchersUtil(), + customAsyncMatchers: runableResources.customAsyncMatchers(), actual: actual, addExpectationResult: addExpectationResult }); function addExpectationResult(passed, result) { - if (currentRunnable() !== spec) { + if (currentRunable() !== spec) { recordLateExpectation(spec, runableType, result); } return spec.addExpectationResult(passed, result); @@ -424,8 +424,8 @@ getJasmineRequireObj().Env = function(j$) { * @param {Object} [options] Optional extra options, as described above */ this.deprecated = function(deprecation, options) { - const runnable = currentRunnable() || topSuite; - deprecator.addDeprecationWarning(runnable, deprecation, options); + const runable = currentRunable() || topSuite; + deprecator.addDeprecationWarning(runable, deprecation, options); }; function queueRunnerFactory(options, args) { @@ -454,7 +454,7 @@ getJasmineRequireObj().Env = function(j$) { options.onException = options.onException || function(e) { - (currentRunnable() || topSuite).handleException(e); + (currentRunable() || topSuite).handleException(e); }; options.deprecated = self.deprecated; @@ -587,23 +587,23 @@ getJasmineRequireObj().Env = function(j$) { * @name Env#execute * @since 2.0.0 * @function - * @param {(string[])=} runnablesToRun IDs of suites and/or specs to run + * @param {(string[])=} runablesToRun IDs of suites and/or specs to run * @param {Function=} onComplete Function that will be called after all specs have run * @return {Promise} */ - this.execute = function(runnablesToRun, onComplete) { + this.execute = function(runablesToRun, onComplete) { if (this._executedBefore) { topSuite.reset(); } this._executedBefore = true; - runnableResources.initForRunnable(topSuite.id); + runableResources.initForRunable(topSuite.id); installGlobalErrors(); - if (!runnablesToRun) { - if (focusedRunnables.length) { - runnablesToRun = focusedRunnables; + if (!runablesToRun) { + if (focusedRunables.length) { + runablesToRun = focusedRunables; } else { - runnablesToRun = [topSuite.id]; + runablesToRun = [topSuite.id]; } } @@ -614,12 +614,12 @@ getJasmineRequireObj().Env = function(j$) { const processor = new j$.TreeProcessor({ tree: topSuite, - runnableIds: runnablesToRun, + runnableIds: runablesToRun, queueRunnerFactory: queueRunnerFactory, failSpecWithNoExpectations: config.failSpecWithNoExpectations, nodeStart: function(suite, next) { currentlyExecutingSuites.push(suite); - runnableResources.initForRunnable(suite.id, suite.parentSuite.id); + runableResources.initForRunable(suite.id, suite.parentSuite.id); reporter.suiteStarted(suite.result, next); suite.startTimer(); }, @@ -628,7 +628,7 @@ getJasmineRequireObj().Env = function(j$) { throw new Error('Tried to complete the wrong suite'); } - runnableResources.clearForRunnable(suite.id); + runableResources.clearForRunable(suite.id); currentlyExecutingSuites.pop(); if (result.status === 'failed') { @@ -693,7 +693,7 @@ getJasmineRequireObj().Env = function(j$) { await reportChildrenOfBeforeAllFailure(topSuite); } - runnableResources.clearForRunnable(topSuite.id); + runableResources.clearForRunable(topSuite.id); currentlyExecutingSuites.pop(); let overallStatus, incompleteReason; @@ -702,7 +702,7 @@ getJasmineRequireObj().Env = function(j$) { topSuite.result.failedExpectations.length > 0 ) { overallStatus = 'failed'; - } else if (focusedRunnables.length > 0) { + } else if (focusedRunables.length > 0) { overallStatus = 'incomplete'; incompleteReason = 'fit() or fdescribe() was found'; } else if (totalSpecsDefined === 0) { @@ -827,36 +827,36 @@ getJasmineRequireObj().Env = function(j$) { * @param {boolean} allow Whether to allow respying */ this.allowRespy = function(allow) { - runnableResources.spyRegistry.allowRespy(allow); + runableResources.spyRegistry.allowRespy(allow); }; this.spyOn = function() { - return runnableResources.spyRegistry.spyOn.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOn.apply( + runableResources.spyRegistry, arguments ); }; this.spyOnProperty = function() { - return runnableResources.spyRegistry.spyOnProperty.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOnProperty.apply( + runableResources.spyRegistry, arguments ); }; this.spyOnAllFunctions = function() { - return runnableResources.spyRegistry.spyOnAllFunctions.apply( - runnableResources.spyRegistry, + return runableResources.spyRegistry.spyOnAllFunctions.apply( + runableResources.spyRegistry, arguments ); }; this.createSpy = function(name, originalFn) { - return runnableResources.spyFactory.createSpy(name, originalFn); + return runableResources.spyFactory.createSpy(name, originalFn); }; this.createSpyObj = function(baseName, methodNames, propertyNames) { - return runnableResources.spyFactory.createSpyObj( + return runableResources.spyFactory.createSpyObj( baseName, methodNames, propertyNames @@ -880,8 +880,8 @@ getJasmineRequireObj().Env = function(j$) { } function ensureIsNotNested(method) { - const runnable = currentRunnable(); - if (runnable !== null && runnable !== undefined) { + const runable = currentRunable(); + if (runable !== null && runable !== undefined) { throw new Error( "'" + method + "' should only be used in 'describe' function" ); @@ -937,7 +937,7 @@ getJasmineRequireObj().Env = function(j$) { const suite = suiteFactory(description); suite.isFocused = true; - focusedRunnables.push(suite.id); + focusedRunables.push(suite.id); unfocusAncestor(); addSpecsToSuite(suite, specDefinitions); @@ -977,9 +977,9 @@ getJasmineRequireObj().Env = function(j$) { function unfocusAncestor() { const focusedAncestor = findFocusedAncestor(currentDeclarationSuite); if (focusedAncestor) { - for (let i = 0; i < focusedRunnables.length; i++) { - if (focusedRunnables[i] === focusedAncestor) { - focusedRunnables.splice(i, 1); + for (let i = 0; i < focusedRunables.length; i++) { + if (focusedRunables[i] === focusedAncestor) { + focusedRunables.splice(i, 1); break; } } @@ -1015,7 +1015,7 @@ getJasmineRequireObj().Env = function(j$) { return spec; function specResultCallback(result, next) { - runnableResources.clearForRunnable(spec.id); + runableResources.clearForRunable(spec.id); currentSpec = null; if (result.status === 'failed') { @@ -1027,7 +1027,7 @@ getJasmineRequireObj().Env = function(j$) { function specStarted(spec, next) { currentSpec = spec; - runnableResources.initForRunnable(spec.id, suite.id); + runableResources.initForRunable(spec.id, suite.id); reporter.specStarted(spec.result, next); } }; @@ -1099,7 +1099,7 @@ getJasmineRequireObj().Env = function(j$) { timeout ); currentDeclarationSuite.addChild(spec); - focusedRunnables.push(spec.id); + focusedRunables.push(spec.id); unfocusAncestor(); return spec.metadata; }; @@ -1113,12 +1113,12 @@ getJasmineRequireObj().Env = function(j$) { * @param {*} value The value of the property */ this.setSpecProperty = function(key, value) { - if (!currentRunnable() || currentRunnable() == currentSuite()) { + if (!currentRunable() || currentRunable() == currentSuite()) { throw new Error( "'setSpecProperty' was used when there was no current spec" ); } - currentRunnable().setSpecProperty(key, value); + currentRunable().setSpecProperty(key, value); }; /** @@ -1139,7 +1139,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.debugLog = function(msg) { - const maybeSpec = currentRunnable(); + const maybeSpec = currentRunable(); if (!maybeSpec || !maybeSpec.debugLog) { throw new Error("'debugLog' was called when there was no current spec"); @@ -1149,23 +1149,23 @@ getJasmineRequireObj().Env = function(j$) { }; this.expect = function(actual) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'expect' was used when there was no current spec, this could be because an asynchronous test timed out" ); } - return currentRunnable().expect(actual); + return currentRunable().expect(actual); }; this.expectAsync = function(actual) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'expectAsync' was used when there was no current spec, this could be because an asynchronous test timed out" ); } - return currentRunnable().expectAsync(actual); + return currentRunable().expectAsync(actual); }; this.beforeEach = function(beforeEachFunction, timeout) { @@ -1234,7 +1234,7 @@ getJasmineRequireObj().Env = function(j$) { }; this.fail = function(error) { - if (!currentRunnable()) { + if (!currentRunable()) { throw new Error( "'fail' was used when there was no current spec, this could be because an asynchronous test timed out" ); @@ -1249,12 +1249,12 @@ getJasmineRequireObj().Env = function(j$) { message += error; } else { // pretty print all kind of objects. This includes arrays. - const pp = runnableResources.makePrettyPrinter(); + const pp = runableResources.makePrettyPrinter(); message += pp(error); } } - currentRunnable().addExpectationResult(false, { + currentRunable().addExpectationResult(false, { matcherName: '', passed: false, expected: '', diff --git a/src/core/RunnableResources.js b/src/core/RunableResources.js similarity index 73% rename from src/core/RunnableResources.js rename to src/core/RunableResources.js index 112a07e1..a71b93b1 100644 --- a/src/core/RunnableResources.js +++ b/src/core/RunableResources.js @@ -1,12 +1,12 @@ -getJasmineRequireObj().RunnableResources = function(j$) { - class RunnableResources { - constructor(getCurrentRunnableId) { - this.byRunnableId_ = {}; - this.getCurrentRunnableId_ = getCurrentRunnableId; +getJasmineRequireObj().RunableResources = function(j$) { + class RunableResources { + constructor(getCurrentRunableId) { + this.byRunableId_ = {}; + this.getCurrentRunableId_ = getCurrentRunableId; this.spyFactory = new j$.SpyFactory( () => { - if (this.getCurrentRunnableId_()) { + if (this.getCurrentRunableId_()) { return this.customSpyStrategies(); } else { return {}; @@ -23,8 +23,8 @@ getJasmineRequireObj().RunnableResources = function(j$) { }); } - initForRunnable(runnableId, parentId) { - const newRes = (this.byRunnableId_[runnableId] = { + initForRunable(runableId, parentId) { + const newRes = (this.byRunableId_[runableId] = { customEqualityTesters: [], customMatchers: {}, customAsyncMatchers: {}, @@ -34,7 +34,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { spies: [] }); - const parentRes = this.byRunnableId_[parentId]; + const parentRes = this.byRunableId_[parentId]; if (parentRes) { newRes.defaultSpyStrategy = parentRes.defaultSpyStrategy; @@ -52,46 +52,45 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - clearForRunnable(runnableId) { + clearForRunable(runableId) { this.spyRegistry.clearSpies(); - delete this.byRunnableId_[runnableId]; + delete this.byRunableId_[runableId]; } spies() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Spies must be created in a before function or a spec' ).spies; } defaultSpyStrategy() { - if (!this.getCurrentRunnableId_()) { + if (!this.getCurrentRunableId_()) { return undefined; } - return this.byRunnableId_[this.getCurrentRunnableId_()] - .defaultSpyStrategy; + return this.byRunableId_[this.getCurrentRunableId_()].defaultSpyStrategy; } setDefaultSpyStrategy(fn) { - this.forCurrentRunnable_( + this.forCurrentRunable_( 'Default spy strategy must be set in a before function or a spec' ).defaultSpyStrategy = fn; } customSpyStrategies() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom spy strategies must be added in a before function or a spec' ).customSpyStrategies; } customEqualityTesters() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom Equalities must be added in a before function or a spec' ).customEqualityTesters; } customMatchers() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Matchers must be added in a before function or a spec' ).customMatchers; } @@ -105,7 +104,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } customAsyncMatchers() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Async Matchers must be added in a before function or a spec' ).customAsyncMatchers; } @@ -119,7 +118,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } customObjectFormatters() { - return this.forCurrentRunnable_( + return this.forCurrentRunable_( 'Custom object formatters must be added in a before function or a spec' ).customObjectFormatters; } @@ -129,7 +128,7 @@ getJasmineRequireObj().RunnableResources = function(j$) { } makeMatchersUtil() { - if (this.getCurrentRunnableId_()) { + if (this.getCurrentRunableId_()) { return new j$.MatchersUtil({ customTesters: this.customEqualityTesters(), pp: this.makePrettyPrinter() @@ -139,8 +138,8 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - forCurrentRunnable_(errorMsg) { - const resources = this.byRunnableId_[this.getCurrentRunnableId_()]; + forCurrentRunable_(errorMsg) { + const resources = this.byRunableId_[this.getCurrentRunableId_()]; if (!resources && errorMsg) { throw new Error(errorMsg); @@ -150,5 +149,5 @@ getJasmineRequireObj().RunnableResources = function(j$) { } } - return RunnableResources; + return RunableResources; }; diff --git a/src/core/requireCore.js b/src/core/requireCore.js index 2ef0636e..faa6b86e 100644 --- a/src/core/requireCore.js +++ b/src/core/requireCore.js @@ -68,7 +68,7 @@ var getJasmineRequireObj = (function(jasmineGlobal) { j$ ); j$.ReportDispatcher = jRequire.ReportDispatcher(j$); - j$.RunnableResources = jRequire.RunnableResources(j$); + j$.RunableResources = jRequire.RunableResources(j$); j$.Spec = jRequire.Spec(j$); j$.Spy = jRequire.Spy(j$); j$.SpyFactory = jRequire.SpyFactory(j$);