Skip to content

Commit

Permalink
References tests: Add custom diff printer
Browse files Browse the repository at this point in the history
This is an ugly workaroud for jestjs/jest#10276.
  • Loading branch information
LinqLover committed May 29, 2021
1 parent 25decb0 commit d73d865
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/utils/if-curtailed.ts
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
}
36 changes: 36 additions & 0 deletions test/_utils/printDiff.ts
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}`
}
}
20 changes: 12 additions & 8 deletions test/references.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import asyncIteratorToArray from 'it-all'
import _ from 'lodash'

import { ReferenceSearcher } from '../src/references'
import ifCurtailed from '../src/utils/if-curtailed'
import { printDiff } from './_utils/printDiff'

import expectedHeuristicReferences from './references.test/expectedReferences-heuristic.json'

Expand Down Expand Up @@ -42,13 +44,15 @@ describe('ReferenceSearcher', () => {
.value())
.value()

expect(aggregatedReferences).toEqual(
expect.objectContaining(
_.mapValues(expectedReferences, dependentReferences => expect.objectContaining(
_.mapValues(dependentReferences, memberReferences => expect.objectContaining(
_.mapValues(memberReferences, lineNumbers => expect.arrayContaining(lineNumbers))
))
))))
// TODO: Test false positive rate?
const aggregatedExpectedReferences = expect.objectContaining(
_.mapValues(expectedReferences, dependentReferences => expect.objectContaining(
_.mapValues(dependentReferences, memberReferences => expect.objectContaining(
_.mapValues(memberReferences, lineNumbers => expect.arrayContaining(lineNumbers))
))
)))
ifCurtailed(
() => expect(aggregatedReferences).toEqual(aggregatedExpectedReferences),
() => printDiff(aggregatedReferences, aggregatedExpectedReferences, packageReferenceSearcher, packageName))
// TODO: Test false positive rate
})
})

0 comments on commit d73d865

Please sign in to comment.