Skip to content

Commit

Permalink
test: update unit tests to use simplified custom matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
hanna-skryl committed Jan 17, 2025
1 parent b5eee9b commit 60d7b79
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 72 deletions.
4 changes: 2 additions & 2 deletions packages/cli/src/lib/compare/compare-command.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ describe('compare-command', () => {
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsCompareCommandObject()] },
).parseAsync();

expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Reports diff written to ${bold(
'.code-pushup/report-diff.json',
)} and ${bold('.code-pushup/report-diff.md')}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ describe('filterMiddleware', () => {
verbose: true,
});

expect(ui()).toHaveLoggedNthLevel(1, 'info');
expect(ui()).toHaveLoggedNthLevel(2, 'info');
expect(ui()).toHaveLoggedNthMessage(
expect(ui()).toHaveNthLogged(
1,
'info',
'The --skipPlugins argument removed the following categories: c1, c2.',
);
expect(ui()).toHaveLoggedNthMessage(
expect(ui()).toHaveNthLogged(
2,
'info',
'The --onlyPlugins argument removed the following categories: c1, c2.',
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ describe('logErrorBeforeThrow', () => {
} catch {
/* suppress */
}
expect(ui()).toHaveLoggedLevel('error');
expect(ui()).toHaveLoggedMessage('Option validation failed');
expect(ui()).toHaveLogged('error', 'Option validation failed');
});

it('should rethrow errors other than OptionValidationError', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('validateFilterOption', () => {
},
{ itemsToFilter, skippedItems: [], verbose: false },
);
expect(ui()).toHaveLoggedMessage(expected);
expect(ui()).toHaveLogged('warn', expected);
},
);

Expand Down Expand Up @@ -93,7 +93,7 @@ describe('validateFilterOption', () => {
},
{ itemsToFilter, skippedItems: [], verbose: false },
);
expect(ui()).toHaveLoggedMessage(expected);
expect(ui()).toHaveLogged('warn', expected);
},
);

Expand All @@ -108,7 +108,7 @@ describe('validateFilterOption', () => {
},
{ itemsToFilter: ['p1'], skippedItems: [], verbose: false },
);
expect(ui()).not.toHaveLogged();
expect(ui()).not.toHaveLogs();
});

it('should log a category ignored as a result of plugin filtering', () => {
Expand All @@ -128,7 +128,8 @@ describe('validateFilterOption', () => {
{ itemsToFilter: ['p1'], skippedItems: [], verbose: true },
);
expect(ui()).toHaveLoggedTimes(1);
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
'The --onlyPlugins argument removed the following categories: c1, c3.',
);
});
Expand Down Expand Up @@ -218,12 +219,14 @@ describe('validateFilterOption', () => {
{ plugins, categories },
{ itemsToFilter: ['p1'], skippedItems: ['p1'], verbose: true },
);
expect(ui()).toHaveLoggedNthMessage(
expect(ui()).toHaveNthLogged(
1,
'warn',
'The --skipPlugins argument references a skipped plugin: p1.',
);
expect(ui()).toHaveLoggedNthMessage(
expect(ui()).toHaveNthLogged(
2,
'info',
'The --skipPlugins argument removed the following categories: c1.',
);
});
Expand Down Expand Up @@ -457,8 +460,8 @@ describe('validateSkippedCategories', () => {
] as NonNullable<Filterables['categories']>,
true,
);
expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
'Category c1 was removed because all its refs were skipped. Affected refs: g1 (group)',
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ describe('merge-diffs-command', () => {
},
).parseAsync();

expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Reports diff written to ${bold('.code-pushup/report-diff.md')}`,
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ describe('print-config-command', () => {
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsConfigCommandObject()] },
).parseAsync();

expect(ui()).not.toHaveLoggedMessageContaining('"$0":');
expect(ui()).not.toHaveLoggedMessageContaining('"_":');
expect(ui()).not.toHaveLogged('log', expect.stringContaining('"$0":'));
expect(ui()).not.toHaveLogged('log', expect.stringContaining('"_":'));

expect(ui()).toHaveLoggedMessageContaining('"outputDir": "destinationDir"');
expect(ui()).not.toHaveLoggedMessageContaining('"output-dir":');
expect(ui()).toHaveLogged(
'log',
expect.stringContaining('"outputDir": "destinationDir"'),
);
expect(ui()).not.toHaveLogged(
'log',
expect.stringContaining('"output-dir":'),
);
});
});
2 changes: 1 addition & 1 deletion packages/core/src/lib/collect-and-persist.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ describe('collectAndPersistReports', () => {
await collectAndPersistReports(
MINIMAL_CONFIG_MOCK as CollectAndPersistReportsOptions,
);
expect(ui()).toHaveLoggedMessage('Made with ❤ by code-pushup.dev');
expect(ui()).toHaveLogged('log', 'Made with ❤ by code-pushup.dev');
});
});
49 changes: 34 additions & 15 deletions packages/core/src/lib/implementation/persist.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,35 +90,54 @@ describe('persistReport', () => {
describe('logPersistedResults', () => {
it('should log report sizes correctly`', () => {
logPersistedResults([{ status: 'fulfilled', value: ['out.json', 10_000] }]);
expect(ui()).toHaveLoggedNthLevel(1, 'success');
expect(ui()).toHaveLoggedNthMessageContaining(
expect(ui()).toHaveNthLogged(
1,
'Generated reports successfully: ',
'success',
expect.stringContaining('Generated reports successfully: '),
);
expect(ui()).toHaveNthLogged(
2,
'success',
expect.stringContaining('9.77 kB'),
);
expect(ui()).toHaveNthLogged(
2,
'success',
expect.stringContaining('out.json'),
);
expect(ui()).toHaveLoggedNthMessageContaining(2, '9.77 kB');
expect(ui()).toHaveLoggedNthMessageContaining(2, 'out.json');
});

