Skip to content

Commit

Permalink
fix: revert concordance diff, use jest's diff output (#3582)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jun 16, 2023
1 parent d9e1419 commit 9c7ea38
Show file tree
Hide file tree
Showing 28 changed files with 1,933 additions and 310 deletions.
25 changes: 19 additions & 6 deletions packages/expect/src/jest-expect.ts
Expand Up @@ -186,6 +186,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
'expected #{this} to be truthy',
'expected #{this} to not be truthy',
obj,
false,
)
})
def('toBeFalsy', function () {
Expand All @@ -195,6 +196,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
'expected #{this} to be falsy',
'expected #{this} to not be falsy',
obj,
false,
)
})
def('toBeGreaterThan', function (expected: number | bigint) {
Expand All @@ -207,6 +209,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected ${actual} to be not greater than ${expected}`,
actual,
expected,
false,
)
})
def('toBeGreaterThanOrEqual', function (expected: number | bigint) {
Expand All @@ -219,6 +222,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected ${actual} to be not greater than or equal to ${expected}`,
actual,
expected,
false,
)
})
def('toBeLessThan', function (expected: number | bigint) {
Expand All @@ -231,6 +235,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected ${actual} to be not less than ${expected}`,
actual,
expected,
false,
)
})
def('toBeLessThanOrEqual', function (expected: number | bigint) {
Expand All @@ -243,6 +248,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected ${actual} to be not less than or equal to ${expected}`,
actual,
expected,
false,
)
})
def('toBeNaN', function () {
Expand Down Expand Up @@ -328,6 +334,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected #{this} to not be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
received,
expected,
false,
)
})

Expand Down Expand Up @@ -356,10 +363,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
}
const formatCalls = (spy: EnhancedSpy, msg: string, actualCall?: any) => {
if (spy.mock.calls) {
msg += c().gray(`\n\nReceived: \n${spy.mock.calls.map((callArg, i) => {
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call:\n\n`)
msg += c().gray(`\n\nReceived: \n\n${spy.mock.calls.map((callArg, i) => {
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call:\n\n`)
if (actualCall)
methodCall += diff(actualCall, callArg, { showLegend: false })
methodCall += diff(actualCall, callArg, { omitAnnotationLines: true })
else
methodCall += stringify(callArg).split('\n').map(line => ` ${line}`).join('\n')
Expand All @@ -371,10 +378,10 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
return msg
}
const formatReturns = (spy: EnhancedSpy, msg: string, actualReturn?: any) => {
msg += c().gray(`\n\nReceived: \n${spy.mock.results.map((callReturn, i) => {
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:\n\n`)
msg += c().gray(`\n\nReceived: \n\n${spy.mock.results.map((callReturn, i) => {
let methodCall = c().bold(` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:\n\n`)
if (actualReturn)
methodCall += diff(actualReturn, callReturn.value, { showLegend: false })
methodCall += diff(actualReturn, callReturn.value, { omitAnnotationLines: true })
else
methodCall += stringify(callReturn).split('\n').map(line => ` ${line}`).join('\n')
Expand All @@ -394,6 +401,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected "${spyName}" to not be called #{exp} times`,
number,
callCount,
false,
)
})
def('toHaveBeenCalledOnce', function () {
Expand All @@ -406,6 +414,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected "${spyName}" to not be called once`,
1,
callCount,
false,
)
})
def(['toHaveBeenCalled', 'toBeCalled'], function () {
Expand Down Expand Up @@ -525,6 +534,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected error not to be instance of ${name}`,
expected,
thrown,
false,
)
}

Expand All @@ -546,6 +556,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
'expected error not to match asymmetric matcher',
matcher.toString(),
thrown,
false,
)
}

