Add is.optional and assert.optional

Fixes #111
This commit is contained in:
Sindre Sorhus 2025-09-13 02:27:50 +07:00
parent c68ad76062
commit 1f2440ae0d
3 changed files with 76 additions and 0 deletions

View file

@ -2251,3 +2251,28 @@ test('custom assertion message', t => {
assert.whitespaceString(undefined, message);
}, {instanceOf: TypeError, message});
});
test('is.optional', t => {
t.true(is.optional(undefined, is.string));
t.true(is.optional('🦄', is.string));
t.false(is.optional(123, is.string));
t.false(is.optional(null, is.string));
});
test('assert.optional', t => {
t.notThrows(() => {
assert.optional(undefined, assert.string);
});
t.notThrows(() => {
assert.optional('🦄', assert.string);
});
t.throws(() => {
assert.optional(123, assert.string);
});
t.throws(() => {
assert.optional(null, assert.string);
});
});