-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
References tests: Add custom diff printer
This is an ugly workaroud for jestjs/jest#10276.
- Loading branch information
Showing
3 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Invoke @param fn and return the result. If the function is curtailed, invoke @param handler before letting things happen. | ||
* | ||
* Adoption of Smalltalk's `BlockClosure >> #ifCurtailed:`. | ||
*/ | ||
export default function ifCurtailed<T>(fn: () => T, handler: () => void): T { | ||
let complete = false | ||
let result: T | ||
try { | ||
result = fn() | ||
complete = true | ||
} finally { | ||
if (complete != true) { | ||
handler() | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** @XXX Cheap and very imprecise workaround for https://github.com/facebook/jest/issues/10276 */ | ||
export function printDiff(actual: unknown, expected: unknown, ...context: unknown[]) { | ||
console.log([...context, ...createDiff(actual, expected)].join("\n")) | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function* createDiff(actual: any, expected: any, level = 0): Generator<string> { | ||
const indent = ' '.repeat(level) | ||
|
||
if (!(expected.sample)) { | ||
if (actual != expected) { | ||
// Only show diffs | ||
yield `${indent}${actual} != ${expected}` | ||
} | ||
return | ||
} | ||
if (Array.isArray(actual)) { | ||
for (const expectedObject of expected.sample) { | ||
yield `${indent}${expectedObject}:` | ||
if (actual.includes(expectedObject)) { | ||
continue | ||
} | ||
yield `${indent} MISSING: ${expectedObject}` | ||
} | ||
return | ||
} | ||
|
||
for (const [expectedKey, expectedObject] of Object.entries(expected.sample)) { | ||
yield `${indent}${expectedKey}:` | ||
if (expectedKey in actual) { | ||
yield* createDiff(actual[expectedKey], expectedObject, level + 1) | ||
continue | ||
} | ||
yield `${indent} MISSING: ${expectedKey}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters