Skip to content

Commit fc0415d

Browse files
committed
test(plugin-lighthouse): update unit tests to use custom matchers
1 parent fa8cad5 commit fc0415d

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

packages/plugin-lighthouse/src/lib/normalize-flags.unit.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { bold, yellow } from 'ansis';
22
import path from 'node:path';
33
import { describe, expect, it } from 'vitest';
4-
import { getLogMessages } from '@code-pushup/test-utils';
54
import { ui } from '@code-pushup/utils';
65
import { DEFAULT_CHROME_FLAGS, LIGHTHOUSE_OUTPUT_PATH } from './constants.js';
76
import { logUnsupportedFlagsInUse, normalizeFlags } from './normalize-flags.js';
@@ -11,9 +10,10 @@ import type { LighthouseOptions } from './types.js';
1110
describe('logUnsupportedFlagsInUse', () => {
1211
it('should log unsupported entries', () => {
1312
logUnsupportedFlagsInUse({ 'list-all-audits': true } as LighthouseOptions);
14-
expect(getLogMessages(ui().logger)).toHaveLength(1);
15-
expect(getLogMessages(ui().logger).at(0)).toBe(
16-
`[ cyan(debug) ] ${yellow('⚠')} Plugin ${bold(
13+
expect(ui()).toHaveLoggedTimes(1);
14+
expect(ui()).toHaveLoggedLevel('debug');
15+
expect(ui()).toHaveLoggedMessage(
16+
`${yellow('⚠')} Plugin ${bold(
1717
'lighthouse',
1818
)} used unsupported flags: ${bold('list-all-audits')}`,
1919
);
@@ -32,9 +32,10 @@ describe('logUnsupportedFlagsInUse', () => {
3232
// unsupported
3333
...unsupportedFlags,
3434
} as unknown as LighthouseOptions);
35-
expect(getLogMessages(ui().logger)).toHaveLength(1);
36-
expect(getLogMessages(ui().logger).at(0)).toBe(
37-
`[ cyan(debug) ] ${yellow('⚠')} Plugin ${bold(
35+
expect(ui()).toHaveLoggedTimes(1);
36+
expect(ui()).toHaveLoggedLevel('debug');
37+
expect(ui()).toHaveLoggedMessage(
38+
`${yellow('⚠')} Plugin ${bold(
3839
'lighthouse',
3940
)} used unsupported flags: ${bold(
4041
'list-all-audits, list-locales, list-trace-categories',
@@ -118,7 +119,7 @@ describe('normalizeFlags', () => {
118119
...supportedFlags,
119120
} as unknown as LighthouseOptions),
120121
).toEqual(expect.not.objectContaining({ 'list-all-audits': true }));
121-
expect(getLogMessages(ui().logger)).toHaveLength(1);
122+
expect(ui()).toHaveLoggedTimes(1);
122123
});
123124

124125
it('should remove any flag with an empty array as a value', () => {

packages/plugin-lighthouse/src/lib/runner/details/details.unit.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { FormattedIcu } from 'lighthouse';
33
import type Details from 'lighthouse/types/lhr/audit-details';
44
import type { Result } from 'lighthouse/types/lhr/audit-result';
55
import { describe, expect, it } from 'vitest';
6-
import { getLogMessages } from '@code-pushup/test-utils';
76
import { ui } from '@code-pushup/utils';
87
import { logUnsupportedDetails, toAuditDetails } from './details.js';
98

@@ -12,9 +11,10 @@ describe('logUnsupportedDetails', () => {
1211
logUnsupportedDetails([
1312
{ details: { type: 'screenshot' } },
1413
] as unknown as Result[]);
15-
expect(getLogMessages(ui().logger)).toHaveLength(1);
16-
expect(getLogMessages(ui().logger).at(0)).toBe(
17-
`[ cyan(debug) ] ${yellow('⚠')} Plugin ${bold(
14+
expect(ui()).toHaveLoggedTimes(1);
15+
expect(ui()).toHaveLoggedLevel('debug');
16+
expect(ui()).toHaveLoggedMessage(
17+
`${yellow('⚠')} Plugin ${bold(
1818
'lighthouse',
1919
)} skipped parsing of unsupported audit details: ${bold('screenshot')}`,
2020
);
@@ -30,9 +30,10 @@ describe('logUnsupportedDetails', () => {
3030
{ details: { type: 'treemap-data' } },
3131
{ details: { type: 'criticalrequestchain' } },
3232
] as unknown as Result[]);
33-
expect(getLogMessages(ui().logger)).toHaveLength(1);
34-
expect(getLogMessages(ui().logger).at(0)).toBe(
35-
`[ cyan(debug) ] ${yellow('⚠')} Plugin ${bold(
33+
expect(ui()).toHaveLoggedTimes(1);
34+
expect(ui()).toHaveLoggedLevel('debug');
35+
expect(ui()).toHaveLoggedMessage(
36+
`${yellow('⚠')} Plugin ${bold(
3637
'lighthouse',
3738
)} skipped parsing of unsupported audit details: ${bold(
3839
'filmstrip, screenshot, debugdata',

packages/plugin-lighthouse/src/lib/runner/details/item-value.unit.test.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { bold } from 'ansis';
22
import type Details from 'lighthouse/types/lhr/audit-details';
33
import { beforeAll, describe, expect, it } from 'vitest';
4-
import { getLogMessages } from '@code-pushup/test-utils';
54
import { ui } from '@code-pushup/utils';
65
import {
76
type SimpleItemValue,
@@ -147,15 +146,17 @@ describe('parseTableItemPropertyValue', () => {
147146
}),
148147
).toBe('');
149148

150-
expect(getLogMessages(ui().logger).at(0)).toBe(
151-
`[ blue(info) ] Value type ${bold('subitems')} is not implemented`,
149+
expect(ui()).toHaveLoggedLevel('info');
150+
expect(ui()).toHaveLoggedMessage(
151+
`Value type ${bold('subitems')} is not implemented`,
152152
);
153153
});
154154

155155
it('should parse value item debugdata to empty string and log implemented', () => {
156156
expect(parseTableItemPropertyValue({ type: 'debugdata' })).toBe('');
157-
expect(getLogMessages(ui().logger).at(0)).toBe(
158-
`[ blue(info) ] Value type ${bold('debugdata')} is not implemented`,
157+
expect(ui()).toHaveLoggedLevel('info');
158+
expect(ui()).toHaveLoggedMessage(
159+
`Value type ${bold('debugdata')} is not implemented`,
159160
);
160161
});
161162

@@ -362,8 +363,9 @@ describe('formatTableItemPropertyValue', () => {
362363
),
363364
).toBe('');
364365

365-
expect(getLogMessages(ui().logger).at(0)).toBe(
366-
`[ blue(info) ] Format type ${bold('multi')} is not implemented`,
366+
expect(ui()).toHaveLoggedLevel('info');
367+
expect(ui()).toHaveLoggedMessage(
368+
`Format type ${bold('multi')} is not implemented`,
367369
);
368370
});
369371

@@ -374,8 +376,9 @@ describe('formatTableItemPropertyValue', () => {
374376
'thumbnail',
375377
),
376378
).toBe('');
377-
expect(getLogMessages(ui().logger).at(0)).toBe(
378-
`[ blue(info) ] Format type ${bold('thumbnail')} is not implemented`,
379+
expect(ui()).toHaveLoggedLevel('info');
380+
expect(ui()).toHaveLoggedMessage(
381+
`Format type ${bold('thumbnail')} is not implemented`,
379382
);
380383
});
381384

packages/plugin-lighthouse/src/lib/runner/utils.unit.test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
type CoreConfig,
1212
auditOutputsSchema,
1313
} from '@code-pushup/models';
14-
import { MEMFS_VOLUME, getLogMessages } from '@code-pushup/test-utils';
14+
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
1515
import { ui } from '@code-pushup/utils';
1616
import { unsupportedDetailTypes } from './details/details.js';
1717
import {
@@ -242,7 +242,7 @@ describe('toAuditOutputs', () => {
242242
}) as Result,
243243
),
244244
);
245-
expect(getLogMessages(ui().logger)).toHaveLength(0);
245+
expect(ui()).not.toHaveLogged();
246246
});
247247

248248
it('should inform that for all unsupported details if verbose IS given', () => {
@@ -260,7 +260,7 @@ describe('toAuditOutputs', () => {
260260
),
261261
{ verbose: true },
262262
);
263-
expect(getLogMessages(ui().logger)).toHaveLength(1);
263+
expect(ui()).toHaveLoggedTimes(1);
264264
});
265265

266266
it('should not parse empty audit details', () => {
@@ -343,9 +343,7 @@ describe('getConfig', () => {
343343
await expect(
344344
getConfig({ preset: 'wrong' as 'desktop' }),
345345
).resolves.toBeUndefined();
346-
expect(getLogMessages(ui().logger).at(0)).toMatch(
347-
'Preset "wrong" is not supported',
348-
);
346+
expect(ui()).toHaveLoggedMessage('Preset "wrong" is not supported');
349347
});
350348

351349
it('should load config from json file if configPath is specified', async () => {
@@ -378,9 +376,7 @@ describe('getConfig', () => {
378376
await expect(
379377
getConfig({ configPath: path.join('wrong.not') }),
380378
).resolves.toBeUndefined();
381-
expect(getLogMessages(ui().logger).at(0)).toMatch(
382-
'Format of file wrong.not not supported',
383-
);
379+
expect(ui()).toHaveLoggedMessage('Format of file wrong.not not supported');
384380
});
385381
});
386382

packages/plugin-lighthouse/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/plugin-lighthouse/vite.config.unit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default defineConfig({
2626
'../../testing/test-setup/src/lib/fs.mock.ts',
2727
'../../testing/test-setup/src/lib/console.mock.ts',
2828
'../../testing/test-setup/src/lib/reset.mocks.ts',
29+
'../../testing/test-setup/src/lib/extend/ui-logger.matcher.ts',
2930
],
3031
},
3132
});

0 commit comments

Comments
 (0)