diff --git a/lib/jasmine-core/jasmine.js b/lib/jasmine-core/jasmine.js index 7f5e459d..960fd0cb 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', @@ -6011,6 +6012,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 51b5e148..b44a40a5 100755 --- a/spec/core/integration/MatchersSpec.js +++ b/spec/core/integration/MatchersSpec.js @@ -469,6 +469,24 @@ 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(); + }); + + verifyFails(function(env) { + env.expect('').toBeNullish(); + }); + }); + describe('toContain', function() { verifyPasses(function(env) { env.addCustomEqualityTester(function(a, b) { diff --git a/spec/core/matchers/toBeNullishSpec.js b/spec/core/matchers/toBeNullishSpec.js new file mode 100644 index 00000000..5f70d1a8 --- /dev/null +++ b/spec/core/matchers/toBeNullishSpec.js @@ -0,0 +1,57 @@ +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); + }); + + 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); + }); + }); +}); diff --git a/src/core/matchers/requireMatchers.js b/src/core/matchers/requireMatchers.js index c8fb4de3..aa636472 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', 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; +};