it('should log fails correctly`', () => {
logPersistedResults([{ status: 'rejected', reason: 'fail' }]);
expect(ui()).toHaveLoggedNthLevel(1, 'warn');
expect(ui()).toHaveLoggedNthMessage(1, 'Generated reports failed: ');
expect(ui()).toHaveLoggedNthMessageContaining(2, 'fail');
expect(ui()).toHaveNthLogged(1, 'warn', 'Generated reports failed: ');
expect(ui()).toHaveNthLogged(2, 'warn', expect.stringContaining('fail'));
});

it('should log report sizes and fails correctly`', () => {
logPersistedResults([
{ status: 'fulfilled', value: ['out.json', 10_000] },
{ status: 'rejected', reason: 'fail' },
]);
expect(ui()).toHaveLoggedNthLevel(1, 'success');
expect(ui()).toHaveLoggedNthMessage(1, 'Generated reports successfully: ');
expect(ui()).toHaveLoggedNthMessageContaining(2, 'out.json');
expect(ui()).toHaveLoggedNthMessageContaining(2, '9.77 kB');
expect(ui()).toHaveLoggedNthMessageContaining(
expect(ui()).toHaveNthLogged(
1,
'success',
'Generated reports successfully: ',
);
expect(ui()).toHaveNthLogged(
2,
'success',
expect.stringContaining('out.json'),
);
expect(ui()).toHaveNthLogged(
2,
'success',
expect.stringContaining('9.77 kB'),
);
expect(ui()).toHaveNthLogged(
3,
'Generated reports failed: ',
'warn',
expect.stringContaining('Generated reports failed: '),
);
expect(ui()).toHaveLoggedNthMessageContaining(3, 'fail');
expect(ui()).toHaveNthLogged(3, 'warn', expect.stringContaining('fail'));
});
});
14 changes: 10 additions & 4 deletions packages/core/src/lib/merge-diffs.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ describe('mergeDiffs', () => {
),
).resolves.toBe(path.join(MEMFS_VOLUME, 'report-diff.md'));

