Files
n8n/packages/@n8n/utils/src/is-unknown-array.test.ts
T
f60cb87e0e chore: Extract the sixfold-duplicated isUnknownArray type guard into @n8n/utils (#38187)
Co-authored-by: n8n-cat-bot[bot] <n8n-cat-bot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-09 09:58:21 +00:00

32 lines
1003 B
TypeScript

import { describe, expect, expectTypeOf, it } from 'vitest';
import { isUnknownArray } from './is-unknown-array';
describe('isUnknownArray', () => {
it('returns true for arrays', () => {
expect(isUnknownArray([])).toBe(true);
expect(isUnknownArray([1, 'two', null])).toBe(true);
});
it('returns false for array-like and other objects', () => {
expect(isUnknownArray({ length: 1 })).toBe(false);
expect(isUnknownArray({})).toBe(false);
expect(isUnknownArray(new Set([1]))).toBe(false);
});
it('returns false for primitives and nullish values', () => {
expect(isUnknownArray('abc')).toBe(false);
expect(isUnknownArray(42)).toBe(false);
expect(isUnknownArray(null)).toBe(false);
expect(isUnknownArray(undefined)).toBe(false);
});
it('narrows elements to unknown rather than any', () => {
const value: unknown = ['a'];
if (isUnknownArray(value)) {
expectTypeOf(value).toEqualTypeOf<readonly unknown[]>();
expectTypeOf(value[0]).toEqualTypeOf<unknown>();
}
});
});