Skip to content

Commit

Permalink
fix(expect): toHaveProperty support array syntax like jest (fix #937) (
Browse files Browse the repository at this point in the history
  • Loading branch information
kalvenschraut committed Mar 15, 2022
1 parent 2b8d1a1 commit e3a7ff3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
11 changes: 7 additions & 4 deletions docs/api/index.md
Expand Up @@ -665,14 +665,15 @@ TODO
import { expect, test } from 'vitest'

const invoice = {
isActive: true,
customer: {
'isActive': true,
'P.O': '12345',
'customer': {
first_name: 'John',
last_name: 'Doe',
location: 'China',
},
total_amount: 5000,
items: [
'total_amount': 5000,
'items': [
{
type: 'apples',
quantity: 10,
Expand All @@ -699,6 +700,8 @@ TODO
expect(invoice).toHaveProperty('items[0].type', 'apples')
expect(invoice).toHaveProperty('items.0.type', 'apples') // dot notation also works

// Wrap your key in an array to avoid the key from being parsed as a deep reference
expect(invoice).toHaveProperty(['P.O'], '12345')
})
```

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/index.ts
Expand Up @@ -93,7 +93,7 @@ declare global {
toBeInstanceOf<E>(expected: E): void
toBeCalledTimes(times: number): void
toHaveLength(length: number): void
toHaveProperty<E>(property: string, value?: E): void
toHaveProperty<E>(property: string | string[], value?: E): void
toBeCloseTo(number: number, numDigits?: number): void
toHaveBeenCalledTimes(times: number): void
toHaveBeenCalledOnce(): void
Expand Down
7 changes: 5 additions & 2 deletions packages/vitest/src/integrations/chai/jest-expect.ts
Expand Up @@ -245,8 +245,11 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return this.have.length(length)
})
// destructuring, because it checks `arguments` inside, and value is passing as `undefined`
def('toHaveProperty', function(...args: [property: string, value?: any]) {
return this.have.deep.nested.property(...args)
def('toHaveProperty', function(...args: [property: string | string[], value?: any]) {
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])
})
def('toBeCloseTo', function(received: number, precision = 2) {
const expected = this._obj
Expand Down
12 changes: 11 additions & 1 deletion test/core/test/jest-expect.test.ts
Expand Up @@ -175,7 +175,15 @@ describe('jest-expect', () => {
expect({}).not.toBe({})

const foo = {}
const complex = { foo: 1, bar: { foo: 'foo', bar: 100, arr: ['first', { zoo: 'monkey' }] } }
const complex = {
'foo': 1,
'foo.bar[0]': 'baz',
'bar': {
foo: 'foo',
bar: 100,
arr: ['first', { zoo: 'monkey' }],
},
}

expect(foo).toBe(foo)
expect(foo).toStrictEqual(foo)
Expand All @@ -193,6 +201,8 @@ describe('jest-expect', () => {
expect(complex).toHaveProperty('bar.arr[1].zoo', 'monkey')
expect(complex).toHaveProperty('bar.arr.0')
expect(complex).toHaveProperty('bar.arr.1.zoo', 'monkey')
expect(complex).toHaveProperty(['bar', 'arr', '1', 'zoo'], 'monkey')
expect(complex).toHaveProperty(['foo.bar[0]'], 'baz')
})

it('assertions', () => {
Expand Down

0 comments on commit e3a7ff3

Please sign in to comment.