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: toHaveProperty with expect.any (fix: #1675) #1682

Merged
merged 3 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import c from 'picocolors'
import { AssertionError } from 'chai'
import { AssertionError, util as chaiUtil } from 'chai'
skarab42 marked this conversation as resolved.
Show resolved Hide resolved
import type { EnhancedSpy } from '../spy'
import { isMockFunction } from '../spy'
import { addSerializer } from '../snapshot/port/plugins'
Expand Down Expand Up @@ -301,7 +301,18 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
if (Array.isArray(args[0]))
args[0] = args[0].map(key => key.replace(/([.[\]])/g, '\\$1')).join('.')

return this.have.deep.nested.property(...args as [property: string, value?: any])
const actual = this._obj
const [propertyName, expected] = args
const { value, exists } = chaiUtil.getPathInfo(actual, propertyName)
const pass = exists && (args.length === 1 || jestEquals(expected, value))

return this.assert(
pass,
'expected #{this} to have property #{exp}',
'expected #{this} to not have property #{exp}',
expected,
actual,
)
})
def('toBeCloseTo', function (received: number, precision = 2) {
const expected = this._obj
Expand Down
5 changes: 5 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ describe('jest-expect', () => {
expect(complex).toHaveProperty('bar.arr.1.zoo', 'monkey')
expect(complex).toHaveProperty(['bar', 'arr', '1', 'zoo'], 'monkey')
expect(complex).toHaveProperty(['foo.bar[0]'], 'baz')

expect(complex).toHaveProperty('foo', expect.any(Number))
expect(complex).toHaveProperty('bar', expect.any(Object))
expect(complex).toHaveProperty('bar.arr', expect.any(Array))
expect(complex).toHaveProperty('bar.arr.0', expect.anything())
})

it('assertions', () => {
Expand Down