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; +};