Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(expect): fix toHaveProperty assertion error diff #4734

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/expect/src/jest-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
if (Array.isArray(args[0]))
args[0] = args[0].map(key => String(key).replace(/([.[\]])/g, '\\$1')).join('.')

const actual = this._obj
const actual = this._obj as any
Copy link
Contributor Author

@hi-ogawa hi-ogawa Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why but typescript was complaining this line on my IDE, so I added explicit any:

vscode error

image

pnpm build (rollup dts?) is fine without this, so probably it's my local setup issue. If you see this unnecessary, then I can revert this, so please let me know.

const [propertyName, expected] = args
const getValue = () => {
const hasOwn = Object.prototype.hasOwnProperty.call(actual, propertyName)
Expand All @@ -347,7 +347,8 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
pass,
`expected #{this} to have property "${propertyName}"${valueString}`,
`expected #{this} to not have property "${propertyName}"${valueString}`,
actual,
expected,
exists ? value : undefined,
)
})
def('toBeCloseTo', function (received: number, precision = 2) {
Expand Down
92 changes: 92 additions & 0 deletions test/core/test/jest-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,4 +903,96 @@ it('correctly prints diff with asymmetric matchers', () => {
}
})

it('toHaveProperty error diff', () => {
setupColors(getDefaultColors())

// make it easy for dev who trims trailing whitespace on IDE
function trim(s: string): string {
return s.replaceAll(/ *$/gm, '')
}

function getError(f: () => unknown) {
try {
f()
return expect.unreachable()
}
catch (error) {
const processed = processError(error)
return [processed.message, trim(processed.diff)]
}
}

// non match value
expect(getError(() => expect({ name: 'foo' }).toHaveProperty('name', 'bar'))).toMatchInlineSnapshot(`
[
"expected { name: 'foo' } to have property "name" with value 'bar'",
"- Expected
+ Received

- bar
+ foo",
]
`)

// non match key
expect(getError(() => expect({ noName: 'foo' }).toHaveProperty('name', 'bar'))).toMatchInlineSnapshot(`
[
"expected { noName: 'foo' } to have property "name" with value 'bar'",
"- Expected:
"bar"

+ Received:
undefined",
]
`)

// non match value (with asymmetric matcher)
expect(getError(() => expect({ name: 'foo' }).toHaveProperty('name', expect.any(Number)))).toMatchInlineSnapshot(`
[
"expected { name: 'foo' } to have property "name" with value Any{ …(3) }",
"- Expected:
Any<Number>

+ Received:
"foo"",
]
`)

// non match key (with asymmetric matcher)
expect(getError(() => expect({ noName: 'foo' }).toHaveProperty('name', expect.any(Number)))).toMatchInlineSnapshot(`
[
"expected { noName: 'foo' } to have property "name" with value Any{ …(3) }",
"- Expected:
Any<Number>

+ Received:
undefined",
]
`)

// non match value (deep key)
expect(getError(() => expect({ parent: { name: 'foo' } }).toHaveProperty('parent.name', 'bar'))).toMatchInlineSnapshot(`
[
"expected { parent: { name: 'foo' } } to have property "parent.name" with value 'bar'",
"- Expected
+ Received

- bar
+ foo",
]
`)

// non match key (deep key)
expect(getError(() => expect({ parent: { noName: 'foo' } }).toHaveProperty('parent.name', 'bar'))).toMatchInlineSnapshot(`
[
"expected { parent: { noName: 'foo' } } to have property "parent.name" with value 'bar'",
"- Expected:
"bar"

+ Received:
undefined",
]
`)
})

it('timeout', () => new Promise(resolve => setTimeout(resolve, 500)))