Skip to content

Commit

Permalink
fix: toHaveProperty with expect.any (fix: vitest-dev#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jul 19, 2022
1 parent c3cc6d8 commit ed9a633
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/vitest/src/integrations/chai/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { unifiedDiff } from '../../node/diff'
import type { ChaiPlugin, MatcherState } from '../../types/chai'
import { arrayBufferEquality, generateToBeMessage, iterableEquality, equals as jestEquals, sparseArrayEquality, subsetEquality, typeEquality } from './jest-utils'
import type { AsymmetricMatcher } from './jest-asymmetric-matchers'
import { Any, Anything } from './jest-asymmetric-matchers'
import { stringify } from './jest-matcher-utils'
import { MATCHERS_OBJECT } from './constants'

Expand Down Expand Up @@ -301,7 +302,15 @@ 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 [propertyName, propertyValue] = args

if (propertyValue instanceof Anything)
return this.have.deep.nested.property(propertyName)

if (propertyValue instanceof Any)
return this.have.deep.nested.property(propertyName).that.is.a(propertyValue.getExpectedType())

return this.have.deep.nested.property(...args as [property: string, value: any])
})
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

0 comments on commit ed9a633

Please sign in to comment.