From 3c3451b6f7baa875f6257f15d9ff83d926a50abd Mon Sep 17 00:00:00 2001 From: Bartosz Golebiowski Date: Mon, 7 Nov 2022 13:04:48 +0100 Subject: [PATCH] fix: align `.each` behavior with jest (#2064) * fix(vitest) align .each behavior with jest close #1858 * chore: update Co-authored-by: golebiowskib Co-authored-by: Anthony Fu --- packages/vitest/src/runtime/suite.ts | 12 +++++-- test/core/test/each.test.ts | 47 +++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/packages/vitest/src/runtime/suite.ts b/packages/vitest/src/runtime/suite.ts index 6f661de46b9d..e29c26cb93ae 100644 --- a/packages/vitest/src/runtime/suite.ts +++ b/packages/vitest/src/runtime/suite.ts @@ -180,12 +180,15 @@ function createSuite() { return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options) } - suiteFn.each = function(this: { withContext: () => SuiteAPI }, cases: ReadonlyArray) { + suiteFn.each = function (this: { withContext: () => SuiteAPI }, cases: ReadonlyArray) { const suite = this.withContext() return (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => { + const arrayOnlyCases = cases.every(Array.isArray) cases.forEach((i, idx) => { const items = Array.isArray(i) ? i : [i] - suite(formatTitle(name, items, idx), () => fn(...items), options) + arrayOnlyCases + ? suite(formatTitle(name, items, idx), () => fn(...items), options) + : suite(formatTitle(name, items, idx), () => fn(i), options) }) } } @@ -213,9 +216,12 @@ function createTest(fn: ( const test = this.withContext() return (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => { + const arrayOnlyCases = cases.every(Array.isArray) cases.forEach((i, idx) => { const items = Array.isArray(i) ? i : [i] - test(formatTitle(name, items, idx), () => fn(...items), options) + arrayOnlyCases + ? test(formatTitle(name, items, idx), () => fn(...items), options) + : test(formatTitle(name, items, idx), () => fn(i), options) }) } } diff --git a/test/core/test/each.test.ts b/test/core/test/each.test.ts index 354f0aa3f5df..fe156aa024d0 100644 --- a/test/core/test/each.test.ts +++ b/test/core/test/each.test.ts @@ -8,13 +8,6 @@ test.each([ expect(a + b).toBe(expected) }) -test.each([ - null, - [null], -])('null is null', (value) => { - expect(value).toBe(null) -}) - test.each([ ['string', true], ['string', false], @@ -136,3 +129,43 @@ describe('context with each - concurrent', () => { }) }) }) + +describe('not all arguments are array describe.each', () => { + const results = [null, [null]] + let i = 0 + + describe.each([null, [null]])('null is null', (value) => { + test('null is null', () => { + expect(value).toEqual(results[i++]) + }) + }) +}) + +describe('not all arguments are array test.each', () => { + const results = [ + null, + [null], + ] + let i = 0 + + test.each([ + null, + [null], + ])('matches results', (value) => { + expect(value).toEqual(results[i++]) + }) +}) + +test.each([ + null, +])('value is null', (value) => { + expect(value).toBeNull() +}) + +test.each([ + [null, null], + [null, null], +])('if all cases are arrays of equal length, treats array elements as arguments', (value1, value2) => { + expect(value1).toBeNull() + expect(value2).toBeNull() +})