diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index 4a5b5cafc230..d2eae82aa8b0 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -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, diff --git a/test/core/test/toHaveProperty.test.ts b/test/core/test/toHaveProperty.test.ts new file mode 100644 index 000000000000..c9353fe47ba9 --- /dev/null +++ b/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') +})