* Merges #2045 from @MattMcCherry
This commit is contained in:
Steve Gravrock
2024-12-12 17:29:27 -08:00
5 changed files with 120 additions and 0 deletions

View File

@@ -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).

View File

@@ -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) {

View File

@@ -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);
});
});
});

View File

@@ -18,6 +18,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
'toBeTrue',
'toBeTruthy',
'toBeUndefined',
'toBeNullish',
'toContain',
'toEqual',
'toHaveSize',

View File

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