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 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: 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
56 changes: 53 additions & 3 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -160,11 +160,61 @@ 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
if (typeof actual !== 'number' && typeof actual !== 'bigint') {
return this.assert(
Copy link
Member

Choose a reason for hiding this comment

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

Not sure about this use of assert

It's not actually an AssertionError, since no assertion was actually performed bc the input was wrong. Maybe we can

  1. Throw TypeError
  2. Move it to some helper function to reuse in other assertions
  3. Rewrite to something like:
assertTypes(expected, 'expected', ['number', 'bigint'])
assertTypes(actual, 'received', ['number', 'bigint']) // throws Recieved values must be type or type, received "type"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Display the message like this when a type error occurred.

スクリーンショット 2022-02-13 17 56 08

Copy link
Member

Choose a reason for hiding this comment

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

Good job 👍

false,
`expected "${actual}" to be a number or bigint`,
`expected "${actual}" to be a number or bigint`,
actual,
expected,
)
}
if (typeof expected !== 'number' && typeof expected !== 'bigint') {
return this.assert(
false,
`expected "${expected}" to be a number or bigint`,
`expected "${expected}" to be a number or bigint`,
actual,
expected,
)
}
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
if (typeof actual !== 'number' && typeof actual !== 'bigint') {
return this.assert(
false,
`expected "${actual}" to be a number or bigint`,
`expected "${actual}" to be a number or bigint`,
actual,
expected,
)
}
if (typeof expected !== 'number' && typeof expected !== 'bigint') {
return this.assert(
false,
`expected "${expected}" to be a number or bigint`,
`expected "${expected}" to be a number or bigint`,
actual,
expected,
)
}
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
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