(actual: TestFile[], expected: TestFile[])
| 613 | * Compare actual outputs with expected outputs |
| 614 | */ |
| 615 | export function compareOutputs(actual: TestFile[], expected: TestFile[]): void { |
| 616 | const actualMap = new Map(actual.map((f) => [f.path, f.content])); |
| 617 | const expectedMap = new Map(expected.map((f) => [f.path, f.content])); |
| 618 | |
| 619 | for (const [filePath, expectedContent] of expectedMap) { |
| 620 | const actualContent = actualMap.get(filePath); |
| 621 | if (actualContent === undefined) { |
| 622 | throw new Error(`Missing file: ${filePath}`); |
| 623 | } |
| 624 | const normalizedActual = normalizeWhitespace(actualContent); |
| 625 | const normalizedExpected = normalizeWhitespace(expectedContent); |
| 626 | |
| 627 | if (!checkMatch(actualContent, expectedContent)) { |
| 628 | const diff = createTwoFilesPatch( |
| 629 | `${filePath} (expected)`, |
| 630 | `${filePath} (actual)`, |
| 631 | normalizedExpected, |
| 632 | normalizedActual, |
| 633 | '', |
| 634 | '', |
| 635 | {context: 3}, |
| 636 | ); |
| 637 | throw new Error(`Content mismatch for ${filePath}:\n\n${diff}`); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | for (const filePath of actualMap.keys()) { |
| 642 | if (!expectedMap.has(filePath)) { |
| 643 | throw new Error(`Unexpected file: ${filePath}`); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | function normalizeWhitespace(s: string): string { |
| 649 | let normalized = s |
no test coverage detected