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

feat: support BigInt for jest assertions #742

Merged
merged 8 commits into from Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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: 2 additions & 2 deletions packages/vitest/src/index.ts
Expand Up @@ -84,8 +84,8 @@ declare global {
toContainEqual<E>(item: E): void
toBeTruthy(): void
toBeFalsy(): void
toBeGreaterThan(num: number): void
toBeGreaterThanOrEqual(num: number): void
toBeGreaterThan(num: number | bigint): void
toBeGreaterThanOrEqual(num: number | bigint): void
toBeLessThan(num: number): void
toBeLessThanOrEqual(num: number): void
Copy link
Member

Choose a reason for hiding this comment

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

Great PR! toBeLessThan and toBeLessThanOrEqual should also be number | bigint. Would you like to modify them as part of this PR?

toBeNaN(): void
Expand Down
25 changes: 22 additions & 3 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -2,6 +2,7 @@ import type { EnhancedSpy } from '../jest-mock'
import { isMockFunction } from '../jest-mock'
import { addSerializer } from '../snapshot/port/plugins'
import type { Constructable } from '../../types'
import { assertTypes } from '../../utils'
import type { ChaiPlugin, MatcherState } from './types'
import { arrayBufferEquality, iterableEquality, equals as jestEquals, sparseArrayEquality, subsetEquality, typeEquality } from './jest-utils'
import type { AsymmetricMatcher } from './jest-asymmetric-matchers'
Expand Down Expand Up @@ -160,11 +161,29 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
obj,
)
})
def('toBeGreaterThan', function(expected: number) {
return this.to.greaterThan(expected)
def('toBeGreaterThan', function(expected: number | bigint) {
const actual = this._obj
assertTypes(actual, 'actual', ['number', 'bigint'])
assertTypes(expected, 'expected', ['number', 'bigint'])
return this.assert(
actual > expected,
`expected ${actual} to be above ${expected}`,
`expected ${actual} not to be above ${expected}`,
actual,
expected,
)
})
def('toBeGreaterThanOrEqual', function(expected: number) {
return this.to.greaterThanOrEqual(expected)
const actual = this._obj
assertTypes(actual, 'actual', ['number', 'bigint'])
assertTypes(expected, 'expected', ['number', 'bigint'])
return this.assert(
actual >= expected,
`expected ${actual} to be above ${expected}`,
`expected ${actual} not to be above ${expected}`,
actual,
expected,
)
})
def('toBeLessThan', function(expected: number) {
return this.to.lessThan(expected)
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/utils/base.ts
Expand Up @@ -99,3 +99,9 @@ export function deepMerge<T extends object = object, S extends object = T>(targe
function isMergableObject(item: any): item is Object {
return isPlainObject(item) && !Array.isArray(item)
}

export function assertTypes(value: unknown, name: string, types: string[]): void {
const receivedType = typeof value
const pass = types.includes(receivedType)
if (!pass) throw new TypeError(`${name} value must be ${types.join(' or ')}, received "${receivedType}"`)
}
11 changes: 11 additions & 0 deletions test/core/test/jest-expect.test.ts
Expand Up @@ -22,8 +22,19 @@ describe('jest-expect', () => {
expect([{ text: 'Hello' }]).toContainEqual({ text: 'Hello' })
expect([{ text: 'Bye' }]).not.toContainEqual({ text: 'Hello' })
expect(1).toBeGreaterThan(0)

expect(BigInt(1)).toBeGreaterThan(BigInt(0))
expect(1).toBeGreaterThan(BigInt(0))
expect(BigInt(1)).toBeGreaterThan(0)

expect(1).toBeGreaterThanOrEqual(1)
expect(1).toBeGreaterThanOrEqual(0)

expect(BigInt(1)).toBeGreaterThanOrEqual(BigInt(1))
expect(BigInt(1)).toBeGreaterThanOrEqual(BigInt(0))
expect(BigInt(1)).toBeGreaterThanOrEqual(1)
expect(1).toBeGreaterThanOrEqual(BigInt(1))

expect(0).toBeLessThan(1)
expect(1).toBeLessThanOrEqual(1)
expect(0).toBeLessThanOrEqual(1)
Expand Down
20 changes: 19 additions & 1 deletion test/core/test/utils.spec.ts
@@ -1,7 +1,25 @@
import { describe, expect, test } from 'vitest'
import { deepMerge } from '../../../packages/vitest/src/utils'
import { assertTypes, deepMerge } from '../../../packages/vitest/src/utils'
import { deepMergeSnapshot } from '../../../packages/vitest/src/integrations/snapshot/port/utils'

describe('assertTypes', () => {
test('the type of value should be number', () => {
const value = 5
const value_string = '5'
assertTypes(value, 'value', ['number'])
expect(() => assertTypes(value_string, 'value_string', ['number'])).toThrow()
})

test('the type of value should be number or BigInt', () => {
const value_number = 5
const value_bigint = BigInt(5)
const value_string = '5'
assertTypes(value_number, 'value_number', ['number', 'bigint'])
assertTypes(value_bigint, 'value_bigint', ['number', 'bigint'])
expect(() => assertTypes(value_string, 'value_string', ['number', 'bigint'])).toThrow()
})
})

describe('deepMerge', () => {
test('non plain objects retain their prototype, arrays are not merging, plain objects are merging', () => {
class Test {
Expand Down