Skip to content

Commit

Permalink
fix: correctly order arguments in Jest diff util (#1245)
Browse files Browse the repository at this point in the history
Some custom matchers like `@testing-library/jest-dom` use
`this.utils.diff` for the output. In Jest the order of arguments in that
util is "expected", then "received", so we're fixing the order of those
arguments here to match Jest's implementation.
  • Loading branch information
silvenon committed May 6, 2022
1 parent 38dab3f commit 70bc30b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -170,5 +170,5 @@ export interface DiffOptions {
// TODO: do something with options
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function diff(a: any, b: any, options?: DiffOptions) {
return unifiedDiff(stringify(a), stringify(b))
return unifiedDiff(stringify(b), stringify(a))
}
19 changes: 19 additions & 0 deletions test/core/test/jest-matcher-utils.test.ts
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'

describe('jest-matcher-utils', () => {
expect.extend({
toBeJestEqual(received: any, expected: any) {
return {
message: () => this.utils.diff(expected, received),
pass: received === expected,
}
},
})

it('diff', () => {
expect(() => {
// @ts-expect-error "toBeJestEqual" is a custom matcher we just created
expect('a').toBeJestEqual('b')
}).toThrowError(/Expected.*"b".*Received.*"a"/ms)
})
})

0 comments on commit 70bc30b

Please sign in to comment.