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

it.each/describe.each break when single-dimensional array contains 0 (numeric zero) #794

Closed
6 tasks done
mgdodge opened this issue Feb 18, 2022 · 0 comments · Fixed by #802
Closed
6 tasks done

it.each/describe.each break when single-dimensional array contains 0 (numeric zero) #794

mgdodge opened this issue Feb 18, 2022 · 0 comments · Fixed by #802

Comments

@mgdodge
Copy link

mgdodge commented Feb 18, 2022

Describe the bug

Migrating tests from jest to Vitest, the way describe.each and it.each function is buggy. According to the jest docs, using describe.each or it.each should internally convert a single-dimensional array to a multidimensional array. For example, [1, 2, 0] would be converted to [[1], [2], [0]]. This is a very common scenario for simple tests.

Unfortunately, using the single-dimensional array syntax with Vitest v0.4.1, testing with [1, 2, 0] fails when running the test for 0 (numeric zero). It seems that the value gets converted to undefined!

I did find the comment in #640 indicating that template literal usage is intentionally left out, but it makes no mention of single-dimensional arrays, which I see as being far more common. If single-dimensional arrays are also being omitted, then I think the docs need to be updated to call out these differences.

Reproduction

[1, 2, 0].forEach((num) => {
	it(`${num} is a number (foreach)`, () => {
		expect(typeof num).toEqual('number');
	});
});

it.each([1, 2, 0])('%s is a number (it.each 1d)', (num) => {
	expect(typeof num).toEqual('number');
});

it.each([
	[1],
	[2],
	[0],
])('%s is a number (it.each 2d)', (num) => {
	expect(typeof num).toEqual('number');
});

describe.each([1, 2, 0])('%s (describe.each 1d)', (num) => {
	it(`${num} is a number (describe.each 1d)`, () => {
		expect(typeof num).toEqual('number');
	});
});

describe.each([
	[1],
	[2],
	[0],
])('%s (describe.each 2d)', (num) => {
	it(`${num} is a number (describe.each 2d)`, () => {
		expect(typeof num).toEqual('number');
	});
});
     √ 1 is a number (foreach)
     √ 2 is a number (foreach)
     √ 0 is a number (foreach)
     √ 1 is a number (it.each 1d)
     √ 2 is a number (it.each 1d)
     × %s is a number (it.each 1d)
     √ 1 is a number (it.each 2d)
     √ 2 is a number (it.each 2d)
     √ 0 is a number (it.each 2d)
     √ 1 (describe.each 1d) (1)
     √ 2 (describe.each 1d) (1)
     ❯ %s (describe.each 1d) (1)
       × undefined is a number (describe.each 1d)
     √ 1 (describe.each 2d) (1)
     √ 2 (describe.each 2d) (1)
     √ 0 (describe.each 2d) (1)

Failing tests result in AssertionError: expected 'undefined' to deeply equal 'number'

System Info

  System:
    OS: Linux 5.10 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i9-11950H @ 2.60GHz
    Memory: 25.69 GB / 31.36 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
    npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
  npmPackages:
    @vitejs/plugin-vue: ^2.2.2 => 2.2.2
    vite: ^2.8.4 => 2.8.4
    vitest: ^0.4.1 => 0.4.1

Used Package Manager

npm

Validations

chaii3 pushed a commit to chaii3/vitest that referenced this issue May 13, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant