Skip to content

Commit fa8cad5

Browse files
committed
test(cli): update unit tests to use custom matchers
1 parent 1462821 commit fa8cad5

File tree

8 files changed

+43
-42
lines changed

8 files changed

+43
-42
lines changed

packages/cli/src/lib/compare/compare-command.unit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
DEFAULT_PERSIST_FORMAT,
66
DEFAULT_PERSIST_OUTPUT_DIR,
77
} from '@code-pushup/models';
8-
import { getLogMessages } from '@code-pushup/test-utils';
98
import { ui } from '@code-pushup/utils';
109
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants.js';
1110
import { yargsCli } from '../yargs-cli.js';
@@ -75,7 +74,8 @@ describe('compare-command', () => {
7574
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsCompareCommandObject()] },
7675
).parseAsync();
7776

78-
expect(getLogMessages(ui().logger).at(-1)).toContain(
77+
expect(ui()).toHaveLoggedLevel('info');
78+
expect(ui()).toHaveLoggedMessage(
7979
`Reports diff written to ${bold(
8080
'.code-pushup/report-diff.json',
8181
)} and ${bold('.code-pushup/report-diff.md')}`,

packages/cli/src/lib/implementation/filter.middleware.unit.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,6 @@ describe('filterMiddleware', () => {
291291
);
292292

293293
it('should trigger verbose logging when skipPlugins or onlyPlugins removes categories', () => {
294-
const loggerSpy = vi.spyOn(ui().logger, 'info');
295-
296294
filterMiddleware({
297295
onlyPlugins: ['p1'],
298296
skipPlugins: ['p2'],
@@ -316,8 +314,15 @@ describe('filterMiddleware', () => {
316314
verbose: true,
317315
});
318316

319-
expect(loggerSpy).toHaveBeenCalledWith(
320-
expect.stringContaining('removed the following categories'),
317+
expect(ui()).toHaveLoggedNthLevel(1, 'info');
318+
expect(ui()).toHaveLoggedNthLevel(2, 'info');
319+
expect(ui()).toHaveLoggedNthMessage(
320+
1,
321+
'The --skipPlugins argument removed the following categories: c1, c2.',
322+
);
323+
expect(ui()).toHaveLoggedNthMessage(
324+
2,
325+
'The --onlyPlugins argument removed the following categories: c1, c2.',
321326
);
322327
});
323328

packages/cli/src/lib/implementation/global.utils.unit.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ describe('logErrorBeforeThrow', () => {
5555
});
5656

5757
it('should log a custom error when OptionValidationError is thrown', async () => {
58-
const loggerSpy = vi.spyOn(ui().logger, 'error').mockImplementation(() => {
59-
/* empty */
60-
});
61-
6258
const errorFn = vi
6359
.fn()
6460
.mockRejectedValue(new OptionValidationError('Option validation failed'));
@@ -68,8 +64,8 @@ describe('logErrorBeforeThrow', () => {
6864
} catch {
6965
/* suppress */
7066
}
71-
72-
expect(loggerSpy).toHaveBeenCalledWith('Option validation failed');
67+
expect(ui()).toHaveLoggedLevel('error');
68+
expect(ui()).toHaveLoggedMessage('Option validation failed');
7369
});
7470

7571
it('should rethrow errors other than OptionValidationError', async () => {

packages/cli/src/lib/implementation/validate-filter-options.utils.unit.test.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect } from 'vitest';
22
import type { CategoryConfig, PluginConfig } from '@code-pushup/models';
3-
import { getLogMessages } from '@code-pushup/test-utils';
43
import { ui } from '@code-pushup/utils';
54
import type { FilterOptionType, Filterables } from './filter.model.js';
65
import {
@@ -19,22 +18,22 @@ describe('validateFilterOption', () => {
1918
[
2019
'onlyPlugins',
2120
['p1', 'p3', 'p4'],
22-
'The --onlyPlugins argument references plugins that do not exist: p3, p4.',
21+
'The --onlyPlugins argument references plugins that do not exist: p3, p4. The only valid plugin is p1.',
2322
],
2423
[
2524
'onlyPlugins',
2625
['p1', 'p3'],
27-
'The --onlyPlugins argument references a plugin that does not exist: p3.',
26+
'The --onlyPlugins argument references a plugin that does not exist: p3. The only valid plugin is p1.',
2827
],
2928
[
3029
'onlyCategories',
3130
['c1', 'c3', 'c4'],
32-
'The --onlyCategories argument references categories that do not exist: c3, c4.',
31+
'The --onlyCategories argument references categories that do not exist: c3, c4. The only valid category is c1.',
3332
],
3433
[
3534
'onlyCategories',
3635
['c1', 'c3'],
37-
'The --onlyCategories argument references a category that does not exist: c3.',
36+
'The --onlyCategories argument references a category that does not exist: c3. The only valid category is c1.',
3837
],
3938
])(
4039
'should log a warning if the only argument %s references nonexistent slugs %o along with valid ones',
@@ -51,31 +50,30 @@ describe('validateFilterOption', () => {
5150
},
5251
{ itemsToFilter, skippedItems: [], verbose: false },
5352
);
54-
const logs = getLogMessages(ui().logger);
55-
expect(logs[0]).toContain(expected);
53+
expect(ui()).toHaveLoggedMessage(expected);
5654
},
5755
);
5856

5957
it.each([
6058
[
6159
'skipPlugins',
6260
['p3', 'p4'],
63-
'The --skipPlugins argument references plugins that do not exist: p3, p4.',
61+
'The --skipPlugins argument references plugins that do not exist: p3, p4. The only valid plugin is p1.',
6462
],
6563
[
6664
'skipPlugins',
6765
['p3'],
68-
'The --skipPlugins argument references a plugin that does not exist: p3.',
66+
'The --skipPlugins argument references a plugin that does not exist: p3. The only valid plugin is p1.',
6967
],
7068
[
7169
'skipCategories',
7270
['c3', 'c4'],
73-
'The --skipCategories argument references categories that do not exist: c3, c4.',
71+
'The --skipCategories argument references categories that do not exist: c3, c4. The only valid category is c1.',
7472
],
7573
[
7674
'skipCategories',
7775
['c3'],
78-
'The --skipCategories argument references a category that does not exist: c3.',
76+
'The --skipCategories argument references a category that does not exist: c3. The only valid category is c1.',
7977
],
8078
])(
8179
'should log a warning if the skip argument %s references nonexistent slugs %o',
@@ -95,8 +93,7 @@ describe('validateFilterOption', () => {
9593
},
9694
{ itemsToFilter, skippedItems: [], verbose: false },
9795
);
98-
const logs = getLogMessages(ui().logger);
99-
expect(logs[0]).toContain(expected);
96+
expect(ui()).toHaveLoggedMessage(expected);
10097
},
10198
);
10299

@@ -111,7 +108,7 @@ describe('validateFilterOption', () => {
111108
},
112109
{ itemsToFilter: ['p1'], skippedItems: [], verbose: false },
113110
);
114-
expect(getLogMessages(ui().logger)).toHaveLength(0);
111+
expect(ui()).not.toHaveLogged();
115112
});
116113

117114
it('should log a category ignored as a result of plugin filtering', () => {
@@ -130,9 +127,9 @@ describe('validateFilterOption', () => {
130127
},
131128
{ itemsToFilter: ['p1'], skippedItems: [], verbose: true },
132129
);
133-
expect(getLogMessages(ui().logger)).toHaveLength(1);
134-
expect(getLogMessages(ui().logger)[0]).toContain(
135-
'The --onlyPlugins argument removed the following categories: c1, c3',
130+
expect(ui()).toHaveLoggedTimes(1);
131+
expect(ui()).toHaveLoggedMessage(
132+
'The --onlyPlugins argument removed the following categories: c1, c3.',
136133
);
137134
});
138135

@@ -221,10 +218,14 @@ describe('validateFilterOption', () => {
221218
{ plugins, categories },
222219
{ itemsToFilter: ['p1'], skippedItems: ['p1'], verbose: true },
223220
);
224-
const logs = getLogMessages(ui().logger);
225-
expect(logs[0]).toContain(
221+
expect(ui()).toHaveLoggedNthMessage(
222+
1,
226223
'The --skipPlugins argument references a skipped plugin: p1.',
227224
);
225+
expect(ui()).toHaveLoggedNthMessage(
226+
2,
227+
'The --skipPlugins argument removed the following categories: c1.',
228+
);
228229
});
229230
});
230231

@@ -446,7 +447,6 @@ describe('validateSkippedCategories', () => {
446447
] as NonNullable<Filterables['categories']>;
447448

448449
it('should log info when categories are removed', () => {
449-
const loggerSpy = vi.spyOn(ui().logger, 'info');
450450
validateSkippedCategories(
451451
categories,
452452
[
@@ -457,7 +457,8 @@ describe('validateSkippedCategories', () => {
457457
] as NonNullable<Filterables['categories']>,
458458
true,
459459
);
460-
expect(loggerSpy).toHaveBeenCalledWith(
460+
expect(ui()).toHaveLoggedLevel('info');
461+
expect(ui()).toHaveLoggedMessage(
461462
'Category c1 was removed because all its refs were skipped. Affected refs: g1 (group)',
462463
);
463464
});

packages/cli/src/lib/merge-diffs/merge-diffs-command.unit.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
DEFAULT_PERSIST_FORMAT,
66
DEFAULT_PERSIST_OUTPUT_DIR,
77
} from '@code-pushup/models';
8-
import { getLogMessages } from '@code-pushup/test-utils';
98
import { ui } from '@code-pushup/utils';
109
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants.js';
1110
import { yargsCli } from '../yargs-cli.js';
@@ -65,7 +64,7 @@ describe('merge-diffs-command', () => {
6564
},
6665
).parseAsync();
6766

68-
expect(getLogMessages(ui().logger).at(-1)).toContain(
67+
expect(ui()).toHaveLoggedMessage(
6968
`Reports diff written to ${bold('.code-pushup/report-diff.md')}`,
7069
);
7170
});

packages/cli/src/lib/print-config/print-config-command.unit.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, expect, vi } from 'vitest';
2-
import { getLogMessages } from '@code-pushup/test-utils';
32
import { ui } from '@code-pushup/utils';
43
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants.js';
54
import { yargsCli } from '../yargs-cli.js';
@@ -27,11 +26,10 @@ describe('print-config-command', () => {
2726
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsConfigCommandObject()] },
2827
).parseAsync();
2928

30-
const log = getLogMessages(ui().logger)[0];
31-
expect(log).not.toContain('"$0":');
32-
expect(log).not.toContain('"_":');
29+
expect(ui()).not.toHaveLoggedMessageContaining('"$0":');
30+
expect(ui()).not.toHaveLoggedMessageContaining('"_":');
3331

34-
expect(log).toContain('"outputDir": "destinationDir"');
35-
expect(log).not.toContain('"output-dir":');
32+
expect(ui()).toHaveLoggedMessageContaining('"outputDir": "destinationDir"');
33+
expect(ui()).not.toHaveLoggedMessageContaining('"output-dir":');
3634
});
3735
});

packages/cli/tsconfig.test.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"src/**/*.test.tsx",
1313
"src/**/*.test.js",
1414
"src/**/*.test.jsx",
15-
"src/**/*.d.ts"
15+
"src/**/*.d.ts",
16+
"../../testing/test-setup/src/vitest.d.ts"
1617
]
1718
}

packages/cli/vite.config.unit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default defineConfig({
2828
'../../testing/test-setup/src/lib/console.mock.ts',
2929
'../../testing/test-setup/src/lib/portal-client.mock.ts',
3030
'../../testing/test-setup/src/lib/reset.mocks.ts',
31+
'../../testing/test-setup/src/lib/extend/ui-logger.matcher.ts',
3132
],
3233
},
3334
});

0 commit comments

Comments
 (0)