Skip to content

Commit 4e9e67d

Browse files
committed
Improve error messages for characterization tests
1 parent 31f8aac commit 4e9e67d

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

convert/test/characterization/characterization.test.ts

+38-7
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import path from 'path';
44
import { TaskOutDirs } from 'Task';
55
import { test, expect, describe } from 'vitest';
66
import { mkdir, readdir, readFile, rm, writeFile } from 'fs/promises';
7-
import { compare, CompareFileHandler, Options } from 'dir-compare';
7+
import { compare, CompareFileHandler, Difference, Options } from 'dir-compare';
88
import { fileCompareHandlers } from 'dir-compare';
99
import { isText } from 'istextorbinary';
1010
import { strFromU8 } from 'fflate';
1111
import { PkBookSpec, PkTestLogger } from '../../convertBooks';
12+
import { AssertionError } from 'assert';
1213

1314
interface TestAppPaths {
1415
input: string;
@@ -84,12 +85,42 @@ async function assertDirsEqual(actual: PathLike, expected: PathLike, excludeFilt
8485
compareTimeStamp: false,
8586
excludeFilter
8687
};
87-
await expect
88-
.poll(async () => {
89-
const result = await compare(actual.toString(), expected.toString(), cmpOptions);
90-
return result.diffSet?.filter((diff) => diff.state != 'equal');
91-
}, pollOptions)
92-
.toEqual([]);
88+
await new Promise((resolve) => setTimeout(resolve, 100));
89+
const comparrison = await compare(expected.toString(), actual.toString(), cmpOptions);
90+
for (const diff of comparrison.diffSet ?? []) {
91+
assertFileDiffEqual(diff);
92+
}
93+
// await expect
94+
// .poll(async () => {
95+
// const result = await compare(actual.toString(), expected.toString(), cmpOptions);
96+
// return result.diffSet?.filter((diff) => diff.state != 'equal');
97+
// }, pollOptions)
98+
// .toEqual([]);
99+
}
100+
101+
function assertFileDiffEqual(diff: Difference) {
102+
if (diff.state != 'equal') {
103+
const message = fileDiffError(diff);
104+
throw new AssertionError({ message });
105+
}
106+
}
107+
108+
function fileDiffError(diff: Difference): string {
109+
const baseMessage = fileDiffErrorSummary(diff);
110+
const details = JSON.stringify(diff, null, 2);
111+
const message = baseMessage + '\nDetails:\n' + details;
112+
return message;
113+
}
114+
115+
function fileDiffErrorSummary(diff: Difference): string {
116+
if (diff.state == 'left') {
117+
return `${diff.type1} '${diff.name1}' is missing from output`;
118+
} else if (diff.state == 'right') {
119+
return `Unexpected ${diff.type2} '${diff.name2}' in output`;
120+
} else if (diff.state == 'distinct') {
121+
return `Contents of ${diff.type1} '${diff.name1}' does not match snapshot`;
122+
}
123+
return `Unexpected diff result: ${diff.state}`;
93124
}
94125

95126
function maskDocIds(catalog: any) {

0 commit comments

Comments
 (0)