Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: align .each behavior with jest #2064

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/vitest/src/runtime/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,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 @@ -233,9 +236,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
Original file line number Diff line number Diff line change
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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe there are better names for these tests? there are 3 tests named null is null, but they check [null] === [null] 🤔

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()
})