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

test.each's behaviour deviates from jest's #1858

Closed
6 tasks done
jfparadis-appomni opened this issue Aug 15, 2022 · 5 comments · Fixed by #2064
Closed
6 tasks done

test.each's behaviour deviates from jest's #1858

jfparadis-appomni opened this issue Aug 15, 2022 · 5 comments · Fixed by #2064

Comments

@jfparadis-appomni
Copy link

Describe the bug

As noted previously in #1461, there are significant differences in test.each() between vitest and jest.

Reproduction

  1. When invoking test.each() with an array containing null, e.g. test.each([null]), the value will be processed as an array containing null ([null]) on Jest and converted to a null on Vitest.

Vitest used to return an empty array in that scenario, but this was changed in #1462) and is enforced in this test:

test.each([
  null,
  [null],
])('null is null', (value) => {
  expect(value).toBe(null)
})

https://github.com/vitest-dev/vitest/blob/main/test/core/test/each.test.ts#L11

However, the same test will fail in Jest:

 FAIL  core/test/each.test.ts
  ●  null is null
    expect(received).toBe(expected) // Object.is equality

    Expected: null
    Received: [null]

      43 |       [null],
      44 |     ])('null is null', (value) => {
    > 45 |       expect(value).toBe(null)
         |                     ^
      46 |     })
      47 |
      48 |     it.each([true, false])(

      at core/test/each.test.ts:45:21

Test Suites: 1 failed, 0 passed, 1 total
Tests:       1 failed, 0 skipped, 0 passed, 1 total
  1. In addition, empty arrays are treated as empty arrays in Jest and undefined in Vitest:
test.each([
  true, false,
  0, 1234,
  new Date(),
  [], {}, () => {},
])('primitives', (value) => {
  console.log(value, typeof value);
});

On Jest, the output is:

      true boolean
      false boolean
      0 number
      1234 number
      2022-08-15T21:52:20.000Z object
      [] object // <-- Difference
      {} object
      [Function (anonymous)] function

On Vitrest, the output is:

      true boolean
      false boolean
      0 number
      1234 number
      2022-08-15T21:52:20.000Z object
      undefined undefined // <-- Difference
      {} object
      [Function (anonymous)] function

System Info

Binaries:
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
  Browsers:
    Chrome: 104.0.5112.79
    Safari: 15.6
  npmPackages:
    vite: 3.0.x => 3.0.7 
    vitest: 0.21.x => 0.21.1

Used Package Manager

npm

Validations

@bartoszgolebiowski
Copy link
Contributor

Should we align with jest behavior?

@williamthorsen
Copy link

Jest's behaviour is exactly what I'd expect, even if I didn't know anything about Jest.

What's the rationale for converting [] to undefined and [null] to null?

@bartoszgolebiowski
Copy link
Contributor

I am waiting for confirmation from core team. I think we should align to jest behavior. @sheremet-va

@sheremet-va
Copy link
Member

sheremet-va commented Sep 18, 2022

I am waiting for confirmation from core team. I think we should align to jest behavior. @sheremet-va

let's align the behavior.

@williamthorsen
Copy link

williamthorsen commented Sep 18, 2022

Clarification of Jest's behaviour (v29.0.3). This wasn't entirely as expected.

These tests pass:

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', (value) => {
  // typeof value: null
  expect(value).toBeNull()
})

These tests fail:

test.each([
  null,
  [null], // <-- FAIL: value is `[null]`
])('null value or null item is null', (value) => {
  // typeof value: null | any[]
  expect(value).toBeNull()
})

test.each([
  [null], // <-- FAIL: value is `[null]`
  null,
])('null value or null item is null', (value) => {
  // typeof value: null | any[]
  expect(value).toBeNull()
})

// ** FAIL: Test times out **
test.each([
  [null],
  [null, null],
])('if all cases are arrays, treats array elements as arguments', (value, _value?) => {
  expect(value).toBeNull()
})

bartoszgolebiowski pushed a commit to bartoszgolebiowski/vitest that referenced this issue Sep 20, 2022
antfu added a commit that referenced this issue Nov 7, 2022
* 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>
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants