diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 01e327a2..7a4db56e 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -835,6 +835,11 @@ getJasmineRequireObj().Spec = function(j$) { } }; + Spec.prototype.getSpecProperty = function(key) { + this.result.properties = this.result.properties || {}; + return this.result.properties[key]; + }; + Spec.prototype.setSpecProperty = function(key, value) { this.result.properties = this.result.properties || {}; this.result.properties[key] = value; @@ -1906,6 +1911,26 @@ getJasmineRequireObj().Env = function(j$) { return suiteBuilder.fit(description, fn, timeout, filename).metadata; }; + /** + * Get a user-defined property as part of the properties field of {@link SpecResult} + * @name Env#getSpecProperty + * @since 5.10.0 + * @function + * @param {String} key The name of the property + * @returns {*} The value of the property + */ + this.getSpecProperty = function(key) { + if ( + !runner.currentRunable() || + runner.currentRunable() == runner.currentSuite() + ) { + throw new Error( + "'getSpecProperty' was used when there was no current spec" + ); + } + return runner.currentRunable().getSpecProperty(key); + }; + /** * Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult} * @name Env#setSpecProperty @@ -8858,6 +8883,18 @@ getJasmineRequireObj().interface = function(jasmine, env) { return env.afterAll.apply(env, arguments); }, + /** + * Get a user-defined property as part of the properties field of {@link SpecResult} + * @name getSpecProperty + * @since 5.10.0 + * @function + * @param {String} key The name of the property + * @returns {*} The value of the property + */ + getSpecProperty: function(key) { + return env.getSpecProperty(key); + }, + /** * Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult} * @name setSpecProperty diff --git a/spec/core/SpecSpec.js b/spec/core/SpecSpec.js index d5a15487..e7b3a3bc 100644 --- a/spec/core/SpecSpec.js +++ b/spec/core/SpecSpec.js @@ -78,6 +78,17 @@ describe('Spec', function() { }); }); + describe('#getSpecProperty', function() { + it('get the property value', function() { + const spec = new jasmineUnderTest.Spec({ + queueableFn: { fn: () => {} } + }); + + spec.setSpecProperty('a', 4); + expect(spec.getSpecProperty('a')).toBe(4); + }); + }); + describe('#setSpecProperty', function() { it('adds the property to the result', function() { const spec = new jasmineUnderTest.Spec({ @@ -88,6 +99,21 @@ describe('Spec', function() { expect(spec.result.properties).toEqual({ a: 4 }); }); + + it('replace the property result when it was previously set', function() { + const spec = new jasmineUnderTest.Spec({ + queueableFn: { fn: () => {} } + }); + + spec.setSpecProperty('a', 'original-value'); + spec.setSpecProperty('b', 'original-value'); + spec.setSpecProperty('a', 'new-value'); + + expect(spec.result.properties).toEqual({ + a: 'new-value', + b: 'original-value' + }); + }); }); it('#status returns passing by default', function() { diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index a9f130b2..5678c24d 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2262,17 +2262,54 @@ describe('Env integration', function() { ); }); + it('throws an exception if you try to getSpecProperty outside of a spec', async function() { + const env = new jasmineUnderTest.Env(); + let exception; + + env.describe('a suite', function() { + env.it('a spec'); + try { + env.getSpecProperty('a prop'); + } catch (e) { + exception = e; + } + env.it('has a test', function() {}); + }); + + await env.execute(); + + expect(exception.message).toBe( + "'getSpecProperty' was used when there was no current spec" + ); + }); + it('reports test properties on specs', async function() { const env = new jasmineUnderTest.Env(), reporter = jasmine.createSpyObj('reporter', ['suiteDone', 'specDone']); reporter.specDone.and.callFake(function(e) { - expect(e.properties).toEqual({ a: 'Bee' }); + expect(e.properties).toEqual({ + fromBeforeEach: 'Pie', + fromSpecInnerCallback: 'Bee', + willChangeInAfterEach: 2, + fromAfterEach: 'Cheese' + }); }); env.addReporter(reporter); + env.beforeEach(function() { + env.setSpecProperty('fromBeforeEach', 'Pie'); + }); + env.afterEach(function() { + env.setSpecProperty( + 'willChangeInAfterEach', + env.getSpecProperty('willChangeInAfterEach') + 1 + ); + env.setSpecProperty('fromAfterEach', 'Cheese'); + }); env.it('calls setSpecProperty', function() { - env.setSpecProperty('a', 'Bee'); + env.setSpecProperty('fromSpecInnerCallback', 'Bee'); + env.setSpecProperty('willChangeInAfterEach', 1); }); await env.execute(); diff --git a/src/core/Env.js b/src/core/Env.js index dc0ee9b1..be7a24be 100644 --- a/src/core/Env.js +++ b/src/core/Env.js @@ -779,6 +779,26 @@ getJasmineRequireObj().Env = function(j$) { return suiteBuilder.fit(description, fn, timeout, filename).metadata; }; + /** + * Get a user-defined property as part of the properties field of {@link SpecResult} + * @name Env#getSpecProperty + * @since 5.10.0 + * @function + * @param {String} key The name of the property + * @returns {*} The value of the property + */ + this.getSpecProperty = function(key) { + if ( + !runner.currentRunable() || + runner.currentRunable() == runner.currentSuite() + ) { + throw new Error( + "'getSpecProperty' was used when there was no current spec" + ); + } + return runner.currentRunable().getSpecProperty(key); + }; + /** * Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult} * @name Env#setSpecProperty diff --git a/src/core/Spec.js b/src/core/Spec.js index 3af546de..1e0ae478 100644 --- a/src/core/Spec.js +++ b/src/core/Spec.js @@ -63,6 +63,11 @@ getJasmineRequireObj().Spec = function(j$) { } }; + Spec.prototype.getSpecProperty = function(key) { + this.result.properties = this.result.properties || {}; + return this.result.properties[key]; + }; + Spec.prototype.setSpecProperty = function(key, value) { this.result.properties = this.result.properties || {}; this.result.properties[key] = value; diff --git a/src/core/requireInterface.js b/src/core/requireInterface.js index 6caaf5fe..507ba14d 100644 --- a/src/core/requireInterface.js +++ b/src/core/requireInterface.js @@ -168,6 +168,18 @@ getJasmineRequireObj().interface = function(jasmine, env) { return env.afterAll.apply(env, arguments); }, + /** + * Get a user-defined property as part of the properties field of {@link SpecResult} + * @name getSpecProperty + * @since 5.10.0 + * @function + * @param {String} key The name of the property + * @returns {*} The value of the property + */ + getSpecProperty: function(key) { + return env.getSpecProperty(key); + }, + /** * Sets a user-defined property that will be provided to reporters as part of the properties field of {@link SpecResult} * @name setSpecProperty