Skip to content

Commit b1f13c3

Browse files
committed
test(core): simplify mocks
1 parent 1cdb088 commit b1f13c3

4 files changed

Lines changed: 32 additions & 56 deletions

File tree

packages/core/src/lib/collect-and-persist.unit.test.ts

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,32 @@ import { collectAndPersistReports } from './collect-and-persist';
88
import { collect } from './implementation/collect';
99
import { logPersistedResults, persistReport } from './implementation/persist';
1010

11-
vi.mock('./implementation/collect', async () => {
12-
return {
13-
collect: vi.fn().mockImplementation(
14-
async () =>
15-
({
16-
packageName: 'code-pushup',
17-
version: '0.0.1',
18-
date: new Date().toISOString(),
19-
duration: 0,
20-
categories: [],
21-
plugins: [],
22-
} as Report),
23-
),
24-
};
25-
});
11+
vi.mock('./implementation/collect', () => ({
12+
collect: vi.fn().mockResolvedValue({
13+
packageName: 'code-pushup',
14+
version: '0.0.1',
15+
date: new Date().toISOString(),
16+
duration: 0,
17+
categories: [],
18+
plugins: [],
19+
} as Report),
20+
}));
2621

27-
vi.mock('./implementation/persist', async () => {
28-
return {
29-
persistReport: vi.fn().mockImplementation(async () => ({})),
30-
logPersistedResults: vi.fn().mockReturnValue(undefined),
31-
};
32-
});
22+
vi.mock('./implementation/persist', () => ({
23+
persistReport: vi.fn(),
24+
logPersistedResults: vi.fn(),
25+
}));
3326

3427
describe('collectAndPersistReports', () => {
3528
it('should call collect and persistReport with correct parameters in non-verbose mode', async () => {
36-
await collectAndPersistReports({
29+
const nonVerboseConfig = {
3730
...MINIMAL_CONFIG_MOCK,
3831
verbose: false,
3932
progress: false,
40-
});
33+
};
34+
await collectAndPersistReports(nonVerboseConfig);
4135

42-
expect(collect).toHaveBeenCalledWith({
43-
...MINIMAL_CONFIG_MOCK,
44-
verbose: false,
45-
progress: false,
46-
});
36+
expect(collect).toHaveBeenCalledWith(nonVerboseConfig);
4737

4838
expect(persistReport).toHaveBeenCalledWith(
4939
{
@@ -54,28 +44,21 @@ describe('collectAndPersistReports', () => {
5444
categories: [],
5545
plugins: [],
5646
},
57-
{
58-
...MINIMAL_CONFIG_MOCK,
59-
verbose: false,
60-
progress: false,
61-
},
47+
nonVerboseConfig,
6248
);
6349

6450
expect(logPersistedResults).not.toHaveBeenCalled();
6551
});
6652

6753
it('should call collect and persistReport with correct parameters in verbose mode', async () => {
68-
await collectAndPersistReports({
54+
const verboseConfig = {
6955
...MINIMAL_CONFIG_MOCK,
7056
verbose: true,
7157
progress: false,
72-
});
58+
};
59+
await collectAndPersistReports(verboseConfig);
7360

74-
expect(collect).toHaveBeenCalledWith({
75-
...MINIMAL_CONFIG_MOCK,
76-
verbose: true,
77-
progress: false,
78-
});
61+
expect(collect).toHaveBeenCalledWith(verboseConfig);
7962

8063
expect(persistReport).toHaveBeenCalledWith(
8164
{
@@ -86,11 +69,7 @@ describe('collectAndPersistReports', () => {
8669
categories: [],
8770
plugins: [],
8871
} as Report,
89-
{
90-
...MINIMAL_CONFIG_MOCK,
91-
verbose: true,
92-
progress: false,
93-
},
72+
verboseConfig,
9473
);
9574

9675
expect(logPersistedResults).toHaveBeenCalled();

packages/core/src/lib/implementation/collect.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('collect', () => {
2929
);
3030
});
3131

32-
it('should execute when no plugins are passed', async () => {
32+
it('should throw when no plugins are passed', async () => {
3333
await expect(
3434
collect({
3535
...MINIMAL_CONFIG_MOCK,

packages/core/src/lib/upload.unit.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ISO_STRING_REGEXP,
77
MINIMAL_REPORT_MOCK,
88
} from '@code-pushup/testing-utils';
9-
import { UploadOptions, upload } from './upload';
9+
import { upload } from './upload';
1010

1111
describe('upload', () => {
1212
it('upload should be called with correct data', async () => {
@@ -31,9 +31,9 @@ describe('upload', () => {
3131
filename: 'report',
3232
format: ['json'],
3333
},
34-
} as UploadOptions);
34+
});
3535

36-
expect(result).toEqual({ packageName: '@code-pushup/cli' }); // TODO is this correct?
36+
expect(result).toEqual({ packageName: '@code-pushup/cli' });
3737

3838
expect(uploadToPortal).toHaveBeenCalledWith({
3939
apiKey: 'dummy-api-key',

testing-utils/src/lib/fixtures/configs/minimal-config.mock.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {
2+
AuditOutput,
23
CoreConfig,
3-
Issue,
44
PluginConfig,
55
RunnerConfig,
66
RunnerFunction,
@@ -17,9 +17,6 @@ export const MINIMAL_RUNNER_CONFIG_MOCK: RunnerConfig = {
1717
export const MINIMAL_RUNNER_FUNCTION_MOCK: RunnerFunction = () => [
1818
{
1919
slug: 'node-version',
20-
title: 'Node version',
21-
description: 'Returns node version',
22-
docsUrl: 'https://nodejs.org/',
2320
score: 0.3,
2421
value: 16,
2522
displayValue: '16.0.0',
@@ -28,10 +25,10 @@ export const MINIMAL_RUNNER_FUNCTION_MOCK: RunnerFunction = () => [
2825
{
2926
severity: 'error',
3027
message: 'The required Node version to run Code PushUp CLI is 18.',
31-
} satisfies Issue,
28+
},
3229
],
3330
},
34-
},
31+
} satisfies AuditOutput,
3532
];
3633

3734
export const MINIMAL_PLUGIN_CONFIG_MOCK: PluginConfig = {
@@ -50,7 +47,7 @@ export const MINIMAL_PLUGIN_CONFIG_MOCK: PluginConfig = {
5047
};
5148

5249
export const MINIMAL_CONFIG_MOCK: CoreConfig = {
53-
persist: { outputDir: 'test', filename: 'report.json' },
50+
persist: { outputDir: '/test', filename: 'report.json' },
5451
upload: {
5552
organization: 'code-pushup',
5653
project: 'cli',

0 commit comments

Comments
 (0)