Skip to content

Commit

Permalink
fix: align .each behavior with jest (#2064)
Browse files Browse the repository at this point in the history
* fix(vitest) align .each behavior with jest

close #1858

* chore: update

Co-authored-by: golebiowskib <bartosz.golebiowski@@ttpsc.pl>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people committed Nov 7, 2022
1 parent 90c1801 commit 3c3451b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
12 changes: 9 additions & 3 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -180,12 +180,15 @@ function createSuite() {
return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options)
}

suiteFn.each = function<T>(this: { withContext: () => SuiteAPI }, cases: ReadonlyArray<T>) {
suiteFn.each = function <T>(this: { withContext: () => SuiteAPI }, cases: ReadonlyArray<T>) {
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)
})
}
}
Expand Down Expand Up @@ -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)
})
}
}
Expand Down
47 changes: 40 additions & 7 deletions test/core/test/each.test.ts
Expand Up @@ -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],
Expand Down Expand Up @@ -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()
})

0 comments on commit 3c3451b

Please sign in to comment.