From 26dfa6d257feeaed2e68875478f51f89552a5b02 Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Mon, 2 Dec 2024 07:52:11 +0000 Subject: [PATCH 1/6] Add .toBeNullish matcher --- lib/jasmine-core/jasmine.js | 1 + spec/core/matchers/toBeNullishSpec.js | 19 +++++++++++++++++++ src/core/matchers/toBeNullish.js | 21 +++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 spec/core/matchers/toBeNullishSpec.js create mode 100644 src/core/matchers/toBeNullish.js diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 811c0130..ccd5f273 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -150,6 +150,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) { 'toBeTrue', 'toBeTruthy', 'toBeUndefined', + 'toBeNullish', 'toContain', 'toEqual', 'toHaveSize', diff --git a/spec/core/matchers/toBeNullishSpec.js b/spec/core/matchers/toBeNullishSpec.js new file mode 100644 index 00000000..29c41fdc --- /dev/null +++ b/spec/core/matchers/toBeNullishSpec.js @@ -0,0 +1,19 @@ +describe('toBeNullish', function() { + it('passes for null values', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(null); + expect(result.pass).toBe(true); + }); + + it('passes for undefined values', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(void 0); + expect(result.pass).toBe(true); + }); + + it('fails when matching defined values', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare('foo'); + expect(result.pass).toBe(false); + }); +}); diff --git a/src/core/matchers/toBeNullish.js b/src/core/matchers/toBeNullish.js new file mode 100644 index 00000000..c833d43f --- /dev/null +++ b/src/core/matchers/toBeNullish.js @@ -0,0 +1,21 @@ +getJasmineRequireObj().toBeNullish = function() { + /** + * {@link expect} the actual value to be `null` or `undefined`. + * @function + * @name matchers#toBeNullish + * @since 5.6.0 + * @example + * expect(result).toBeNullish(): + */ + function toBeNullish() { + return { + compare: function(actual) { + return { + pass: null === actual || void 0 === actual + }; + } + }; + } + + return toBeNullish; +}; From d9286c549fbdb0547aa6b1b774241020b7610244 Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Mon, 2 Dec 2024 09:37:02 +0000 Subject: [PATCH 2/6] add integration test for toBeNullish --- spec/core/integration/MatchersSpec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/core/integration/MatchersSpec.js b/spec/core/integration/MatchersSpec.js index f09656cd..912ca968 100755 --- a/spec/core/integration/MatchersSpec.js +++ b/spec/core/integration/MatchersSpec.js @@ -469,6 +469,21 @@ describe('Matchers (Integration)', function() { }); }); + describe('toBeNullish', function() { + verifyPasses(function(env) { + env.expect(undefined).toBeNullish(); + }); + + verifyPasses(function(env) { + env.expect(null).toBeNullish(); + }); + + verifyFails(function(env) { + env.expect(1).toBeNullish(); + }); + + }); + describe('toContain', function() { verifyPasses(function(env) { env.addCustomEqualityTester(function(a, b) { From 580323c22195aac47f2a583661caecfa7e8b1e57 Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Mon, 2 Dec 2024 09:51:51 +0000 Subject: [PATCH 3/6] run prettier and fix tests --- lib/jasmine-core/jasmine.js | 22 ++++++++++++++++++++++ spec/core/integration/MatchersSpec.js | 1 - src/core/matchers/requireMatchers.js | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index ccd5f273..3b92ca4f 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -6011,6 +6011,28 @@ getJasmineRequireObj().toBeNull = function() { return toBeNull; }; +getJasmineRequireObj().toBeNullish = function() { + /** + * {@link expect} the actual value to be `null` or `undefined`. + * @function + * @name matchers#toBeNullish + * @since 5.6.0 + * @example + * expect(result).toBeNullish(): + */ + function toBeNullish() { + return { + compare: function(actual) { + return { + pass: null === actual || void 0 === actual + }; + } + }; + } + + return toBeNullish; +}; + getJasmineRequireObj().toBePositiveInfinity = function(j$) { /** * {@link expect} the actual value to be `Infinity` (infinity). diff --git a/spec/core/integration/MatchersSpec.js b/spec/core/integration/MatchersSpec.js index 912ca968..223b4a16 100755 --- a/spec/core/integration/MatchersSpec.js +++ b/spec/core/integration/MatchersSpec.js @@ -481,7 +481,6 @@ describe('Matchers (Integration)', function() { verifyFails(function(env) { env.expect(1).toBeNullish(); }); - }); describe('toContain', function() { diff --git a/src/core/matchers/requireMatchers.js b/src/core/matchers/requireMatchers.js index b15435c4..673a776a 100755 --- a/src/core/matchers/requireMatchers.js +++ b/src/core/matchers/requireMatchers.js @@ -18,6 +18,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) { 'toBeTrue', 'toBeTruthy', 'toBeUndefined', + 'toBeNullish', 'toContain', 'toEqual', 'toHaveSize', From 27bb6ebac1e55585a2278a3f5448dbc259ea82da Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Mon, 2 Dec 2024 10:18:24 +0000 Subject: [PATCH 4/6] reset jasmine.js --- lib/jasmine-core/jasmine.js | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 3b92ca4f..811c0130 100644 --- a/lib/jasmine-core/jasmine.js +++ b/lib/jasmine-core/jasmine.js @@ -150,7 +150,6 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) { 'toBeTrue', 'toBeTruthy', 'toBeUndefined', - 'toBeNullish', 'toContain', 'toEqual', 'toHaveSize', @@ -6011,28 +6010,6 @@ getJasmineRequireObj().toBeNull = function() { return toBeNull; }; -getJasmineRequireObj().toBeNullish = function() { - /** - * {@link expect} the actual value to be `null` or `undefined`. - * @function - * @name matchers#toBeNullish - * @since 5.6.0 - * @example - * expect(result).toBeNullish(): - */ - function toBeNullish() { - return { - compare: function(actual) { - return { - pass: null === actual || void 0 === actual - }; - } - }; - } - - return toBeNullish; -}; - getJasmineRequireObj().toBePositiveInfinity = function(j$) { /** * {@link expect} the actual value to be `Infinity` (infinity). From 2f3689713b07b002c5cb45431de9f312e9fed5a7 Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Tue, 10 Dec 2024 11:31:16 +0000 Subject: [PATCH 5/6] add tests for falsy values --- spec/core/matchers/toBeNullishSpec.js | 38 +++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spec/core/matchers/toBeNullishSpec.js b/spec/core/matchers/toBeNullishSpec.js index 29c41fdc..5f70d1a8 100644 --- a/spec/core/matchers/toBeNullishSpec.js +++ b/spec/core/matchers/toBeNullishSpec.js @@ -16,4 +16,42 @@ describe('toBeNullish', function() { const result = matcher.compare('foo'); expect(result.pass).toBe(false); }); + + describe('falsy values', () => { + it('fails for 0', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(0); + expect(result.pass).toBe(false); + }); + + it('fails for -0', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(-0); + expect(result.pass).toBe(false); + }); + + it('fails for empty string', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(''); + expect(result.pass).toBe(false); + }); + + it('fails for false', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(false); + expect(result.pass).toBe(false); + }); + + it('fails for NaN', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(NaN); + expect(result.pass).toBe(false); + }); + + it('fails for 0n', function() { + const matcher = jasmineUnderTest.matchers.toBeNullish(); + const result = matcher.compare(BigInt(0)); + expect(result.pass).toBe(false); + }); + }); }); From 36dd6b07d1551e01f39ba5c430897879288ee455 Mon Sep 17 00:00:00 2001 From: Matt McCherry Date: Tue, 10 Dec 2024 11:39:44 +0000 Subject: [PATCH 6/6] add integration test for a falsy value --- spec/core/integration/MatchersSpec.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spec/core/integration/MatchersSpec.js b/spec/core/integration/MatchersSpec.js index 223b4a16..ee8ce2c1 100755 --- a/spec/core/integration/MatchersSpec.js +++ b/spec/core/integration/MatchersSpec.js @@ -481,6 +481,10 @@ describe('Matchers (Integration)', function() { verifyFails(function(env) { env.expect(1).toBeNullish(); }); + + verifyFails(function(env) { + env.expect('').toBeNullish(); + }); }); describe('toContain', function() {