From 5cf26159524a4244627a1b194b9521e9e2bdfcdb Mon Sep 17 00:00:00 2001 From: togami2864 Date: Sat, 19 Feb 2022 22:56:36 +0900 Subject: [PATCH 1/6] fix: add unidefined check to avoid converting 0 to undefined --- packages/vitest/src/utils/base.ts | 2 +- test/core/test/each.test.ts | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index ce44a3303098..82ebf0f78958 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -53,7 +53,7 @@ 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..7d1a72b45160 100644 --- a/test/core/test/each.test.ts +++ b/test/core/test/each.test.ts @@ -57,3 +57,9 @@ describe.each([ expect(a + b).not.toBeLessThan(expected) }) }) + +describe.each([1, 2, 0])('%s (describe.each 1d)', (num) => { + test(`${num} is a number (describe.each 1d)`, () => { + expect(typeof num).toEqual('number') + }) +}) From a8ff14db16d343899ed1af26d1a11e8e8ea44761 Mon Sep 17 00:00:00 2001 From: togami2864 Date: Sat, 19 Feb 2022 22:58:55 +0900 Subject: [PATCH 2/6] test: add to-array test --- test/core/test/each.test.ts | 1 + test/core/test/utils.spec.ts | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/core/test/each.test.ts b/test/core/test/each.test.ts index 7d1a72b45160..c495c294c3c7 100644 --- a/test/core/test/each.test.ts +++ b/test/core/test/each.test.ts @@ -58,6 +58,7 @@ describe.each([ }) }) +// 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 }]) + }) +}) From 1685c1bea94b119fa31da204bf4c7aa0b345851f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 19 Feb 2022 15:24:16 +0100 Subject: [PATCH 3/6] chore: use ts shortcut to assign array --- packages/vitest/src/utils/base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index 82ebf0f78958..dc52d9267f41 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -53,7 +53,7 @@ export function clone(val: T): T { */ export function toArray(array?: Nullable>): Array { - if (array === null || array === undefined) array = [] + array ??= [] if (Array.isArray(array)) return array return [array] From 9d6ff7d337378af45ea985972108453124f253a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 19 Feb 2022 15:46:19 +0100 Subject: [PATCH 4/6] chore: revert ts shortcut for array --- packages/vitest/src/utils/base.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index dc52d9267f41..8542600450b9 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -53,7 +53,9 @@ export function clone(val: T): T { */ export function toArray(array?: Nullable>): Array { - array ??= [] + if (array === null || array === undefined) + array = [] + if (Array.isArray(array)) return array return [array] From 51b26ad8dc990dd7fb7b95210717feecfff851d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 19 Feb 2022 15:51:42 +0100 Subject: [PATCH 5/6] cleanup: remove trailing spaces --- packages/vitest/src/utils/base.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index 8542600450b9..4d3303743a33 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -53,11 +53,12 @@ export function clone(val: T): T { */ export function toArray(array?: Nullable>): Array { - if (array === null || array === undefined) + if (array === null || array === undefined) array = [] if (Array.isArray(array)) return array + return [array] } From cded36b77ec837e14076e2f9482186ebafc14eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaqu=C3=ADn=20S=C3=A1nchez?= Date: Sat, 19 Feb 2022 16:00:40 +0100 Subject: [PATCH 6/6] fix lint --- packages/vitest/src/utils/base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vitest/src/utils/base.ts b/packages/vitest/src/utils/base.ts index 4d3303743a33..41c56913d177 100644 --- a/packages/vitest/src/utils/base.ts +++ b/packages/vitest/src/utils/base.ts @@ -55,7 +55,7 @@ export function clone(val: T): T { export function toArray(array?: Nullable>): Array { if (array === null || array === undefined) array = [] - + if (Array.isArray(array)) return array