diff --git a/packages/vitest/src/integrations/chai/jest-expect.ts b/packages/vitest/src/integrations/chai/jest-expect.ts index 5b37cd4c4e9f..2498bcde1435 100644 --- a/packages/vitest/src/integrations/chai/jest-expect.ts +++ b/packages/vitest/src/integrations/chai/jest-expect.ts @@ -313,18 +313,21 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => { const actual = this._obj const [propertyName, expected] = args - 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)) + const getValue = () => { + const hasOwn = Object.prototype.hasOwnProperty.call(actual, propertyName) + if (hasOwn) + return { value: actual[propertyName], exists: true } + return utils.getPathInfo(actual, propertyName) } + const { value, exists } = getValue() + const pass = exists && (args.length === 1 || jestEquals(expected, value)) + + const valueString = args.length === 1 ? '' : ` with value ${utils.objDisplay(expected)}` return this.assert( pass, - 'expected #{this} to have property #{exp}', - 'expected #{this} to not have property #{exp}', - expected, + `expected #{this} to have property "${propertyName}"${valueString}`, + `expected #{this} to not have property "${propertyName}"${valueString}`, actual, ) }) diff --git a/test/core/test/jest-expect.test.ts b/test/core/test/jest-expect.test.ts index 97babe67698c..30dbdc31ada6 100644 --- a/test/core/test/jest-expect.test.ts +++ b/test/core/test/jest-expect.test.ts @@ -178,6 +178,8 @@ describe('jest-expect', () => { const complex = { 'foo': 1, 'foo.bar[0]': 'baz', + 'a-b': true, + 'a-b-1.0.0': true, 'bar': { foo: 'foo', bar: 100, @@ -194,6 +196,8 @@ describe('jest-expect', () => { expect(complex).toMatchObject({ bar: { bar: 100 } }) expect(complex).toMatchObject({ foo: expect.any(Number) }) + expect(complex).toHaveProperty('a-b') + expect(complex).toHaveProperty('a-b-1.0.0') expect(complex).toHaveProperty('foo') expect(complex).toHaveProperty('foo', 1) expect(complex).toHaveProperty('bar.foo', 'foo') @@ -208,6 +212,14 @@ describe('jest-expect', () => { expect(complex).toHaveProperty('bar', expect.any(Object)) expect(complex).toHaveProperty('bar.arr', expect.any(Array)) expect(complex).toHaveProperty('bar.arr.0', expect.anything()) + + expect(() => { + expect(complex).toHaveProperty('some-unknown-property') + }).toThrowError() + + expect(() => { + expect(complex).toHaveProperty('a-b', false) + }).toThrowErrorMatchingInlineSnapshot('"expected { foo: 1, \'foo.bar[0]\': \'baz\', …(3) } to have property \\"a-b\\" with value false"') }) it('assertions', () => { diff --git a/test/core/test/toHaveProperty.test.ts b/test/core/test/toHaveProperty.test.ts deleted file mode 100644 index c9353fe47ba9..000000000000 --- a/test/core/test/toHaveProperty.test.ts +++ /dev/null @@ -1,11 +0,0 @@ -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') -})