Skip to content

Commit cb062c4

Browse files
committed
test(array): replace example-based tests with PBT
- Verify array input returns same reference - Verify non-array non-nullish input wraps in single-element array - Verify null/undefined returns empty array - Verify result is always an array Property-based tests cover all original cases and more edge cases.
1 parent 2d65c5d commit cb062c4

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/utils/array.test.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
import { fc, test as fcTest } from '@fast-check/vitest';
12
import { toArray } from './array';
23

3-
describe('toArray', () => {
4-
it.each([
5-
[undefined, []],
6-
[null, []],
7-
[false, [false]],
8-
[0, [0]],
9-
['', ['']],
10-
[[], []],
11-
['foo', ['foo']],
12-
[['foo'], ['foo']],
13-
])('%s => %s', (input, expected) => {
14-
expect(toArray(input)).toEqual(expected);
4+
describe('toArray - Property-Based Tests', () => {
5+
fcTest.prop([fc.array(fc.anything())], { numRuns: 100 })(
6+
'array input returns the same array reference',
7+
(arr) => {
8+
expect(toArray(arr)).toBe(arr);
9+
},
10+
);
11+
12+
fcTest.prop([fc.anything().filter((x) => !Array.isArray(x) && x != null)], { numRuns: 100 })(
13+
'non-array non-nullish input returns single-element array',
14+
(value) => {
15+
const result = toArray(value);
16+
expect(result).toHaveLength(1);
17+
expect(result[0]).toBe(value);
18+
},
19+
);
20+
21+
fcTest.prop([fc.constantFrom(null, undefined)], { numRuns: 10 })(
22+
'null or undefined returns empty array',
23+
(value) => {
24+
expect(toArray(value)).toEqual([]);
25+
},
26+
);
27+
28+
fcTest.prop([fc.anything()], { numRuns: 100 })('result is always an array', (value) => {
29+
expect(Array.isArray(toArray(value))).toBe(true);
1530
});
1631
});

0 commit comments

Comments
 (0)