-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jest-diff: Add options for colors and symbols #8841
Conversation
Here is the realistic example of codemod from the issue by Ewan Harris const options = {
aAnnotation: 'Original',
aColor: chalk.red,
bAnnotation: 'Modified',
bColor: chalk.green,
});
const difference = diffStringsAligned(a, b, options); First example picture baseline above and optional below 2 more example pictures baseline at left and optional at right const options = {
aSymbol: '<',
bSymbol: '>',
};
const difference = diffLines(a, b, options); // default export const options = {
commonColor: line => line,
commonSymbol: '=',
};
const difference = diffLines(a, b, options); // default export |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! Code / tests also appear solid after scrolling through the first time
@@ -160,6 +162,7 @@ namespace diff { | |||
export type DiffOptions = JestDiffOptions; | |||
} | |||
|
|||
diff.getStringDiff = getStringDiff; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking, correct? Just mentioning because the changelog entry does not say so
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I had hoped to beat the minor because that export is only for first time in 24.9.0 :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's just pretend it was never public api. Nobody will be depending on it yet.
We might also not have a release until 25, at which point it doesn't matter anyways
packages/jest-diff/README.md
Outdated
|
||
Two named exports compare **strings** character-by-character: | ||
|
||
- `diffStringsAligned` returns a string which includes comparison lines. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It took me quite a while (even after reading the whole readme) to understand the difference between those two. I still do not quite understand the naming, which I think was part of what confused me because I associated things like the beginning of lines with "aligned".
Would you say that unified diff vs split diff (like for example in GitHub) is a good analogy for those functions and could be used for naming?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on being a bit confused about what aligned
means. Unified vs split makes more sense to me (that's also the naming bitbucket uses, so I think it's pretty standard)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brilliant analogy!
- so-called aligned is like unified diff
- so-called unaligned is used in Jest to display one-line strings in normal compact report:
Expected: "change from"
Received: "change to"
For multiline strings, a split like GitHub would consist of array of string tuples:
[
[a0, b0],
[a1, b1],
// and so on
]
for corresponding changed chunks. For deleted b
is empty, for inserted a
is empty.
I am curious, can y’all think of a use case in third-party dependents?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I have renamed
diffStringsAligned
anddiffStringsUnified
- I think that I will replace
diffStringsUnaligned
withdiffStringsRaw
that returns an array ofDiff
objects, so that it will be reusable to the max
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So happy this is getting a proper readme!
packages/jest-diff/README.md
Outdated
|
||
To use `diffLines` as the function name, write either of the following: | ||
|
||
- `const diffLines = require('jest-diff');` in a CommonJS module |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll probably go for named exports in jest 25 (we wanna avoid having to use export =
), but this is of course fine for now
Co-Authored-By: Simen Bekkhus <[email protected]>
Sorry about the conflict. It's an autifxable eslint rule, so shouldn't be too bad to resolve at least 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome, thank you so much for tackling it! I think this makes jest-diff
pretty general purpose
@pedrottimark fix conflicts and merge this? I think it's good to go 🙂 |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Fixes #7980
Let dependents configure
jest-diff
to follow other conventions, likediff
orgit diff
Add properties to
DiffOptions
type:aColor
chalk.green
aSymbol
->aIndicator
'-'
bColor
chalk.red
bSymbol
->bIndicator
'+'
commonColor
chalk.dim
commonSymbol
->commonIndicator
' '
omitAnnotationLines
false
EDIT: renamed
Symbol
asIndicator
similar togit diff
options in #8881getStringDiff
and move conditions to callers of the following:diffStringsUnified
/cc @ewanharris does this fit your application?diffStringsRaw
Number.isSafeInteger(contextLines)
to fixTypeError: Cannot read property 'length' of undefined
README.md
file for package :)To help you prioritize your attention, the most complicated code changes:
printDiffs.ts
injest-diff
replace old export that was not re-usable outside of Jest :(index.ts
injest-matcher-utils
include specific conditions on the calling sideprint.ts
injest-snapshot
include specific conditions on the calling sidePart of me wishes this was in 24.9.0 and part of me is thankful that it isn’t
The next step will be to use these options for alternative colors in snapshot diffs
Test plan
diff.test.ts
injest-diff
printDiffOrStringify.test.ts
injest-snapshot
printDiffOrStringified.test.ts
injest-snapshot
Some new tests that I temporarily omitted from
jest-diff
confirmed a problem with substring highlight that I had seen recently while reviewing snapshot tests. I will fix it in a separate PR.See also pictures in the following comment