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: add check to avoid converting 0 to undefined #802

Merged
merged 6 commits into from Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vitest/src/utils/base.ts
Expand Up @@ -53,7 +53,9 @@ export function clone<T>(val: T): T {
*/

export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
array = array || []
if (array === null || array === undefined)
array = []

if (Array.isArray(array))
return array
return [array]
Expand Down
7 changes: 7 additions & 0 deletions test/core/test/each.test.ts
Expand Up @@ -57,3 +57,10 @@ describe.each([
expect(a + b).not.toBeLessThan(expected)
})
})

// issue #794
describe.each([1, 2, 0])('%s (describe.each 1d)', (num) => {
test(`${num} is a number (describe.each 1d)`, () => {
expect(typeof num).toEqual('number')
})
})
23 changes: 22 additions & 1 deletion test/core/test/utils.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, test } from 'vitest'
import { assertTypes, deepMerge } from '../../../packages/vitest/src/utils'
import { assertTypes, deepMerge, toArray } from '../../../packages/vitest/src/utils'
import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils'

describe('assertTypes', () => {
Expand Down Expand Up @@ -96,3 +96,24 @@ describe('deepMerge', () => {
})
})
})

describe('toArray', () => {
test('number should be converted to array correctly', () => {
expect(toArray(0)).toEqual([0])
expect(toArray(1)).toEqual([1])
expect(toArray(2)).toEqual([2])
})

test('return empty array when given null or undefined', () => {
expect(toArray(null)).toEqual([])
expect(toArray(undefined)).toEqual([])
})

test('return the value as is when given the array', () => {
expect(toArray([1, 1, 2])).toEqual([1, 1, 2])
})

test('object should be stored in the array correctly', () => {
expect(toArray({ a: 1, b: 1, expected: 2 })).toEqual([{ a: 1, b: 1, expected: 2 }])
})
})