expect(ui()).toHaveLoggedNthMessageContaining(
expect(ui()).toHaveNthLogged(
1,
'Skipped invalid report diff - Failed to read JSON file missing-report-diff.json',
'warn',
expect.stringContaining(
'Skipped invalid report diff - Failed to read JSON file missing-report-diff.json',
),
);
expect(ui()).toHaveLoggedNthMessageContaining(
expect(ui()).toHaveNthLogged(
2,
'Skipped invalid report diff - Invalid reports diff in invalid-report-diff.json',
'warn',
expect.stringContaining(
'Skipped invalid report diff - Invalid reports diff in invalid-report-diff.json',
),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ end_of_record
path.join('coverage', 'lcov.info'),
]);

expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'warn',
`Coverage plugin: Empty lcov report file detected at ${path.join(
'coverage',
'lcov.info',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('logUnsupportedFlagsInUse', () => {
it('should log unsupported entries', () => {
logUnsupportedFlagsInUse({ 'list-all-audits': true } as LighthouseOptions);
expect(ui()).toHaveLoggedTimes(1);
expect(ui()).toHaveLoggedLevel('debug');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'debug',
`${yellow('⚠')} Plugin ${bold(
'lighthouse',
)} used unsupported flags: ${bold('list-all-audits')}`,
Expand All @@ -33,8 +33,8 @@ describe('logUnsupportedFlagsInUse', () => {
...unsupportedFlags,
} as unknown as LighthouseOptions);
expect(ui()).toHaveLoggedTimes(1);
expect(ui()).toHaveLoggedLevel('debug');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'debug',
`${yellow('⚠')} Plugin ${bold(
'lighthouse',
)} used unsupported flags: ${bold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('logUnsupportedDetails', () => {
{ details: { type: 'screenshot' } },
] as unknown as Result[]);
expect(ui()).toHaveLoggedTimes(1);
expect(ui()).toHaveLoggedLevel('debug');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'debug',
`${yellow('⚠')} Plugin ${bold(
'lighthouse',
)} skipped parsing of unsupported audit details: ${bold('screenshot')}`,
Expand All @@ -31,8 +31,8 @@ describe('logUnsupportedDetails', () => {
{ details: { type: 'criticalrequestchain' } },
] as unknown as Result[]);
expect(ui()).toHaveLoggedTimes(1);
expect(ui()).toHaveLoggedLevel('debug');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'debug',
`${yellow('⚠')} Plugin ${bold(
'lighthouse',
)} skipped parsing of unsupported audit details: ${bold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ describe('parseTableItemPropertyValue', () => {
}),
).toBe('');

expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Value type ${bold('subitems')} is not implemented`,
);
});

it('should parse value item debugdata to empty string and log implemented', () => {
expect(parseTableItemPropertyValue({ type: 'debugdata' })).toBe('');
expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Value type ${bold('debugdata')} is not implemented`,
);
});
Expand Down Expand Up @@ -363,8 +363,8 @@ describe('formatTableItemPropertyValue', () => {
),
).toBe('');

expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Format type ${bold('multi')} is not implemented`,
);
});
Expand All @@ -376,8 +376,8 @@ describe('formatTableItemPropertyValue', () => {
'thumbnail',
),
).toBe('');
expect(ui()).toHaveLoggedLevel('info');
expect(ui()).toHaveLoggedMessage(
expect(ui()).toHaveLogged(
'info',
`Format type ${bold('thumbnail')} is not implemented`,
);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-lighthouse/src/lib/runner/utils.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ describe('toAuditOutputs', () => {
}) as Result,
),
);
expect(ui()).not.toHaveLogged();
expect(ui()).not.toHaveLogs();
});

it('should inform that for all unsupported details if verbose IS given', () => {
Expand Down Expand Up @@ -343,7 +343,7 @@ describe('getConfig', () => {
await expect(
getConfig({ preset: 'wrong' as 'desktop' }),
).resolves.toBeUndefined();
expect(ui()).toHaveLoggedMessage('Preset "wrong" is not supported');
expect(ui()).toHaveLogged('info', 'Preset "wrong" is not supported');
});

it('should load config from json file if configPath is specified', async () => {
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('getConfig', () => {
await expect(
getConfig({ configPath: path.join('wrong.not') }),
).resolves.toBeUndefined();
expect(ui()).toHaveLoggedMessage('Format of file wrong.not not supported');
expect(ui()).toHaveLogged('info', 'Format of file wrong.not not supported');
});
});

Expand Down
16 changes: 8 additions & 8 deletions packages/utils/src/lib/log-results.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ describe('logPromiseResults', () => {
'Uploaded reports successfully:',
(result): string => result.value.toString(),
);
expect(ui()).toHaveLoggedNthLevel(1, 'success');
expect(ui()).toHaveLoggedNthMessage(1, 'Uploaded reports successfully:');
expect(ui()).toHaveLoggedNthLevel(2, 'success');
expect(ui()).toHaveLoggedNthMessage(2, 'out.json');
expect(ui()).toHaveNthLogged(
1,
'success',
'Uploaded reports successfully:',
);
expect(ui()).toHaveNthLogged(2, 'success', 'out.json');
});

it('should log on fail', () => {
Expand All @@ -79,9 +81,7 @@ describe('logPromiseResults', () => {
'Generated reports failed:',
(result: { reason: string }) => result.reason,
);
expect(ui()).toHaveLoggedNthLevel(1, 'warn');
expect(ui()).toHaveLoggedNthMessage(1, 'Generated reports failed:');
expect(ui()).toHaveLoggedNthLevel(2, 'warn');
expect(ui()).toHaveLoggedNthMessage(2, 'fail');
expect(ui()).toHaveNthLogged(1, 'warn', 'Generated reports failed:');
expect(ui()).toHaveNthLogged(2, 'warn', 'fail');
});
});
Loading

0 comments on commit 60d7b79

Please sign in to comment.