From 66251263925529065cd3c9060a236259a86cbf2f Mon Sep 17 00:00:00 2001 From: cyly <786156072@qq.com> Date: Fri, 5 Aug 2022 11:51:14 +0800 Subject: [PATCH 1/2] fix: toHaveProperty should judge object own property --- packages/vitest/src/integrations/chai/jest-expect.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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, From c8a457753ed6d463965cd8b9ffa25d861cd0a76a Mon Sep 17 00:00:00 2001 From: cyly <786156072@qq.com> Date: Fri, 5 Aug 2022 12:05:34 +0800 Subject: [PATCH 2/2] test: add test --- test/core/test/toHaveProperty.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/core/test/toHaveProperty.test.ts 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') +})