-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmerge-diffs.unit.test.ts
More file actions
80 lines (71 loc) · 2.63 KB
/
merge-diffs.unit.test.ts
File metadata and controls
80 lines (71 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { vol } from 'memfs';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import type { PersistConfig } from '@code-pushup/models';
import {
reportsDiffAddedPluginMock,
reportsDiffAltMock,
reportsDiffMock,
reportsDiffUnchangedMock,
} from '@code-pushup/test-fixtures';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import { fileExists, logger } from '@code-pushup/utils';
import { mergeDiffs } from './merge-diffs.js';
describe('mergeDiffs', () => {
const diffPaths = {
'console/report-diff.json': JSON.stringify(reportsDiffMock()),
'admin/report-diff.json': JSON.stringify(reportsDiffAltMock()),
'website/report-diff.json': JSON.stringify(reportsDiffUnchangedMock()),
'docs/report-diff.json': JSON.stringify(reportsDiffAddedPluginMock()),
};
const files = Object.keys(diffPaths);
const persistConfig: Required<PersistConfig> = {
outputDir: MEMFS_VOLUME,
filename: 'report',
format: ['json', 'md'],
skipReports: false,
};
beforeEach(() => {
vol.fromJSON(diffPaths, MEMFS_VOLUME);
});
it('should create Markdown file', async () => {
const outputPath = await mergeDiffs(files, persistConfig);
expect(outputPath).toBe(path.join(MEMFS_VOLUME, 'report-diff.md'));
await expect(fileExists(outputPath)).resolves.toBeTrue();
await expect(readFile(outputPath, 'utf8')).resolves.toContain(
'# Code PushUp',
);
});
it('should use parent folder as project name in Markdown output if label missing in diff', async () => {
const outputPath = await mergeDiffs(files, persistConfig);
const markdown = await readFile(outputPath, 'utf8');
// `website` is unchanged, therefore not mentioned by name
expect(markdown).toContain('## 💼 Project `console`');
expect(markdown).toContain('## 💼 Project `admin`');
expect(markdown).toContain('## 💼 Project `docs`');
});
it('should log warnings if file parsing failed', async () => {
vol.fromJSON(
{ ...diffPaths, 'invalid-report-diff.json': '{}' },
MEMFS_VOLUME,
);
await expect(
mergeDiffs(
[...files, 'missing-report-diff.json', 'invalid-report-diff.json'],
persistConfig,
),
).resolves.toBe(path.join(MEMFS_VOLUME, 'report-diff.md'));
expect(logger.warn).toHaveBeenNthCalledWith(
1,
expect.stringContaining(
'Skipped invalid report diff - Failed to read JSON file missing-report-diff.json',
),
);
expect(logger.warn).toHaveBeenNthCalledWith(
2,
expect.stringContaining(
'Skipped invalid report diff - Invalid reports diff in invalid-report-diff.json',
),
);
});
});