Skip to content

Commit

Permalink
fix: add check to avoid converting 0 to undefined (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Feb 19, 2022
1 parent 1272e4f commit 2e1e9c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/vitest/src/utils/base.ts
Expand Up @@ -53,9 +53,12 @@ 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 }])
})
})

0 comments on commit 2e1e9c3

Please sign in to comment.