Skip to content

Commit fd176bb

Browse files
committed
test(utils): improve test and mock naming, clean up tests
1 parent ead9fd6 commit fd176bb

20 files changed

Lines changed: 205 additions & 201 deletions

packages/models/src/lib/audit.unit.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { describe, expect, it } from 'vitest';
2-
import { report } from '../../test';
2+
import { reportMock } from '../../test';
33
import { pluginConfigSchema } from './plugin-config';
44
import { auditOutputsSchema } from './plugin-process-output';
55

66
describe('auditOutputsSchema', () => {
77
it('should pass if output audits are valid', () => {
8-
const auditOutputs = report().plugins[0].audits;
8+
const auditOutputs = reportMock().plugins[0].audits;
99
expect(() => auditOutputsSchema.parse(auditOutputs)).not.toThrow();
1010
});
1111

1212
it('should throw if slugs of audits are invalid', () => {
13-
const auditOutputs = report().plugins[0].audits;
13+
const auditOutputs = reportMock().plugins[0].audits;
1414
auditOutputs[0].slug = '-invalid-audit-slug';
1515

1616
expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
@@ -19,7 +19,7 @@ describe('auditOutputsSchema', () => {
1919
});
2020

2121
it('should throw if slugs of audits are duplicated', () => {
22-
const audits = report().plugins[0].audits;
22+
const audits = reportMock().plugins[0].audits;
2323
const auditOutputs = [...audits, audits[0]];
2424
expect(() => auditOutputsSchema.parse(auditOutputs)).toThrow(
2525
'In plugin audits the slugs are not unique',
@@ -28,7 +28,7 @@ describe('auditOutputsSchema', () => {
2828

2929
it('should throw if plugin groups refs contain invalid slugs', () => {
3030
const invalidAuditRef = '-invalid-audit-ref';
31-
const pluginConfig = report().plugins[1];
31+
const pluginConfig = reportMock().plugins[1];
3232
const groups = pluginConfig.groups;
3333
groups[0].refs[0].slug = invalidAuditRef;
3434
pluginConfig.groups = groups;

packages/models/test/fixtures/report.mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { categoryConfigs } from './categories.mock';
33
import { eslintPluginReport } from './eslint-plugin.mock';
44
import { lighthousePluginReport } from './lighthouse-plugin.mock';
55

6-
export function report(): Report {
6+
export function reportMock(): Report {
77
return reportSchema.parse({
88
packageName: '@code-pushup/core',
99
version: '0.0.1',

packages/models/test/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export { config, minimalConfig, minimalReport } from './fixtures/config.mock';
2-
export { report } from './fixtures/report.mock';
2+
export { reportMock } from './fixtures/report.mock';
33
export * from './memfs';
44

55
export { eslintPluginConfig } from './fixtures/eslint-plugin.mock';
66
export { lighthousePluginConfig } from './fixtures/lighthouse-plugin.mock';
77

8-
export { echoRunnerConfig } from './fixtures/runner-config.mock';
8+
export { categoryConfig } from './fixtures/categories.mock';
99
export { persistConfig } from './fixtures/persist-config.mock';
1010
export {
1111
auditConfig,
1212
auditReport,
1313
pluginConfig,
1414
} from './fixtures/plugin-config.mock';
15+
export { echoRunnerConfig } from './fixtures/runner-config.mock';
1516
export { uploadConfig } from './fixtures/upload-config.mock';
16-
export { categoryConfig } from './fixtures/categories.mock';

packages/utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export {
1818
pluginWorkDir,
1919
readJsonFile,
2020
readTextFile,
21-
toUnixPath,
2221
} from './lib/file-system';
2322
export {
2423
formatBytes,
@@ -58,5 +57,6 @@ export {
5857
objectToEntries,
5958
objectToKeys,
6059
toArray,
60+
toUnixPath,
6161
} from './lib/transform';
6262
export { verboseUtils } from './lib/verbose-utils';

packages/utils/src/lib/__snapshots__/report-to-md.integration.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ ESLint rule **prefer-arrow-callback**. [📖 Docs](https://eslint.org/docs/lates
420420
421421
## About
422422
423-
Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme) on Sat Jan 01 2000 00:00:00 GMT+0000 (Coordinated Universal Time).
423+
Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cli#readme) on Wed Sep 15 2021 00:00:00 GMT+0000 (Coordinated Universal Time).
424424
425425
|Commit|Version|Duration|Plugins|Categories|Audits|
426426
|:--|:--:|:--:|:--:|:--:|:--:|
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect } from 'vitest';
2+
import { getLatestCommit } from './git';
3+
4+
const gitCommitDateRegex =
5+
/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{2}:\d{2}:\d{2} \d{4} [+|-]\d{4}$/;
6+
7+
describe('getLatestCommit', () => {
8+
it('should log latest commit', async () => {
9+
await expect(getLatestCommit()).resolves.toEqual(
10+
expect.objectContaining({
11+
hash: expect.stringMatching(/^[0-9a-f]{40}$/),
12+
message: expect.stringMatching(/.+/),
13+
author: expect.stringMatching(/.+/),
14+
date: expect.stringMatching(gitCommitDateRegex),
15+
}),
16+
);
17+
});
18+
});

packages/utils/src/lib/git.unit.test.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
1-
import chalk from 'chalk';
2-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
32
import { FileResult } from './file-system';
4-
import { formatBytes } from './formatting';
53
import { logMultipleResults, logPromiseResults } from './log-results';
64

7-
const succeededCallback = (result: PromiseFulfilledResult<FileResult>) => {
8-
const [fileName, size] = result.value;
9-
console.info(
10-
`- ${chalk.bold(fileName)}` +
11-
(size ? ` (${chalk.gray(formatBytes(size))})` : ''),
12-
);
13-
};
14-
15-
const failedCallback = (result: PromiseRejectedResult) => {
16-
console.warn(`- ${chalk.bold(result.reason)}`);
17-
};
18-
195
describe('logMultipleResults', () => {
20-
const succeededCallbackMock = vi.fn().mockImplementation(succeededCallback);
21-
const failedCallbackMock = vi.fn().mockImplementation(failedCallback);
22-
23-
beforeEach(() => {
24-
succeededCallbackMock.mockClear();
25-
failedCallbackMock.mockClear();
26-
});
27-
28-
afterEach(() => {
29-
vi.restoreAllMocks();
30-
});
6+
const succeededCallbackMock = vi.fn();
7+
const failedCallbackMock = vi.fn();
318

329
it('should call logPromiseResults with successfull plugin result', async () => {
3310
logMultipleResults(
@@ -72,8 +49,8 @@ describe('logMultipleResults', () => {
7249
failedCallbackMock,
7350
);
7451

75-
expect(succeededCallbackMock).toHaveBeenCalled();
76-
expect(failedCallbackMock).toHaveBeenCalled();
52+
expect(succeededCallbackMock).toHaveBeenCalledOnce();
53+
expect(failedCallbackMock).toHaveBeenCalledOnce();
7754
});
7855
});
7956

@@ -86,30 +63,32 @@ describe('logPromiseResults', () => {
8663
value: ['out.json'],
8764
} as PromiseFulfilledResult<FileResult>,
8865
],
89-
'Uploaded reports successfully: ',
90-
succeededCallback,
66+
'Uploaded reports successfully:',
67+
result => {
68+
console.info(result.value);
69+
},
9170
);
9271

93-
expect(console.info).toHaveBeenCalledWith(
94-
expect.stringContaining('Uploaded reports successfully: '),
95-
);
96-
expect(console.info).toHaveBeenCalledWith(
97-
expect.stringContaining('- out.json'),
72+
expect(console.info).toHaveBeenNthCalledWith(
73+
1,
74+
'Uploaded reports successfully:',
9875
);
76+
expect(console.info).toHaveBeenNthCalledWith(2, ['out.json']);
9977
});
10078

10179
it('should log on fail', async () => {
10280
logPromiseResults(
10381
[{ status: 'rejected', reason: 'fail' } as PromiseRejectedResult],
104-
'Generated reports failed: ',
105-
failedCallback,
82+
'Generated reports failed:',
83+
result => {
84+
console.warn(result.reason);
85+
},
10686
);
10787

108-
expect(console.warn).toHaveBeenCalledWith(
109-
expect.stringContaining('Generated reports failed: '),
110-
);
111-
expect(console.warn).toHaveBeenCalledWith(
112-
expect.stringContaining('- fail'),
88+
expect(console.warn).toHaveBeenNthCalledWith(
89+
1,
90+
'Generated reports failed:',
11391
);
92+
expect(console.warn).toHaveBeenNthCalledWith(2, 'fail');
11493
});
11594
});

packages/utils/src/lib/md/font-style.unit.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ describe('style', () => {
2626
expect(result).toBe('~**_Hello World_**~');
2727
});
2828

29-
it('should handle empty styles array', () => {
29+
it('should return unchanged text for an empty styles array', () => {
3030
const result = style('Hello World', []);
3131
expect(result).toBe('Hello World');
3232
});
3333

34-
it('should handle empty text', () => {
34+
it('should apply formatting on empty string', () => {
3535
const result = style('', ['i', 'b']);
3636
expect(result).toBe('**__**');
3737
});

packages/utils/src/lib/md/headline.unit.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('headline', () => {
3131
expect(result).toBe('###### Hello World');
3232
});
3333

34-
it('should handle empty text', () => {
34+
it('should still insert heading for empty string', () => {
3535
const result = headline('', 3);
3636
expect(result).toBe('### ');
3737
});

0 commit comments

Comments
 (0)