Expand All @@ -561,6 +572,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected "${spyName}" to not be successfully called`,
calledAndNotThrew,
!calledAndNotThrew,
false,
)
})
def(['toHaveReturnedTimes', 'toReturnTimes'], function (times: number) {
Expand All @@ -573,6 +585,7 @@ export const JestChaiExpect: ChaiPlugin = (chai, utils) => {
`expected "${spyName}" to not be successfully called ${times} times`,
`expected number of returns: ${times}`,
`received number of returns: ${successfulReturns}`,
false,
)
})
def(['toHaveReturnedWith', 'toReturnWith'], function (value: any) {
Expand Down
11 changes: 2 additions & 9 deletions packages/expect/src/jest-matcher-utils.ts
@@ -1,7 +1,7 @@
import { getColors, stringify } from '@vitest/utils'
import { unifiedDiff } from '@vitest/utils/diff'
import type { DiffOptions, MatcherHintOptions } from './types'
import type { MatcherHintOptions } from './types'

export { diff } from '@vitest/utils/diff'
export { stringify }

export function getMatcherUtils() {
Expand Down Expand Up @@ -101,10 +101,3 @@ export function getMatcherUtils() {
printExpected,
}
}

// TODO: do something with options
export function diff(a: any, b: any, options?: DiffOptions) {
return unifiedDiff(b, a, {
showLegend: options?.showLegend,
})
}
25 changes: 2 additions & 23 deletions packages/expect/src/types.ts
Expand Up @@ -17,6 +17,8 @@ export type ChaiPlugin = FirstFunctionArgument<typeof chaiUse>

export type Tester = (a: any, b: any) => boolean | undefined

export type { DiffOptions } from '@vitest/utils/diff'

export interface MatcherHintOptions {
comment?: string
expectedColor?: Formatter
Expand All @@ -28,29 +30,6 @@ export interface MatcherHintOptions {
secondArgumentColor?: Formatter
}

export interface DiffOptions {
aAnnotation?: string
aColor?: Formatter
aIndicator?: string
bAnnotation?: string
bColor?: Formatter
bIndicator?: string
changeColor?: Formatter
changeLineTrailingSpaceColor?: Formatter
commonColor?: Formatter
commonIndicator?: string
commonLineTrailingSpaceColor?: Formatter
contextLines?: number
emptyFirstOrLastLinePlaceholder?: string
expand?: boolean
includeChangeCounts?: boolean
omitAnnotationLines?: boolean
patchColor?: Formatter
// pretty-format type
compareKeys?: any
showLegend?: boolean
}

export interface MatcherState {
assertionCalls: number
currentTestName?: string
Expand Down
3 changes: 1 addition & 2 deletions packages/ui/client/components/views/ViewReportError.vue
@@ -1,6 +1,5 @@
<script setup lang="ts">
import type { ErrorWithDiff } from '#types'
import { unifiedDiff } from '~/composables/diff'
import { openInEditor, shouldOpenInEditor } from '~/composables/error'
const props = defineProps<{
Expand All @@ -20,7 +19,7 @@ const isDiffShowable = computed(() => {
})
function diff() {
return unifiedDiff(props.error.actual, props.error.expected)
return props.error.diff
}
</script>

Expand Down
1 change: 0 additions & 1 deletion packages/ui/client/composables/diff.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/utils/package.json
Expand Up @@ -47,7 +47,7 @@
"prepublishOnly": "pnpm build"
},
"dependencies": {
"concordance": "^5.0.4",
"diff-sequences": "^29.4.3",
"loupe": "^2.3.6",
"pretty-format": "^27.5.1"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/rollup.config.js
Expand Up @@ -9,7 +9,7 @@ import pkg from './package.json' assert { type: 'json' }
const entries = {
index: 'src/index.ts',
helpers: 'src/helpers.ts',
diff: 'src/diff.ts',
diff: 'src/diff/index.ts',
error: 'src/error.ts',
types: 'src/types.ts',
}
Expand Down
98 changes: 0 additions & 98 deletions packages/utils/src/descriptors.ts

This file was deleted.

51 changes: 0 additions & 51 deletions packages/utils/src/diff.ts

This file was deleted.

0 comments on commit 9c7ea38

Please sign in to comment.