is/test/test.ts

2823 lines
70 KiB
TypeScript
Raw Normal View History

2026-04-07 17:38:43 +07:00
/* eslint-disable @typescript-eslint/no-empty-function, @stylistic/curly-newline, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/no-unsafe-argument */
import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import net from 'node:net';
import Stream from 'node:stream';
import {inspect} from 'node:util';
2026-04-07 17:38:43 +07:00
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {JSDOM} from 'jsdom';
2018-05-03 05:22:32 +02:00
import {Subject, Observable} from 'rxjs';
import {temporaryFile} from 'tempy';
import {expectTypeOf} from 'expect-type';
import ZenObservable from 'zen-observable';
2022-10-17 18:02:01 +07:00
import is, {
2026-04-07 17:38:43 +07:00
assert as isAssert,
assertPropertyKey,
type AssertionTypeDescription,
2026-04-09 00:31:33 +07:00
type NaN as NaNType,
type Predicate,
2022-10-17 18:02:01 +07:00
type Primitive,
type TypedArray,
type TypeName,
type UrlString,
2026-04-07 17:38:43 +07:00
} from '../source/index.ts';
import {keysOf} from '../source/utilities.ts';
2017-09-22 00:44:27 +07:00
class PromiseSubclassFixture<T> extends Promise<T> {}
2017-11-06 16:26:59 +01:00
class ErrorSubclassFixture extends Error {}
2017-09-22 00:44:27 +07:00
const {window} = new JSDOM();
const {document} = window;
2017-10-07 09:19:11 -07:00
type Test = Readonly<{
fixtures: unknown[];
2019-04-28 03:15:11 -05:00
typename?: TypeName;
typeDescription?: AssertionTypeDescription;
}>;
// Every entry should be unique and belongs in the most specific type for that entry
const reusableFixtures = {
asyncFunction: [async function () {}, async () => {}],
asyncGeneratorFunction: [
async function * () {},
async function * () {
yield 4;
},
],
boundFunction: [() => {}, function () {}.bind(null)], // eslint-disable-line no-extra-bind
buffer: [Buffer.from('🦄')],
emptyArray: [[], new Array()], // eslint-disable-line @typescript-eslint/no-array-constructor
emptyMap: [new Map()],
emptySet: [new Set()],
emptyString: ['', String()],
function: [
function foo() {}, // eslint-disable-line func-names
function () {},
],
generatorFunction: [
function * () {},
function * () {
yield 4;
},
],
infinite: [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY],
integer: [0, -0, 6],
nativePromise: [Promise.resolve(), PromiseSubclassFixture.resolve()],
number: [1.4],
numericString: ['5', '-3.2', 'Infinity', '0x56'],
plainObject: [
{x: 1},
Object.create(null),
new Object(), // eslint-disable-line no-object-constructor
structuredClone({x: 1}),
structuredClone(Object.create(null)),
structuredClone(new Object()), // eslint-disable-line no-object-constructor
],
promise: [Object.create({then() {}, catch() {}})], // eslint-disable-line unicorn/no-thenable
safeInteger: [(2 ** 53) - 1, -(2 ** 53) + 1],
} as const satisfies Partial<{[K in keyof typeof is]: unknown[]}>;
const primitiveTypes = {
undefined: {
2017-11-06 16:26:59 +01:00
fixtures: [
undefined,
2019-04-28 03:15:11 -05:00
],
typename: 'undefined',
},
null: {
2017-11-06 16:26:59 +01:00
fixtures: [
null,
2019-04-28 03:15:11 -05:00
],
typename: 'null',
},
string: {
2017-11-06 16:26:59 +01:00
fixtures: [
'🦄',
'hello world',
...reusableFixtures.emptyString,
...reusableFixtures.numericString,
2019-04-28 03:15:11 -05:00
],
typename: 'string',
},
emptyString: {
fixtures: [...reusableFixtures.emptyString],
typename: 'string',
typeDescription: 'empty string',
},
number: {
2017-11-06 16:26:59 +01:00
fixtures: [
...reusableFixtures.number,
...reusableFixtures.infinite,
...reusableFixtures.integer,
...reusableFixtures.safeInteger,
2019-04-28 03:15:11 -05:00
],
typename: 'number',
},
bigint: {
2019-11-07 15:56:02 +07:00
fixtures: [
1n,
0n,
-0n,
2026-04-07 17:38:43 +07:00
1234n,
2019-11-07 15:56:02 +07:00
],
typename: 'bigint',
},
boolean: {
2017-11-06 16:26:59 +01:00
fixtures: [
true, false,
2019-04-28 03:15:11 -05:00
],
typename: 'boolean',
},
numericString: {
fixtures: [...reusableFixtures.numericString],
typename: 'string',
typeDescription: 'string with a number',
},
nan: {
2017-11-06 16:26:59 +01:00
fixtures: [
NaN, // eslint-disable-line unicorn/prefer-number-properties
Number.NaN,
2019-04-28 03:15:11 -05:00
],
typename: 'NaN',
typeDescription: 'NaN',
},
nullOrUndefined: {
2018-11-01 13:21:49 +02:00
fixtures: [
null,
undefined,
2019-04-28 03:15:11 -05:00
],
typeDescription: 'null or undefined',
},
integer: {
fixtures: [...reusableFixtures.integer, ...reusableFixtures.safeInteger],
typename: 'number',
typeDescription: 'integer',
},
safeInteger: {
fixtures: [...reusableFixtures.integer, ...reusableFixtures.safeInteger],
typename: 'number',
2026-04-09 00:31:33 +07:00
typeDescription: 'safe integer',
},
infinite: {
fixtures: [...reusableFixtures.infinite],
typename: 'number',
typeDescription: 'infinite number',
},
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
const objectTypes = {
symbol: {
fixtures: [
Symbol('🦄'),
],
typename: 'symbol',
},
array: {
2017-11-06 16:26:59 +01:00
fixtures: [
[1, 2],
Array.from({length: 2}),
...reusableFixtures.emptyArray,
2019-04-28 03:15:11 -05:00
],
typename: 'Array',
},
emptyArray: {
fixtures: [...reusableFixtures.emptyArray],
typename: 'Array',
typeDescription: 'empty array',
},
function: {
2017-11-06 16:26:59 +01:00
fixtures: [
...reusableFixtures.asyncFunction,
...reusableFixtures.asyncGeneratorFunction,
...reusableFixtures.boundFunction,
...reusableFixtures.function,
...reusableFixtures.generatorFunction,
2019-04-28 03:15:11 -05:00
],
typename: 'Function',
},
buffer: {
fixtures: [...reusableFixtures.buffer],
typename: 'Buffer',
},
blob: {
fixtures: [
new window.Blob(),
],
typename: 'Blob',
},
object: {
2017-11-06 16:26:59 +01:00
fixtures: [
Object.create({x: 1}),
2026-04-09 00:31:33 +07:00
{[Symbol.toStringTag]: 'String'},
...reusableFixtures.plainObject,
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
},
regExp: {
2017-11-06 16:26:59 +01:00
fixtures: [
2026-04-07 17:38:43 +07:00
/\w/v,
// eslint-disable-next-line prefer-regex-literals
new RegExp(String.raw`\w`, 'v'),
2019-04-28 03:15:11 -05:00
],
typename: 'RegExp',
},
date: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Date(),
2019-04-28 03:15:11 -05:00
],
typename: 'Date',
},
error: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Error('🦄'),
new ErrorSubclassFixture(),
2019-04-28 03:15:11 -05:00
],
typename: 'Error',
},
nativePromise: {
fixtures: [...reusableFixtures.nativePromise],
typename: 'Promise',
typeDescription: 'native Promise',
},
promise: {
2017-11-06 16:26:59 +01:00
fixtures: [
...reusableFixtures.nativePromise,
...reusableFixtures.promise,
2019-04-28 03:15:11 -05:00
],
typename: 'Promise',
typeDescription: 'Promise',
},
generator: {
2017-11-06 16:26:59 +01:00
fixtures: [
2017-11-07 10:23:00 +07:00
(function * () {
yield 4;
})(),
2019-04-28 03:15:11 -05:00
],
typename: 'Generator',
},
asyncGenerator: {
fixtures: [
(async function * () {
yield 4;
})(),
],
typename: 'AsyncGenerator',
},
generatorFunction: {
fixtures: [...reusableFixtures.generatorFunction],
typename: 'Function',
typeDescription: 'GeneratorFunction',
},
asyncGeneratorFunction: {
fixtures: [...reusableFixtures.asyncGeneratorFunction],
typename: 'Function',
typeDescription: 'AsyncGeneratorFunction',
},
asyncFunction: {
fixtures: [...reusableFixtures.asyncFunction],
typename: 'Function',
typeDescription: 'AsyncFunction',
},
boundFunction: {
fixtures: [...reusableFixtures.boundFunction, ...reusableFixtures.asyncFunction],
typename: 'Function',
2026-04-09 00:31:33 +07:00
typeDescription: 'bound Function',
},
map: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Map([['one', '1']]),
...reusableFixtures.emptyMap,
2019-04-28 03:15:11 -05:00
],
typename: 'Map',
},
emptyMap: {
fixtures: [...reusableFixtures.emptyMap],
typename: 'Map',
typeDescription: 'empty map',
},
set: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Set(['one']),
...reusableFixtures.emptySet,
2019-04-28 03:15:11 -05:00
],
typename: 'Set',
},
emptySet: {
fixtures: [...reusableFixtures.emptySet],
typename: 'Set',
typeDescription: 'empty set',
},
weakSet: {
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakSet(),
2019-04-28 03:15:11 -05:00
],
typename: 'WeakSet',
},
weakRef: {
fixtures: [
new window.WeakRef({}),
],
typename: 'WeakRef',
},
weakMap: {
2017-11-06 16:26:59 +01:00
fixtures: [
new WeakMap(),
2019-04-28 03:15:11 -05:00
],
typename: 'WeakMap',
},
int8Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Int8Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Int8Array',
},
uint8Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint8Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Uint8Array',
},
uint8ClampedArray: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint8ClampedArray(),
2019-04-28 03:15:11 -05:00
],
typename: 'Uint8ClampedArray',
},
int16Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Int16Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Int16Array',
},
uint16Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint16Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Uint16Array',
},
int32Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Int32Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Int32Array',
},
uint32Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Uint32Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Uint32Array',
},
float32Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Float32Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Float32Array',
},
float64Array: {
2017-11-06 16:26:59 +01:00
fixtures: [
new Float64Array(),
2019-04-28 03:15:11 -05:00
],
typename: 'Float64Array',
},
bigInt64Array: {
2019-11-07 15:56:02 +07:00
fixtures: [
new BigInt64Array(),
2019-11-07 15:56:02 +07:00
],
typename: 'BigInt64Array',
},
bigUint64Array: {
2019-11-07 15:56:02 +07:00
fixtures: [
new BigUint64Array(),
2019-11-07 15:56:02 +07:00
],
typename: 'BigUint64Array',
},
arrayBuffer: {
2017-11-06 16:26:59 +01:00
fixtures: [
new ArrayBuffer(10),
2019-04-28 03:15:11 -05:00
],
typename: 'ArrayBuffer',
},
dataView: {
fixtures: [
new DataView(new ArrayBuffer(10)),
2019-04-28 03:15:11 -05:00
],
typename: 'DataView',
},
plainObject: {
2017-11-06 16:26:59 +01:00
fixtures: [
...reusableFixtures.plainObject,
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
typeDescription: 'plain object',
},
htmlElement: {
2017-11-06 16:26:59 +01:00
fixtures: [
'div',
'input',
'span',
'img',
'canvas',
'script',
]
.map(fixture => document.createElement(fixture)),
typeDescription: 'HTMLElement',
},
observable: {
fixtures: [
new Observable(),
new Subject(),
new ZenObservable(() => {}),
2019-04-28 03:15:11 -05:00
],
typename: 'Observable',
},
nodeStream: {
2017-11-19 15:16:06 -05:00
fixtures: [
fs.createReadStream('readme.md'),
fs.createWriteStream(temporaryFile()),
2017-11-19 15:16:06 -05:00
new net.Socket(),
new Stream.Duplex(),
new Stream.PassThrough(),
new Stream.Readable(),
new Stream.Transform(),
new Stream.Stream(),
new Stream.Writable(),
2019-04-28 03:15:11 -05:00
],
typename: 'Object',
typeDescription: 'Node.js Stream',
},
formData: {
2017-11-06 16:26:59 +01:00
fixtures: [
new window.FormData(),
2019-04-28 03:15:11 -05:00
],
typename: 'FormData',
},
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
const types = {
...objectTypes,
...primitiveTypes,
} as const satisfies Partial<{[K in keyof typeof is]: Test}>;
type TypeNameWithFixture = keyof typeof types;
const subClasses = new Map<TypeNameWithFixture, TypeNameWithFixture[]>([
['uint8Array', ['buffer']], // It's too hard to differentiate the two
['object', keysOf(objectTypes)],
2017-09-22 00:44:27 +07:00
]);
const notAssertionFixtures = {
bigint: {fixture: 1n, nonFixture: '🦄', typeDescription: 'bigint'},
boolean: {fixture: false, nonFixture: '🦄', typeDescription: 'boolean'},
null: {fixture: null, nonFixture: '🦄', typeDescription: 'null'},
nullOrUndefined: {fixtures: [null, undefined], nonFixture: '🦄', typeDescription: 'null or undefined'},
primitive: {fixtures: [false, null, undefined], nonFixture: [], typeDescription: 'primitive'},
string: {fixture: '🦄', nonFixture: 1, typeDescription: 'string'},
symbol: {fixture: Symbol('🦄'), nonFixture: '🦄', typeDescription: 'symbol'},
undefined: {fixture: undefined, nonFixture: null, typeDescription: 'undefined'},
} as const satisfies Record<keyof typeof isAssert.not, ({fixture: unknown} | {fixtures: unknown[]}) & {nonFixture: unknown; typeDescription: string}>;
2017-11-06 16:26:59 +01:00
// This ensures a certain method matches only the types it's supposed to and none of the other methods' types
2026-04-07 17:38:43 +07:00
for (const type of keysOf(types)) {
test(`is.${type}`, () => {
const {fixtures, typeDescription, typename} = types[type] as Test;
const valueType = typeDescription ?? typename ?? 'unspecified';
2017-11-06 16:26:59 +01:00
2026-04-07 17:38:43 +07:00
const testAssert: (value: unknown) => never | void = isAssert[type];
const testIs: Predicate = is[type];
2017-11-06 16:26:59 +01:00
for (const fixture of fixtures) {
2026-04-07 17:38:43 +07:00
assert.ok(testIs(fixture), `Value: ${inspect(fixture)}`);
assert.doesNotThrow(() => {
testAssert(fixture);
});
2017-11-06 16:26:59 +01:00
2026-04-07 17:38:43 +07:00
if (typename !== undefined) {
assert.strictEqual(is(fixture), typename);
}
2017-09-22 00:44:27 +07:00
}
for (const key of keysOf(types).filter(key => key !== type)) {
if (subClasses.has(type) && subClasses.get(type)?.includes(key)) {
continue;
}
2017-09-22 00:44:27 +07:00
for (let i = 0; i < types[key].fixtures.length; i += 1) {
const fixture: unknown = types[key].fixtures[i];
2020-02-22 02:01:58 +07:00
if (fixtures.includes(fixture)) {
continue;
}
2026-04-07 17:38:43 +07:00
assert.strictEqual(testIs(fixture), false, `${key}.fixture[${i}]: ${inspect(fixture)} should not be ${type}`);
assert.throws(() => {
2020-02-22 02:01:58 +07:00
testAssert(fixture);
}, {
message: `Expected value which is \`${valueType}\`, received value of type \`${is(fixture)}\`.`,
2020-02-22 02:01:58 +07:00
});
}
2017-09-22 00:44:27 +07:00
}
2026-04-07 17:38:43 +07:00
});
}
2017-09-22 00:44:27 +07:00
2026-04-07 17:38:43 +07:00
test('is.positiveNumber', () => {
assert.ok(is.positiveNumber(6));
assert.ok(is.positiveNumber(1.4));
assert.ok(is.positiveNumber(Number.POSITIVE_INFINITY));
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.positiveNumber(6);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.positiveNumber(1.4);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.positiveNumber(Number.POSITIVE_INFINITY);
});
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.positiveNumber(0), false);
assert.strictEqual(is.positiveNumber(-0), false);
assert.strictEqual(is.positiveNumber(-6), false);
assert.strictEqual(is.positiveNumber(-1.4), false);
assert.strictEqual(is.positiveNumber(Number.NEGATIVE_INFINITY), false);
2026-04-09 00:31:33 +07:00
assert.strictEqual(is.positiveNumber(Number.NaN), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.positiveNumber(0);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.positiveNumber(-0);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.positiveNumber(-6);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.positiveNumber(-1.4);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.positiveNumber(Number.NEGATIVE_INFINITY);
});
});
2026-04-09 00:31:33 +07:00
test('is.nan', () => {
assert.ok(is.nan(Number.NaN));
assert.ok(is.nan(NaN)); // eslint-disable-line unicorn/prefer-number-properties
assert.doesNotThrow(() => {
isAssert.nan(Number.NaN);
});
assert.strictEqual(is.nan(0), false);
assert.strictEqual(is.nan(-0), false);
assert.strictEqual(is.nan(1), false);
assert.strictEqual(is.nan(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.nan(Number.NEGATIVE_INFINITY), false);
assert.strictEqual(is.nan('NaN'), false);
assert.strictEqual(is.nan(undefined), false);
assert.throws(() => {
isAssert.nan(0);
});
assert.throws(() => {
isAssert.nan(1);
});
assert.throws(() => {
isAssert.nan('NaN');
});
});
test('is.finiteNumber', () => {
assert.ok(is.finiteNumber(6));
assert.ok(is.finiteNumber(-6));
assert.ok(is.finiteNumber(0));
assert.ok(is.finiteNumber(1.4));
assert.doesNotThrow(() => {
isAssert.finiteNumber(6);
});
assert.doesNotThrow(() => {
isAssert.finiteNumber(0);
});
assert.strictEqual(is.finiteNumber(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.finiteNumber(Number.NEGATIVE_INFINITY), false);
assert.strictEqual(is.finiteNumber(Number.NaN), false);
assert.throws(() => {
isAssert.finiteNumber(Number.POSITIVE_INFINITY);
});
assert.throws(() => {
isAssert.finiteNumber(Number.NEGATIVE_INFINITY);
});
assert.throws(() => {
isAssert.finiteNumber(Number.NaN);
});
});
2026-04-07 17:38:43 +07:00
test('is.negativeNumber', () => {
assert.ok(is.negativeNumber(-6));
assert.ok(is.negativeNumber(-1.4));
assert.ok(is.negativeNumber(Number.NEGATIVE_INFINITY));
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.negativeNumber(-6);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.negativeNumber(-1.4);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.negativeNumber(Number.NEGATIVE_INFINITY);
});
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.negativeNumber(0), false);
assert.strictEqual(is.negativeNumber(-0), false);
assert.strictEqual(is.negativeNumber(6), false);
assert.strictEqual(is.negativeNumber(1.4), false);
assert.strictEqual(is.negativeNumber(Number.POSITIVE_INFINITY), false);
2026-04-09 00:31:33 +07:00
assert.strictEqual(is.negativeNumber(Number.NaN), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.negativeNumber(0);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.negativeNumber(-0);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.negativeNumber(6);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.negativeNumber(1.4);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.negativeNumber(Number.POSITIVE_INFINITY);
});
});
test('is.nonNegativeNumber', () => {
assert.ok(is.nonNegativeNumber(0));
assert.ok(is.nonNegativeNumber(6));
assert.ok(is.nonNegativeNumber(1.4));
assert.ok(is.nonNegativeNumber(Number.POSITIVE_INFINITY));
assert.doesNotThrow(() => {
isAssert.nonNegativeNumber(0);
});
assert.doesNotThrow(() => {
isAssert.nonNegativeNumber(6);
});
assert.ok(is.nonNegativeNumber(-0)); // -0 >= 0 is true in JavaScript
assert.strictEqual(is.nonNegativeNumber(-6), false);
assert.strictEqual(is.nonNegativeNumber(-1.4), false);
assert.strictEqual(is.nonNegativeNumber(Number.NEGATIVE_INFINITY), false);
assert.strictEqual(is.nonNegativeNumber(Number.NaN), false);
assert.throws(() => {
isAssert.nonNegativeNumber(-6);
});
assert.throws(() => {
isAssert.nonNegativeNumber(Number.NEGATIVE_INFINITY);
});
});
test('is.positiveInteger', () => {
assert.ok(is.positiveInteger(1));
assert.ok(is.positiveInteger(6));
assert.ok(is.positiveInteger(100));
assert.doesNotThrow(() => {
isAssert.positiveInteger(1);
});
assert.doesNotThrow(() => {
isAssert.positiveInteger(6);
});
assert.strictEqual(is.positiveInteger(0), false);
assert.strictEqual(is.positiveInteger(-1), false);
assert.strictEqual(is.positiveInteger(1.5), false);
assert.strictEqual(is.positiveInteger(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.positiveInteger(Number.NaN), false);
assert.throws(() => {
isAssert.positiveInteger(0);
});
assert.throws(() => {
isAssert.positiveInteger(-1);
});
assert.throws(() => {
isAssert.positiveInteger(1.5);
});
});
test('is.negativeInteger', () => {
assert.ok(is.negativeInteger(-1));
assert.ok(is.negativeInteger(-6));
assert.ok(is.negativeInteger(-100));
assert.doesNotThrow(() => {
isAssert.negativeInteger(-1);
});
assert.doesNotThrow(() => {
isAssert.negativeInteger(-6);
});
assert.strictEqual(is.negativeInteger(0), false);
assert.strictEqual(is.negativeInteger(-0), false); // -0 < 0 is false in JavaScript
assert.strictEqual(is.negativeInteger(1), false);
assert.strictEqual(is.negativeInteger(-1.5), false);
assert.strictEqual(is.negativeInteger(Number.NEGATIVE_INFINITY), false);
assert.strictEqual(is.negativeInteger(Number.NaN), false);
assert.throws(() => {
isAssert.negativeInteger(0);
});
assert.throws(() => {
isAssert.negativeInteger(1);
});
assert.throws(() => {
isAssert.negativeInteger(-1.5);
});
assert.throws(() => {
isAssert.negativeInteger(Number.NEGATIVE_INFINITY);
});
});
test('is.nonNegativeInteger', () => {
assert.ok(is.nonNegativeInteger(0));
assert.ok(is.nonNegativeInteger(1));
assert.ok(is.nonNegativeInteger(100));
assert.doesNotThrow(() => {
isAssert.nonNegativeInteger(0);
});
assert.doesNotThrow(() => {
isAssert.nonNegativeInteger(1);
});
assert.ok(is.nonNegativeInteger(-0)); // -0 >= 0 is true in JavaScript
assert.strictEqual(is.nonNegativeInteger(-1), false);
assert.strictEqual(is.nonNegativeInteger(1.5), false);
assert.strictEqual(is.nonNegativeInteger(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.nonNegativeInteger(Number.NaN), false);
assert.throws(() => {
isAssert.nonNegativeInteger(-1);
});
assert.throws(() => {
isAssert.nonNegativeInteger(1.5);
});
assert.throws(() => {
isAssert.nonNegativeInteger(Number.POSITIVE_INFINITY);
});
});
2026-04-09 00:31:33 +07:00
test('is.infinite', () => {
assert.ok(is.infinite(Number.POSITIVE_INFINITY));
assert.ok(is.infinite(Number.NEGATIVE_INFINITY));
assert.doesNotThrow(() => {
isAssert.infinite(Number.POSITIVE_INFINITY);
});
assert.doesNotThrow(() => {
isAssert.infinite(Number.NEGATIVE_INFINITY);
});
assert.strictEqual(is.infinite(0), false);
assert.strictEqual(is.infinite(1), false);
assert.strictEqual(is.infinite(-1), false);
assert.strictEqual(is.infinite(Number.NaN), false);
assert.strictEqual(is.infinite(Number.MAX_VALUE), false);
assert.strictEqual(is.infinite('Infinity'), false);
assert.throws(() => {
isAssert.infinite(0);
});
assert.throws(() => {
isAssert.infinite(Number.NaN);
});
});
2026-04-07 17:38:43 +07:00
test('is.numericString supplemental', () => {
assert.strictEqual(is.numericString(''), false);
assert.strictEqual(is.numericString(' '), false);
assert.strictEqual(is.numericString(' \t\t\n'), false);
assert.strictEqual(is.numericString(1), false);
assert.strictEqual(is.numericString(' 5'), false);
assert.strictEqual(is.numericString('5 '), false);
assert.strictEqual(is.numericString(' 5 '), false);
assert.strictEqual(is.numericString('\t3'), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.numericString('');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.numericString(1);
2020-01-22 18:47:01 +07:00
});
2018-11-01 13:21:49 +02:00
});
2026-04-07 17:38:43 +07:00
test('is.array supplemental', () => {
assert.ok(is.array([1, 2, 3], is.number));
assert.strictEqual(is.array([1, '2', 3], is.number), false);
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.array([1, 2], isAssert.number);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.array([1, '2'], isAssert.number);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
const x: unknown[] = [1, 2, 3];
2026-04-07 17:38:43 +07:00
isAssert.array(x, isAssert.number);
2024-06-25 08:08:48 +08:00
x[0]?.toFixed(0);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
const x: unknown[] = [1, 2, 3];
if (is.array<number>(x, is.number)) {
2024-06-25 08:08:48 +08:00
x[0]?.toFixed(0);
}
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.array([1, '2'], isAssert.number, 'Expected numbers');
}, /Expected numbers/v);
2017-09-22 00:44:27 +07:00
});
test('is.arrayOf', () => {
const isStringArray = is.arrayOf(is.string);
assert.ok(isStringArray(['a', 'b', 'c']));
assert.ok(isStringArray([]));
assert.strictEqual(isStringArray([1, 2, 3]), false);
assert.strictEqual(isStringArray(['a', 1]), false);
assert.strictEqual(isStringArray('not an array'), false);
assert.strictEqual(isStringArray(undefined), false);
const isNumberArray = is.arrayOf(is.number);
assert.ok(isNumberArray([1, 2, 3]));
assert.strictEqual(isNumberArray([1, '2']), false);
});
test('is.oneOf', () => {
const isDirection = is.oneOf(['north', 'south', 'east', 'west'] as const);
assert.ok(isDirection('north'));
assert.ok(isDirection('west'));
assert.strictEqual(isDirection('up'), false);
assert.strictEqual(isDirection(1), false);
assert.strictEqual(isDirection(undefined), false);
const isSmallNumber = is.oneOf([1, 2, 3] as const);
assert.ok(isSmallNumber(1));
assert.strictEqual(isSmallNumber(4), false);
// Empty values array always returns false
const isNever = is.oneOf([] as const);
assert.strictEqual(isNever('anything'), false);
// Array.includes uses SameValueZero, so NaN matches NaN (unlike ===)
const isNanValue = is.oneOf([Number.NaN] as const);
assert.ok(isNanValue(Number.NaN));
});
2026-04-07 17:38:43 +07:00
test('is.boundFunction supplemental', () => {
assert.strictEqual(is.boundFunction(function () {}), false); // eslint-disable-line prefer-arrow-callback
2020-01-22 18:47:01 +07:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.boundFunction(function () {}); // eslint-disable-line prefer-arrow-callback
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
});
2026-04-07 17:38:43 +07:00
test('is.asyncFunction supplemental', () => {
const fixture = async () => {};
if (is.asyncFunction(fixture)) {
2026-04-07 17:38:43 +07:00
assert.ok(is.function(fixture().then));
2020-01-22 18:47:01 +07:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.function(fixture().then);
2020-01-22 18:47:01 +07:00
});
}
2019-03-31 21:50:37 +07:00
});
2017-09-22 00:44:27 +07:00
2026-04-07 17:38:43 +07:00
test('is.asyncGenerator supplemental', () => {
const fixture = (async function * () {
yield 4;
})();
if (is.asyncGenerator(fixture)) {
2026-04-07 17:38:43 +07:00
assert.ok(is.function(fixture.next));
}
});
2026-04-07 17:38:43 +07:00
test('is.asyncGeneratorFunction supplemental', () => {
const fixture = async function * () {
yield 4;
};
if (is.asyncGeneratorFunction(fixture)) {
2026-04-07 17:38:43 +07:00
assert.ok(is.function(fixture().next));
}
});
2026-04-07 17:38:43 +07:00
test('is.enumCase', () => {
enum NonNumericalEnum {
Key1 = 'key1',
Key2 = 'key2',
}
enum NumericKeyStringEnum {
// eslint-disable-next-line @stylistic/quote-props
'0' = 'zero',
'01' = 'padded',
}
2026-04-07 17:38:43 +07:00
assert.ok(is.enumCase('key1', NonNumericalEnum));
assert.doesNotThrow(() => {
isAssert.enumCase('key1', NonNumericalEnum);
});
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.enumCase('invalid', NonNumericalEnum), false);
assert.throws(() => {
isAssert.enumCase('invalid', NonNumericalEnum);
});
assert.ok(is.enumCase('zero', NumericKeyStringEnum));
assert.ok(is.enumCase('padded', NumericKeyStringEnum));
assert.doesNotThrow(() => {
isAssert.enumCase('zero', NumericKeyStringEnum);
});
assert.doesNotThrow(() => {
isAssert.enumCase('padded', NumericKeyStringEnum);
});
enum NumericalEnum {
Key1 = 0,
Key2 = 1,
}
assert.ok(is.enumCase(0, NumericalEnum));
assert.ok(is.enumCase(1, NumericalEnum));
assert.strictEqual(is.enumCase('Key1', NumericalEnum), false);
assert.strictEqual(is.enumCase('Key2', NumericalEnum), false);
assert.doesNotThrow(() => {
isAssert.enumCase(0, NumericalEnum);
});
assert.throws(() => {
isAssert.enumCase('Key1', NumericalEnum);
});
enum HeterogeneousEnum {
A = 1,
B = 'hello',
}
assert.ok(is.enumCase(1, HeterogeneousEnum));
assert.ok(is.enumCase('hello', HeterogeneousEnum));
assert.strictEqual(is.enumCase('A', HeterogeneousEnum), false);
});
2026-04-07 17:38:43 +07:00
test('is.directInstanceOf', () => {
const error = new Error('fixture');
2017-12-09 11:55:08 -05:00
const errorSubclass = new ErrorSubclassFixture();
2026-04-07 17:38:43 +07:00
assert.ok(is.directInstanceOf(error, Error));
assert.ok(is.directInstanceOf(errorSubclass, ErrorSubclassFixture));
assert.doesNotThrow(() => {
isAssert.directInstanceOf(error, Error);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.directInstanceOf(errorSubclass, ErrorSubclassFixture);
2020-01-22 18:47:01 +07:00
});
2017-12-09 11:55:08 -05:00
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.directInstanceOf(error, ErrorSubclassFixture), false);
assert.strictEqual(is.directInstanceOf(errorSubclass, Error), false);
assert.throws(() => {
isAssert.directInstanceOf(error, ErrorSubclassFixture);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.directInstanceOf(errorSubclass, Error);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.directInstanceOf(undefined, Error), false);
assert.strictEqual(is.directInstanceOf(null, Error), false);
2017-12-09 11:55:08 -05:00
});
2026-04-07 17:38:43 +07:00
test('is.urlInstance', () => {
2019-11-07 15:56:02 +07:00
const url = new URL('https://example.com');
2026-04-07 17:38:43 +07:00
assert.ok(is.urlInstance(url));
assert.strictEqual(is.urlInstance({}), false);
assert.strictEqual(is.urlInstance(undefined), false);
assert.strictEqual(is.urlInstance(null), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.urlInstance(url);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlInstance({});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlInstance(undefined);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlInstance(null);
2020-01-22 18:47:01 +07:00
});
2018-07-10 12:04:20 +03:00
});
2026-04-07 17:38:43 +07:00
test('is.urlString', () => {
2018-12-13 17:52:21 +02:00
const url = 'https://example.com';
2026-04-07 17:38:43 +07:00
assert.ok(is.urlString(url));
assert.strictEqual(is.urlString(new URL(url)), false);
assert.strictEqual(is.urlString({}), false);
assert.strictEqual(is.urlString(undefined), false);
assert.strictEqual(is.urlString(null), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.urlString(url);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlString(new URL(url));
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlString({});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlString(undefined);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlString(null);
2020-01-22 18:47:01 +07:00
});
2018-12-13 17:52:21 +02:00
});
// Type test for urlString narrowing fix (issue #212)
// This test demonstrates that the fix allows proper type narrowing in both branches
(() => {
const value: unknown = 'test';
if (is.urlString(value)) {
// ✅ In true branch: value is narrowed to UrlString
expectTypeOf(value).toEqualTypeOf<UrlString>();
expectTypeOf(value).toMatchTypeOf<string>();
} else {
// ✅ In false branch: value remains unknown (not incorrectly narrowed)
expectTypeOf(value).toEqualTypeOf<unknown>();
// ✅ Manual narrowing to string still works
if (typeof value === 'string') {
expectTypeOf(value).toEqualTypeOf<string>();
}
}
})();
2026-04-09 00:31:33 +07:00
// Type test for is.nan branded-type narrowing
(() => {
const value: unknown = Number.NaN;
if (is.nan(value)) {
// ✅ In true branch: value is narrowed to the branded NaN type
expectTypeOf(value).toEqualTypeOf<NaNType>();
expectTypeOf(value).toMatchTypeOf<number>();
} else {
// ✅ In false branch: value remains unknown (not incorrectly narrowed)
expectTypeOf(value).toEqualTypeOf<unknown>();
}
})();
2026-04-07 17:38:43 +07:00
test('is.truthy', () => {
assert.ok(is.truthy('unicorn'));
assert.ok(is.truthy('🦄'));
assert.ok(is.truthy(new Set()));
assert.ok(is.truthy(Symbol('🦄')));
assert.ok(is.truthy(true));
assert.ok(is.truthy(1));
assert.ok(is.truthy(1n));
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy('unicorn');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy('🦄');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy(new Set());
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy(Symbol('🦄'));
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy(true);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy(1);
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.truthy(1n);
});
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` narrow downs boolean type to `true`.
{
const booleans = [true, false];
const function_ = (value: true) => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(booleans[0]);
function_(booleans[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` excludes zero value from number type.
{
const bits: Array<0 | 1> = [1, 0, -0];
const function_ = (value: 1) => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(bits[0]);
function_(bits[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` excludes zero value from bigint type.
{
const bits: Array<0n | 1n> = [1n, 0n, -0n];
const function_ = (value: 1n) => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(bits[0]);
function_(bits[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` excludes empty string from string type.
{
const strings: Array<'nonEmpty' | ''> = ['nonEmpty', ''];
const function_ = (value: 'nonEmpty') => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(strings[0]);
function_(strings[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` excludes undefined from mixed type.
{
const maybeUndefineds = ['🦄', undefined];
const function_ = (value: string) => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(maybeUndefineds[0]);
function_(maybeUndefineds[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.truthy` excludes null from mixed type.
{
const maybeNulls = ['🦄', null];
const function_ = (value: string) => value;
2026-04-07 17:38:43 +07:00
isAssert.truthy(maybeNulls[0]);
function_(maybeNulls[0]);
}
2017-10-20 14:49:52 +00:00
});
2026-04-07 17:38:43 +07:00
test('is.falsy', () => {
assert.ok(is.falsy(false));
assert.ok(is.falsy(0));
assert.ok(is.falsy(''));
assert.ok(is.falsy(null));
assert.ok(is.falsy(undefined));
assert.ok(is.falsy(Number.NaN));
assert.ok(is.falsy(0n));
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(false);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(0);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy('');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(null);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(undefined);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(Number.NaN);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.falsy(0n);
});
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` narrow downs boolean type to `false`.
{
const booleans = [false, true];
2024-06-25 08:08:48 +08:00
const function_ = (value?: false) => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(booleans[0]);
function_(booleans[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` narrow downs number type to `0`.
{
const bits = [0, -0, 1];
2024-06-25 08:08:48 +08:00
const function_ = (value?: 0) => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(bits[0]);
function_(bits[0]);
2026-04-07 17:38:43 +07:00
isAssert.falsy(bits[1]);
function_(bits[1]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` narrow downs bigint type to `0n`.
{
const bits = [0n, -0n, 1n];
2024-06-25 08:08:48 +08:00
const function_ = (value?: 0n) => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(bits[0]);
function_(bits[0]);
2026-04-07 17:38:43 +07:00
isAssert.falsy(bits[1]);
function_(bits[1]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` narrow downs string type to empty string.
{
const strings = ['', 'nonEmpty'];
2024-06-25 08:08:48 +08:00
const function_ = (value?: '') => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(strings[0]);
function_(strings[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` can narrow down mixed type to undefined.
{
const maybeUndefineds = [undefined, Symbol('🦄')];
const function_ = (value: undefined) => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(maybeUndefineds[0]);
function_(maybeUndefineds[0]);
}
2026-04-07 17:38:43 +07:00
// Checks that `isAssert.falsy` can narrow down mixed type to null.
{
const maybeNulls = [null, Symbol('🦄')];
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line @typescript-eslint/no-restricted-types
2024-06-25 08:08:48 +08:00
const function_ = (value?: null) => value;
2026-04-07 17:38:43 +07:00
isAssert.falsy(maybeNulls[0]);
function_(maybeNulls[0]);
}
2017-10-20 14:49:52 +00:00
});
2026-04-07 17:38:43 +07:00
test('is.primitive', () => {
const primitives: Primitive[] = [
2017-09-22 00:44:27 +07:00
undefined,
null,
'🦄',
6,
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
2017-09-22 00:44:27 +07:00
true,
false,
Symbol('🦄'),
6n,
2017-09-22 00:44:27 +07:00
];
2020-01-22 18:47:01 +07:00
for (const element of primitives) {
2026-04-07 17:38:43 +07:00
assert.ok(is.primitive(element));
assert.doesNotThrow(() => {
isAssert.primitive(element);
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
}
});
2026-04-07 17:38:43 +07:00
test('is.integer supplemental', () => {
assert.strictEqual(is.integer(1.4), false);
assert.throws(() => {
isAssert.integer(1.4);
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
});
2026-04-07 17:38:43 +07:00
test('is.safeInteger supplemental', () => {
assert.strictEqual(is.safeInteger(2 ** 53), false);
assert.strictEqual(is.safeInteger(-(2 ** 53)), false);
assert.throws(() => {
isAssert.safeInteger(2 ** 53);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.safeInteger(-(2 ** 53));
2020-01-22 18:47:01 +07:00
});
2017-10-17 11:19:17 -04:00
});
2026-04-07 17:38:43 +07:00
test('is.iterable', () => {
assert.ok(is.iterable(''));
assert.ok(is.iterable([]));
assert.ok(is.iterable(new Map()));
assert.strictEqual(is.iterable(null), false);
assert.strictEqual(is.iterable(undefined), false);
assert.strictEqual(is.iterable(0), false);
assert.strictEqual(is.iterable(Number.NaN), false);
assert.strictEqual(is.iterable(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.iterable({}), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.iterable('');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.iterable([]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.iterable(new Map());
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable(null);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable(undefined);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable(0);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable(Number.NaN);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable(Number.POSITIVE_INFINITY);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.iterable({});
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
});
2026-04-07 17:38:43 +07:00
test('is.asyncIterable', () => {
assert.ok(is.asyncIterable({
[Symbol.asyncIterator]() {},
2019-11-07 15:56:02 +07:00
}));
2018-07-09 20:35:16 +03:00
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.asyncIterable(null), false);
assert.strictEqual(is.asyncIterable(undefined), false);
assert.strictEqual(is.asyncIterable(0), false);
assert.strictEqual(is.asyncIterable(Number.NaN), false);
assert.strictEqual(is.asyncIterable(Number.POSITIVE_INFINITY), false);
assert.strictEqual(is.asyncIterable({}), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.asyncIterable({
[Symbol.asyncIterator]() {},
2020-01-22 18:47:01 +07:00
});
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable(null);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable(undefined);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable(0);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable(Number.NaN);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable(Number.POSITIVE_INFINITY);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.asyncIterable({});
2020-01-22 18:47:01 +07:00
});
2019-11-07 15:56:02 +07:00
});
2018-07-09 20:35:16 +03:00
2026-04-07 17:38:43 +07:00
test('is.class', () => {
2019-03-31 20:41:19 +07:00
class Foo {} // eslint-disable-line @typescript-eslint/no-extraneous-class
2026-04-07 17:38:43 +07:00
// Note: Using new Function to test a minified class (no whitespace in source)
const minifiedClass = new Function('return class{};'); // eslint-disable-line no-new-func
2017-09-24 22:26:15 +03:00
const classDeclarations = [
Foo,
class Bar extends Foo {},
minifiedClass(),
2017-09-24 22:26:15 +03:00
];
2019-03-31 20:41:19 +07:00
for (const classDeclaration of classDeclarations) {
2026-04-07 17:38:43 +07:00
assert.ok(is.class(classDeclaration));
2020-01-22 18:47:01 +07:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.class(classDeclaration);
2020-01-22 18:47:01 +07:00
});
2017-09-24 22:26:15 +03:00
}
});
2026-04-07 17:38:43 +07:00
test('is.typedArray', () => {
const typedArrays: TypedArray[] = [
2019-03-31 21:50:37 +07:00
new Int8Array(),
new Uint8Array(),
new Uint8ClampedArray(),
2026-04-07 17:38:43 +07:00
new Int16Array(),
2019-03-31 21:50:37 +07:00
new Uint16Array(),
new Int32Array(),
new Uint32Array(),
new Float32Array(),
2019-11-07 15:56:02 +07:00
new Float64Array(),
new BigInt64Array(),
new BigUint64Array(),
2017-09-22 00:44:27 +07:00
];
2018-10-30 23:19:19 +07:00
for (const item of typedArrays) {
2026-04-07 17:38:43 +07:00
assert.ok(is.typedArray(item));
2020-01-22 18:47:01 +07:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.typedArray(item);
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
}
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.typedArray(new ArrayBuffer(1)), false);
assert.strictEqual(is.typedArray([]), false);
assert.strictEqual(is.typedArray({}), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.typedArray(new ArrayBuffer(1));
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.typedArray([]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.typedArray({});
2020-01-22 18:47:01 +07:00
});
2017-09-22 00:44:27 +07:00
});
2017-09-25 20:20:06 +01:00
2026-04-07 17:38:43 +07:00
test('is.arrayLike', () => {
2019-03-31 20:41:19 +07:00
(function () {
2026-04-07 17:38:43 +07:00
assert.ok(is.arrayLike(arguments)); // eslint-disable-line prefer-rest-params
2017-10-26 09:30:17 -04:00
})();
2019-03-31 20:41:19 +07:00
2026-04-07 17:38:43 +07:00
assert.ok(is.arrayLike([]));
assert.ok(is.arrayLike('unicorn'));
2017-10-26 09:30:17 -04:00
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.arrayLike({}), false);
assert.strictEqual(is.arrayLike(() => {}), false);
assert.strictEqual(is.arrayLike(new Map()), false);
2020-01-22 12:08:35 +01:00
(function () {
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(function () {
isAssert.arrayLike(arguments); // eslint-disable-line prefer-rest-params
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
})();
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.arrayLike([]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.arrayLike('unicorn');
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.arrayLike({});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.arrayLike(() => {});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.arrayLike(new Map());
2020-01-22 18:47:01 +07:00
});
2017-10-26 09:30:17 -04:00
});
2026-04-07 17:38:43 +07:00
test('is.tupleLike', () => {
(function () {
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.tupleLike(arguments, []), false); // eslint-disable-line prefer-rest-params
})();
2026-04-07 17:38:43 +07:00
assert.ok(is.tupleLike([], []));
assert.ok(is.tupleLike([1, '2', true, {}, [], undefined, null], [is.number, is.string, is.boolean, is.object, is.array, is.undefined, is.nullOrUndefined]));
assert.strictEqual(is.tupleLike('unicorn', [is.string]), false);
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.tupleLike({}, []), false);
assert.strictEqual(is.tupleLike(() => {}, [is.function]), false);
assert.strictEqual(is.tupleLike(new Map(), [is.map]), false);
(function () {
2026-04-07 17:38:43 +07:00
assert.throws(function () {
isAssert.tupleLike(arguments, []); // eslint-disable-line prefer-rest-params
});
})();
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.tupleLike([], []);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.tupleLike('unicorn', [is.string]);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.tupleLike({}, [is.object]);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.tupleLike(() => {}, [is.function]);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.tupleLike(new Map(), [is.map]);
});
{
const tuple = [[false, 'unicorn'], 'string', true];
if (is.tupleLike(tuple, [is.array, is.string, is.boolean])) {
if (is.tupleLike(tuple[0], [is.boolean, is.string])) { // eslint-disable-line unicorn/no-lonely-if
const value = tuple[0][1];
expectTypeOf(value).toEqualTypeOf<string>();
}
}
}
{
const tuple = [{isTest: true}, '1', true, null];
2023-08-07 08:50:03 +08:00
if (is.tupleLike(tuple, [is.nonEmptyObject, is.string, is.boolean, is.null])) {
const value = tuple[0];
expectTypeOf(value).toEqualTypeOf<Record<string | number | symbol, unknown>>();
}
}
{
const tuple = [1, '1', true, null, undefined];
2026-04-07 17:38:43 +07:00
if (is.tupleLike(tuple, [is.number, is.string, is.boolean, is.null, is.undefined])) {
const numericValue = tuple[0];
const stringValue = tuple[1];
const booleanValue = tuple[2];
2026-04-07 17:38:43 +07:00
const nullValue = tuple[3];
const undefinedValue = tuple[4];
expectTypeOf(numericValue).toEqualTypeOf<number>();
expectTypeOf(stringValue).toEqualTypeOf<string>();
expectTypeOf(booleanValue).toEqualTypeOf<boolean>();
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line @typescript-eslint/no-restricted-types
expectTypeOf(nullValue).toEqualTypeOf<null>();
2026-04-07 17:38:43 +07:00
expectTypeOf(undefinedValue).toEqualTypeOf<undefined>();
}
}
});
2026-04-07 17:38:43 +07:00
test('is.inRange', () => {
2017-09-25 20:20:06 +01:00
const x = 3;
2026-04-07 17:38:43 +07:00
assert.ok(is.inRange(x, [0, 5]));
assert.ok(is.inRange(x, [5, 0]));
assert.ok(is.inRange(x, [-5, 5]));
assert.ok(is.inRange(x, [5, -5]));
assert.strictEqual(is.inRange(x, [4, 8]), false);
assert.ok(is.inRange(-7, [-5, -10]));
assert.ok(is.inRange(-5, [-5, -10]));
assert.ok(is.inRange(-10, [-5, -10]));
assert.ok(is.inRange(x, 10));
assert.ok(is.inRange(0, 0));
assert.ok(is.inRange(-2, -3));
assert.strictEqual(is.inRange(x, 2), false);
assert.strictEqual(is.inRange(-3, -2), false);
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2018-10-30 23:19:19 +07:00
is.inRange(0, []);
});
2017-09-26 02:32:58 +07:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2018-10-30 23:19:19 +07:00
is.inRange(0, [5]);
2017-09-26 02:32:58 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2018-10-30 23:19:19 +07:00
is.inRange(0, [1, 2, 3]);
2017-09-26 02:32:58 +07:00
});
2020-01-22 12:08:35 +01:00
assert.throws(() => {
is.inRange(5, [Number.NaN, 10]);
}, TypeError);
assert.throws(() => {
is.inRange(5, [0, Number.NaN]);
}, TypeError);
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(x, [0, 5]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(x, [5, 0]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(x, [-5, 5]);
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(x, [5, -5]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.inRange(x, [4, 8]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(-7, [-5, -10]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(-5, [-5, -10]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(-10, [-5, -10]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(x, 10);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(0, 0);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.inRange(-2, -3);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.inRange(x, 2);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.inRange(-3, -2);
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2026-04-07 17:38:43 +07:00
isAssert.inRange(0, []);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2026-04-07 17:38:43 +07:00
isAssert.inRange(0, [5]);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2023-08-07 08:50:03 +08:00
// @ts-expect-error invalid argument
2026-04-07 17:38:43 +07:00
isAssert.inRange(0, [1, 2, 3]);
2020-01-22 12:08:35 +01:00
});
2017-09-25 20:20:06 +01:00
});
2017-10-05 00:18:25 -07:00
2026-04-07 17:38:43 +07:00
test('is.htmlElement supplemental', () => {
assert.strictEqual(is.htmlElement({nodeType: 1, nodeName: 'div'}), false);
assert.throws(() => {
isAssert.htmlElement({nodeType: 1, nodeName: 'div'});
2020-01-22 18:47:01 +07:00
});
2019-04-28 03:15:11 -05:00
const tagNames = [
'div',
'input',
'span',
'img',
'canvas',
'script',
] as const;
2019-04-28 03:15:11 -05:00
for (const tagName of tagNames) {
const element = document.createElement(tagName);
2026-04-07 17:38:43 +07:00
assert.strictEqual(is(element), 'HTMLElement');
2019-04-28 03:15:11 -05:00
}
2017-10-07 09:19:11 -07:00
const nonHtmlElements = [
document.createTextNode('data'),
document.createProcessingInstruction('xml-stylesheet', 'href="mycss.css" type="text/css"'),
document.createComment('This is a comment'),
document,
document.implementation.createDocumentType('svg:svg', '-//W3C//DTD SVG 1.1//EN', 'https://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'),
document.createDocumentFragment(),
] as const;
2017-11-19 15:16:06 -05:00
for (const element of nonHtmlElements) {
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.htmlElement(element);
});
}
2017-10-05 00:18:25 -07:00
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.evenInteger', () => {
for (const element of [-6, 2, 4]) {
2026-04-07 17:38:43 +07:00
assert.ok(is.evenInteger(element));
assert.doesNotThrow(() => {
isAssert.evenInteger(element);
2020-01-22 18:47:01 +07:00
});
2017-10-17 11:36:10 -04:00
}
for (const element of [-3, 1, 5]) {
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.evenInteger(element), false);
assert.throws(() => {
isAssert.evenInteger(element);
2020-01-22 18:47:01 +07:00
});
2017-10-17 11:36:10 -04:00
}
});
2026-04-07 17:38:43 +07:00
test('is.oddInteger', () => {
for (const element of [-5, 7, 13]) {
2026-04-07 17:38:43 +07:00
assert.ok(is.oddInteger(element));
assert.doesNotThrow(() => {
isAssert.oddInteger(element);
2020-01-22 18:47:01 +07:00
});
2017-10-17 11:36:10 -04:00
}
for (const element of [-8, 8, 10]) {
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.oddInteger(element), false);
assert.throws(() => {
isAssert.oddInteger(element);
2020-01-22 18:47:01 +07:00
});
2017-10-17 11:36:10 -04:00
}
});
2026-04-07 17:38:43 +07:00
test('is.nonEmptyArray', () => {
assert.ok(is.nonEmptyArray([1, 2, 3]));
assert.strictEqual(is.nonEmptyArray([]), false);
assert.strictEqual(is.nonEmptyArray(new Array()), false); // eslint-disable-line @typescript-eslint/no-array-constructor
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.nonEmptyArray([1, 2, 3]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyArray([]);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyArray(new Array()); // eslint-disable-line @typescript-eslint/no-array-constructor
2020-01-22 18:47:01 +07:00
});
2022-10-17 17:51:49 +07:00
{
const strings = ['🦄', 'unicorn'] as string[] | undefined;
const function_ = (value: string) => value;
if (is.nonEmptyArray(strings)) {
const value = strings[0];
function_(value);
}
}
{
const mixed = ['🦄', 'unicorn', 1, 2];
const function_ = (value: string | number) => value;
if (is.nonEmptyArray(mixed)) {
const value = mixed[0];
function_(value);
}
}
{
const arrays = [['🦄'], ['unicorn']];
const function_ = (value: string[]) => value;
if (is.nonEmptyArray(arrays)) {
const value = arrays[0];
function_(value);
}
}
{
const strings = ['🦄', 'unicorn'] as string[] | undefined;
const function_ = (value: string) => value;
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyArray(strings);
const value = strings[0];
function_(value);
}
{
const mixed = ['🦄', 'unicorn', 1, 2];
const function_ = (value: string | number) => value;
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyArray(mixed);
const value = mixed[0];
function_(value);
}
{
const arrays = [['🦄'], ['unicorn']];
const function_ = (value: string[]) => value;
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyArray(arrays);
const value = arrays[0];
function_(value);
}
2018-09-28 11:54:35 +05:30
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.emptyString supplemental', () => {
assert.strictEqual(is.emptyString('🦄'), false);
assert.throws(() => {
isAssert.emptyString('🦄');
2020-01-22 18:47:01 +07:00
});
2018-09-28 11:54:35 +05:30
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.emptyStringOrWhitespace supplemental', () => {
assert.ok(is.emptyStringOrWhitespace(' '));
assert.strictEqual(is.emptyStringOrWhitespace('🦄'), false);
assert.strictEqual(is.emptyStringOrWhitespace('unicorn'), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.emptyStringOrWhitespace(' ');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.emptyStringOrWhitespace('🦄');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.emptyStringOrWhitespace('unicorn');
2020-01-22 18:47:01 +07:00
});
let value = 'test'; // eslint-disable-line prefer-const -- can't use `const` here because then it will be inferred as `never` in the `if` block
if (is.emptyStringOrWhitespace(value)) {
value.charAt(0); // Should be inferred as `'' | Whitespace` and not `never`
} else {
value.charAt(0); // Should be inferred as `string` and not `never`
}
});
2026-04-07 17:38:43 +07:00
test('is.nonEmptyString', () => {
assert.strictEqual(is.nonEmptyString(''), false);
assert.strictEqual(is.nonEmptyString(String()), false);
assert.ok(is.nonEmptyString('🦄'));
2022-02-25 16:19:29 +07:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyString('');
2022-02-25 16:19:29 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyString(String());
2022-02-25 16:19:29 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.nonEmptyString('🦄');
2022-02-25 16:19:29 +07:00
});
});
2026-04-07 17:38:43 +07:00
test('is.nonEmptyStringAndNotWhitespace', () => {
assert.strictEqual(is.nonEmptyStringAndNotWhitespace(' '), false);
assert.ok(is.nonEmptyStringAndNotWhitespace('🦄'));
for (const value of [null, undefined, 5, Number.NaN, {}, []]) {
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.nonEmptyStringAndNotWhitespace(value), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyStringAndNotWhitespace(value);
});
}
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyStringAndNotWhitespace('');
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.nonEmptyStringAndNotWhitespace('🦄');
});
2018-09-28 11:54:35 +05:30
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.emptyObject', () => {
assert.ok(is.emptyObject({}));
assert.ok(is.emptyObject(new Object())); // eslint-disable-line no-object-constructor
assert.strictEqual(is.emptyObject({unicorn: '🦄'}), false);
assert.strictEqual(is.emptyObject(function () {}), false); // eslint-disable-line prefer-arrow-callback
assert.strictEqual(is.emptyObject(() => {}), false);
assert.strictEqual(is.emptyObject(class Foo {}), false); // eslint-disable-line @typescript-eslint/no-extraneous-class
assert.strictEqual(is.emptyObject([]), false);
assert.strictEqual(is.emptyObject(['unicorn']), false);
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.emptyObject({});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.emptyObject(new Object()); // eslint-disable-line no-object-constructor
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.emptyObject({unicorn: '🦄'});
2020-01-22 18:47:01 +07:00
});
assert.throws(() => {
isAssert.emptyObject(function () {}); // eslint-disable-line prefer-arrow-callback
});
2018-09-28 11:54:35 +05:30
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.nonEmptyObject', () => {
2018-10-30 23:19:19 +07:00
const foo = {};
is.nonEmptyObject(foo);
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.nonEmptyObject({}), false);
assert.strictEqual(is.nonEmptyObject(new Object()), false); // eslint-disable-line no-object-constructor
assert.ok(is.nonEmptyObject({unicorn: '🦄'}));
2020-01-22 12:08:35 +01:00
assert.strictEqual(is.nonEmptyObject([]), false);
assert.strictEqual(is.nonEmptyObject(['unicorn']), false);
const functionWithProperty = function () {};
(functionWithProperty as any).custom = 'value';
assert.strictEqual(is.nonEmptyObject(functionWithProperty), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyObject({});
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.nonEmptyObject(new Object()); // eslint-disable-line no-object-constructor
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.nonEmptyObject({unicorn: '🦄'});
2020-01-22 18:47:01 +07:00
});
2018-09-28 11:54:35 +05:30
});
2017-10-06 02:39:36 -05:00
2026-04-07 17:38:43 +07:00
test('is.nonEmptySet', () => {
const temporarySet = new Set();
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.nonEmptySet(temporarySet), false);
assert.throws(() => {
isAssert.nonEmptySet(temporarySet);
2020-01-22 18:47:01 +07:00
});
2017-10-06 02:39:36 -05:00
temporarySet.add(1);
2026-04-07 17:38:43 +07:00
assert.ok(is.nonEmptySet(temporarySet));
assert.doesNotThrow(() => {
isAssert.nonEmptySet(temporarySet);
2020-01-22 18:47:01 +07:00
});
2018-09-28 11:54:35 +05:30
});
2026-04-07 17:38:43 +07:00
test('is.nonEmptyMap', () => {
const temporaryMap = new Map();
2026-04-07 17:38:43 +07:00
assert.strictEqual(is.nonEmptyMap(temporaryMap), false);
assert.throws(() => {
isAssert.nonEmptyMap(temporaryMap);
2020-01-22 18:47:01 +07:00
});
2018-09-28 11:54:35 +05:30
temporaryMap.set('unicorn', '🦄');
2026-04-07 17:38:43 +07:00
assert.ok(is.nonEmptyMap(temporaryMap));
assert.doesNotThrow(() => {
isAssert.nonEmptyMap(temporaryMap);
2020-01-22 18:47:01 +07:00
});
2017-10-17 16:46:58 -04:00
});
2026-04-07 17:38:43 +07:00
test('is.propertyKey', () => {
assert.ok(is.propertyKey('key'));
assert.ok(is.propertyKey(42));
assert.ok(is.propertyKey(Symbol('')));
assert.strictEqual(is.propertyKey(null), false);
assert.strictEqual(is.propertyKey(undefined), false);
assert.strictEqual(is.propertyKey(true), false);
assert.strictEqual(is.propertyKey({}), false);
assert.strictEqual(is.propertyKey([]), false);
assert.strictEqual(is.propertyKey(new Map()), false);
assert.strictEqual(is.propertyKey(new Set()), false);
// AssertPropertyKey should narrow to PropertyKey (string | number | symbol), not just number
const symbolValue: unknown = Symbol('test');
assertPropertyKey(symbolValue);
expectTypeOf(symbolValue).toEqualTypeOf<PropertyKey>();
});
2026-04-07 17:38:43 +07:00
test('is.any', () => {
assert.ok(is.any(is.string, {}, true, '🦄'));
assert.ok(is.any(is.object, false, {}, 'unicorns'));
assert.strictEqual(is.any(is.boolean, '🦄', [], 3), false);
assert.strictEqual(is.any(is.integer, true, 'lol', {}), false);
assert.ok(is.any([is.string, is.number], {}, true, '🦄'));
assert.strictEqual(is.any([is.boolean, is.number], 'unicorns', [], new Map()), false);
assert.strictEqual(typeof is.any([is.string, is.number]), 'function');
assert.throws(() => {
2019-02-04 02:13:23 +07:00
is.any(null as any, true);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
is.any([], 'value');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2018-10-30 23:19:19 +07:00
is.any(is.string);
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.any(is.string, {}, true, '🦄');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.any(is.object, false, {}, 'unicorns');
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any([is.string, is.number]);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(is.boolean, '🦄', [], 3);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(is.integer, true, 'lol', {});
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(null as any, true);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any([], 'value');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(is.string);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(is.string, 1, 2, 3);
}, {
// Includes expected type and removes duplicates from received types:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string`. Received values of type `number`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(is.string, 1, [4]);
}, {
// Includes expected type and lists all received types:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string`. Received values of types `number` and `Array`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any([is.string, is.nullOrUndefined], 1);
}, {
// Handles array as first argument:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string` or `null or undefined`. Received values of type `number`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any([is.string, is.number, is.boolean], null, undefined, Number.NaN);
}, {
// Handles more than 2 expected and received types:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string`, `number`, or `boolean`. Received values of types `null`, `undefined`, and `NaN`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.any(() => false, 1);
}, {
// Default type assertion message
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `predicate returns truthy for any value`.', 'v'),
});
});
2026-04-07 17:38:43 +07:00
test('is.all', () => {
assert.ok(is.all(is.object, {}, new Set(), new Map()));
assert.ok(is.all(is.boolean, true, false));
assert.strictEqual(is.all(is.string, '🦄', []), false);
assert.strictEqual(is.all(is.set, new Map(), {}), false);
2026-04-07 17:38:43 +07:00
assert.ok(is.all(is.array, ['1'], ['2']));
assert.ok(is.all([is.string, is.nonEmptyString], '🦄', 'unicorns'));
assert.strictEqual(is.all([is.string, is.number], '🦄'), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2019-02-04 02:13:23 +07:00
is.all(null as any, true);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
is.all([], 'value');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
2018-10-30 23:19:19 +07:00
is.all(is.string);
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.all(is.object, {}, new Set(), new Map());
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.all(is.boolean, true, false);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all([is.string, is.number]);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.all([is.string, is.nonEmptyString], '🦄', 'unicorns');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(is.string, '🦄', []);
2020-01-22 18:47:01 +07:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all([is.string, is.number], '🦄');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(is.set, new Map(), {});
2020-01-22 18:47:01 +07:00
});
2020-01-22 12:08:35 +01:00
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(null as any, true);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all([], 'value');
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(is.string);
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(is.string, 1, 2, 3);
}, {
// Includes expected type and removes duplicates from received types:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string`. Received values of type `number`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(is.string, 1, [4]);
}, {
// Includes expected type and lists all received types:
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `string`. Received values of types `number` and `Array`.', 'v'),
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.all(() => false, 1);
}, {
// Default type assertion message
2026-04-07 17:38:43 +07:00
// eslint-disable-next-line prefer-regex-literals
message: new RegExp('Expected values which are `predicate returns truthy for all values`.', 'v'),
});
2020-01-22 12:08:35 +01:00
});
2026-04-07 17:38:43 +07:00
test('is.any as predicate factory', () => {
// Returns a type guard function when called with only predicates
const isStringOrNumber = is.any([is.string, is.number]);
2026-04-07 17:38:43 +07:00
assert.strictEqual(typeof isStringOrNumber, 'function');
assert.ok(isStringOrNumber('hello'));
assert.ok(isStringOrNumber(123));
assert.strictEqual(isStringOrNumber(true), false);
assert.strictEqual(isStringOrNumber({}), false);
2026-04-07 17:38:43 +07:00
// Type narrowing works correctly (compile-time check)
const value: unknown = 'test';
if (isStringOrNumber(value)) {
// TypeScript should narrow to string | number
const narrowed: string | number = value;
2026-04-07 17:38:43 +07:00
assert.ok(typeof narrowed === 'string' || typeof narrowed === 'number');
}
// Works with is.optional
2026-04-07 17:38:43 +07:00
assert.ok(is.optional(undefined, is.any([is.string, is.number])));
assert.ok(is.optional('test', is.any([is.string, is.number])));
assert.ok(is.optional(42, is.any([is.string, is.number])));
assert.strictEqual(is.optional(true, is.any([is.string, is.number])), false);
const predicateArray: Predicate[] = [is.string, is.number];
const isStringOrNumberFromArray = is.any(predicateArray);
2026-04-07 17:38:43 +07:00
assert.strictEqual(typeof isStringOrNumberFromArray, 'function');
assert.ok(isStringOrNumberFromArray('hello'));
assert.ok(isStringOrNumberFromArray(123));
assert.strictEqual(isStringOrNumberFromArray(true), false);
2026-04-07 17:38:43 +07:00
// Type narrowing with is.optional (compile-time check)
const optionalValue: unknown = undefined;
if (is.optional(optionalValue, is.any([is.string, is.number]))) {
// TypeScript should narrow to string | number | undefined
const narrowed: string | number | undefined = optionalValue;
2026-04-07 17:38:43 +07:00
assert.ok(narrowed === undefined || typeof narrowed === 'string' || typeof narrowed === 'number');
}
// Works with more predicates
const isStringOrNumberOrBoolean = is.any([is.string, is.number, is.boolean]);
2026-04-07 17:38:43 +07:00
assert.ok(isStringOrNumberOrBoolean('hello'));
assert.ok(isStringOrNumberOrBoolean(123));
assert.ok(isStringOrNumberOrBoolean(true));
assert.strictEqual(isStringOrNumberOrBoolean({}), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
is.any([is.string, 123 as any]);
});
});
2026-04-07 17:38:43 +07:00
test('is.all as predicate factory', () => {
// Returns a type guard function when called with only predicates
const isArrayAndNonEmpty = is.all([is.array, is.nonEmptyArray]);
2026-04-07 17:38:43 +07:00
assert.strictEqual(typeof isArrayAndNonEmpty, 'function');
assert.ok(isArrayAndNonEmpty(['hello']));
assert.strictEqual(isArrayAndNonEmpty([]), false);
assert.strictEqual(isArrayAndNonEmpty('hello'), false);
// Type narrowing works correctly
const value: unknown = ['test'];
if (isArrayAndNonEmpty(value)) {
// TypeScript should narrow to the intersection type
2026-04-07 17:38:43 +07:00
assert.ok(Array.isArray(value));
assert.ok(value.length > 0);
}
// Works with is.optional
2026-04-07 17:38:43 +07:00
assert.ok(is.optional(undefined, is.all([is.object, is.plainObject])));
assert.ok(is.optional({foo: 'bar'}, is.all([is.object, is.plainObject])));
assert.strictEqual(is.optional([], is.all([is.object, is.plainObject])), false);
2026-04-07 17:38:43 +07:00
assert.throws(() => {
is.all([is.string, 123 as any]);
});
});
2026-04-07 17:38:43 +07:00
test('is.formData supplemental', () => {
const data = new window.FormData();
2026-04-07 17:38:43 +07:00
assert.ok(is.formData(data));
assert.strictEqual(is.formData({}), false);
assert.strictEqual(is.formData(undefined), false);
assert.strictEqual(is.formData(null), false);
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.formData(data);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.formData({});
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.formData(undefined);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.formData(null);
});
});
2026-04-07 17:38:43 +07:00
test('is.urlSearchParams', () => {
const searchParameters = new URLSearchParams();
2026-04-07 17:38:43 +07:00
assert.ok(is.urlSearchParams(searchParameters));
assert.strictEqual(is.urlSearchParams({}), false);
assert.strictEqual(is.urlSearchParams(undefined), false);
assert.strictEqual(is.urlSearchParams(null), false);
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.urlSearchParams(searchParameters);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlSearchParams({});
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlSearchParams(undefined);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.urlSearchParams(null);
});
});
2026-04-07 17:38:43 +07:00
test('is.validDate', () => {
assert.ok(is.validDate(new Date()));
assert.strictEqual(is.validDate(new Date('x')), false);
assert.doesNotThrow(() => {
isAssert.validDate(new Date());
2024-02-29 08:23:30 +01:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.validDate(new Date('x'));
2024-02-29 08:23:30 +01:00
});
});
2026-04-07 17:38:43 +07:00
test('is.validLength', () => {
assert.ok(is.validLength(1));
assert.ok(is.validLength(0));
assert.strictEqual(is.validLength(-1), false);
assert.strictEqual(is.validLength(0.1), false);
assert.doesNotThrow(() => {
isAssert.validLength(1);
2023-08-07 08:50:03 +08:00
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.validLength(-1);
});
assert.throws(() => {
isAssert.validLength(0.1);
2023-08-07 08:50:03 +08:00
});
});
2026-04-07 17:38:43 +07:00
test('is.whitespaceString', () => {
assert.ok(is.whitespaceString(' '));
assert.ok(is.whitespaceString(' '));
assert.ok(is.whitespaceString('   '));
assert.ok(is.whitespaceString('\u3000'));
assert.ok(is.whitespaceString(' '));
assert.strictEqual(is.whitespaceString(''), false);
assert.strictEqual(is.whitespaceString('-'), false);
assert.strictEqual(is.whitespaceString(' hi '), false);
assert.doesNotThrow(() => {
isAssert.whitespaceString(' ');
});
assert.throws(() => {
isAssert.whitespaceString('');
});
assert.throws(() => {
isAssert.whitespaceString(' hi ');
});
2023-08-07 08:50:03 +08:00
});
2026-04-07 17:38:43 +07:00
test('assert', () => {
// Contrived test showing that TypeScript acknowledges the type assertion in `isAssert.number()`.
// Real-world usage includes asserting user input, but here we use a random number/string generator.
2020-01-22 12:08:35 +01:00
const getNumberOrStringRandomly = (): number | string => {
2020-01-22 18:47:01 +07:00
const random = Math.random();
2020-01-22 12:08:35 +01:00
2020-01-22 18:47:01 +07:00
if (random < 0.5) {
2020-01-22 12:08:35 +01:00
return 'sometimes this function returns text';
}
2020-01-22 18:47:01 +07:00
return random;
2020-01-22 12:08:35 +01:00
};
const canUseOnlyNumber = (badlyTypedArgument: any): number => {
// Narrow the type to number, or throw an error at runtime for non-numbers.
2026-04-07 17:38:43 +07:00
isAssert.number(badlyTypedArgument);
2020-01-22 12:08:35 +01:00
// Both the type and runtime value is number.
return 1000 * badlyTypedArgument;
};
const badlyTypedVariable: any = getNumberOrStringRandomly();
2026-04-07 17:38:43 +07:00
assert.ok(is.number(badlyTypedVariable) || is.string(badlyTypedVariable));
2020-01-22 12:08:35 +01:00
// Using try/catch for test purposes only.
try {
const result = canUseOnlyNumber(badlyTypedVariable);
// Got lucky, the input was a number yielding a good result.
2026-04-07 17:38:43 +07:00
assert.ok(is.number(result));
2020-01-22 12:08:35 +01:00
} catch {
// Assertion was tripped.
2026-04-07 17:38:43 +07:00
assert.ok(is.string(badlyTypedVariable));
2020-01-22 12:08:35 +01:00
}
});
2026-04-07 17:38:43 +07:00
test('custom assertion message', () => {
const message = 'Custom error message';
2026-04-09 00:31:33 +07:00
const assertThrowsTypeErrorWithMessage = (assertion: () => void) => {
// `node:assert` does not verify the error class when matching only on `{message}`.
assert.throws(assertion, error => {
assert.ok(error instanceof TypeError);
assert.strictEqual(error.message, message);
return true;
});
};
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.array(undefined, undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.arrayBuffer(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.arrayLike(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.asyncFunction(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.asyncGenerator(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.asyncGeneratorFunction(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.asyncIterable(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.bigInt64Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.bigUint64Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.bigint(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.blob(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.boolean(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.boundFunction(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.buffer(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.class(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.dataView(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.date(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.directInstanceOf(undefined, Error, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptyArray(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptyMap(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptyObject(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptySet(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptyString(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.emptyStringOrWhitespace(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
enum Enum {}
2026-04-07 17:38:43 +07:00
isAssert.enumCase('invalid', Enum, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.error(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.evenInteger(33, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.falsy(true, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
isAssert.finiteNumber(Number.POSITIVE_INFINITY, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.float32Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.float64Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.formData(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.function(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.generator(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.generatorFunction(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.htmlElement(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.inRange(5, [1, 2], message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.infinite(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.int16Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.int32Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.int8Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.integer(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.iterable(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.map(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nan(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nativePromise(undefined, message);
2026-04-09 00:31:33 +07:00
});
assertThrowsTypeErrorWithMessage(() => {
isAssert.not.undefined(undefined, message);
});
assertThrowsTypeErrorWithMessage(() => {
isAssert.not.string('hello', message);
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.negativeNumber(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nodeStream(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyArray(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyMap(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyObject(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptySet(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyString(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nonEmptyStringAndNotWhitespace(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
isAssert.nonNegativeNumber(-1, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.null(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.nullOrUndefined(false, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.number(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.numericString(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.object(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.observable(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.oddInteger(42, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.plainObject(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
isAssert.positiveInteger(0, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.positiveNumber(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.primitive([], message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.promise(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.propertyKey(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.regExp(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.safeInteger(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.set(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.sharedArrayBuffer(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.string(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.symbol(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.truthy(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.tupleLike(undefined, [], message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.typedArray(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.uint16Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.uint32Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.uint8Array(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.uint8ClampedArray(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.undefined(false, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.urlInstance(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.urlSearchParams(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.urlString(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.validDate(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.validLength(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.weakMap(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.weakRef(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.weakSet(undefined, message);
2026-04-09 00:31:33 +07:00
});
2026-04-09 00:31:33 +07:00
assertThrowsTypeErrorWithMessage(() => {
2026-04-07 17:38:43 +07:00
isAssert.whitespaceString(undefined, message);
2026-04-09 00:31:33 +07:00
});
});
test('isAssert.not.undefined', () => {
assert.throws(() => {
isAssert.not.undefined(undefined);
}, {
message: 'Expected value which is not `undefined`, received value of type `undefined`.',
});
assert.doesNotThrow(() => {
isAssert.not.undefined(null);
});
assert.doesNotThrow(() => {
isAssert.not.undefined(false);
});
assert.doesNotThrow(() => {
isAssert.not.undefined(0);
});
assert.doesNotThrow(() => {
isAssert.not.undefined('');
});
});
test('isAssert.not', () => {
assert.deepStrictEqual(new Set(keysOf(isAssert.not)), new Set(keysOf(notAssertionFixtures)));
for (const type of keysOf(notAssertionFixtures)) {
const {nonFixture, typeDescription} = notAssertionFixtures[type];
const testAssert = isAssert.not[type];
const fixtures = 'fixtures' in notAssertionFixtures[type] ? notAssertionFixtures[type].fixtures : [notAssertionFixtures[type].fixture];
for (const fixture of fixtures) {
assert.throws(() => {
testAssert(fixture);
}, {
message: `Expected value which is not \`${typeDescription}\`, received value of type \`${is(fixture)}\`.`,
});
}
assert.doesNotThrow(() => {
testAssert(nonFixture);
});
}
assert.strictEqual('number' in isAssert.not, false);
assert.strictEqual('integer' in isAssert.not, false);
assert.strictEqual('object' in isAssert.not, false);
assert.strictEqual('blob' in isAssert.not, false);
assert.strictEqual('array' in isAssert.not, false);
assert.strictEqual('date' in isAssert.not, false);
assert.strictEqual('function' in isAssert.not, false);
assert.strictEqual('map' in isAssert.not, false);
assert.strictEqual('set' in isAssert.not, false);
});
test('isAssert.not edge cases', () => {
assert.doesNotThrow(() => {
isAssert.not.null(undefined);
});
});
2026-04-07 17:38:43 +07:00
test('is.optional', () => {
assert.ok(is.optional(undefined, is.string));
assert.ok(is.optional('🦄', is.string));
assert.strictEqual(is.optional(123, is.string), false);
assert.strictEqual(is.optional(null, is.string), false);
});
2026-04-07 17:38:43 +07:00
test('isAssert.optional', () => {
assert.doesNotThrow(() => {
isAssert.optional(undefined, isAssert.string);
});
2026-04-07 17:38:43 +07:00
assert.doesNotThrow(() => {
isAssert.optional('🦄', isAssert.string);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.optional(123, isAssert.string);
});
2026-04-07 17:38:43 +07:00
assert.throws(() => {
isAssert.optional(null, isAssert.string);
});
});