Skip to content

Commit

Permalink
fix: toHaveProperty should judge object own property (#1795)
Browse files Browse the repository at this point in the history
* fix: toHaveProperty should judge object own property

* test: add test
  • Loading branch information
PatrickChen928 committed Aug 5, 2022
1 parent 6ed32dd commit 4a6aeb6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -303,8 +303,12 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {

const actual = this._obj
const [propertyName, expected] = args
const { value, exists } = utils.getPathInfo(actual, propertyName)
const pass = exists && (args.length === 1 || jestEquals(expected, value))
let pass = false
if (Object.prototype.hasOwnProperty.call(actual, propertyName)) { pass = true }
else {
const { value, exists } = utils.getPathInfo(actual, propertyName)
pass = exists && (args.length === 1 || jestEquals(expected, value))
}

return this.assert(
pass,
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/toHaveProperty.test.ts
@@ -0,0 +1,11 @@
import { expect, it } from 'vitest'

const obj = {
'a-b': true,
'a-b-1.0.0': true,
}

it('should have key', () => {
expect(obj).toHaveProperty('a-b')
expect(obj).toHaveProperty('a-b-1.0.0')
})

0 comments on commit 4a6aeb6

Please sign in to comment.