From f822ffea2182c4f4c6234a8ab4f361f0165b69e6 Mon Sep 17 00:00:00 2001 From: kbon Date: Mon, 25 Aug 2025 23:00:06 -0400 Subject: [PATCH 1/2] feat(`getSpecProperty`) get a user-defined property --- spec/core/SpecSpec.js | 26 ++++++++++++++++++++ spec/core/integration/EnvSpec.js | 41 ++++++++++++++++++++++++++++++-- src/core/Env.js | 20 ++++++++++++++++ src/core/Spec.js | 5 ++++ src/core/requireInterface.js | 12 ++++++++++ 5 files changed, 102 insertions(+), 2 deletions(-) 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..f4837c9b 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('willChange') + 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 From 7feec406d94bbcc342e66f466c77a9bc95db9385 Mon Sep 17 00:00:00 2001 From: kbon Date: Mon, 25 Aug 2025 23:12:29 -0400 Subject: [PATCH 2/2] set the right spec property key --- spec/core/integration/EnvSpec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/core/integration/EnvSpec.js b/spec/core/integration/EnvSpec.js index f4837c9b..5678c24d 100644 --- a/spec/core/integration/EnvSpec.js +++ b/spec/core/integration/EnvSpec.js @@ -2303,7 +2303,7 @@ describe('Env integration', function() { env.afterEach(function() { env.setSpecProperty( 'willChangeInAfterEach', - env.getSpecProperty('willChange') + 1 + env.getSpecProperty('willChangeInAfterEach') + 1 ); env.setSpecProperty('fromAfterEach', 'Cheese'); });