diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index ce44a3303098..41c56913d177 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -53,9 +53,12 @@ export function clone(val: T): T { */ export function toArray(array?: Nullable>): Array { - array = array || [] + if (array === null || array === undefined) + array = [] + if (Array.isArray(array)) return array + return [array] } diff --git a/test/core/test/each.test.ts b/test/core/test/each.test.ts index 79b16365b85c..c495c294c3c7 100644 --- a/test/core/test/each.test.ts +++ b/test/core/test/each.test.ts @@ -57,3 +57,10 @@ describe.each([ expect(a + b).not.toBeLessThan(expected) }) }) + +// issue #794 +describe.each([1, 2, 0])('%s (describe.each 1d)', (num) => { + test(`${num} is a number (describe.each 1d)`, () => { + expect(typeof num).toEqual('number') + }) +}) diff --git a/test/core/test/utils.spec.ts b/test/core/test/utils.spec.ts index ac0cb0abcec1..70dc674ce1a1 100644 --- a/test/core/test/utils.spec.ts +++ b/test/core/test/utils.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from 'vitest' -import { assertTypes, deepMerge } from '../../../packages/vitest/src/utils' +import { assertTypes, deepMerge, toArray } from '../../../packages/vitest/src/utils' import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils' describe('assertTypes', () => { @@ -96,3 +96,24 @@ describe('deepMerge', () => { }) }) }) + +describe('toArray', () => { + test('number should be converted to array correctly', () => { + expect(toArray(0)).toEqual([0]) + expect(toArray(1)).toEqual([1]) + expect(toArray(2)).toEqual([2]) + }) + + test('return empty array when given null or undefined', () => { + expect(toArray(null)).toEqual([]) + expect(toArray(undefined)).toEqual([]) + }) + + test('return the value as is when given the array', () => { + expect(toArray([1, 1, 2])).toEqual([1, 1, 2]) + }) + + test('object should be stored in the array correctly', () => { + expect(toArray({ a: 1, b: 1, expected: 2 })).toEqual([{ a: 1, b: 1, expected: 2 }]) + }) +})