Skip to content

Commit 4396fb4

Browse files
committed
refactor(plugin-coverage): use explicit calls in binding tests
1 parent 9bc20e0 commit 4396fb4

File tree

1 file changed

+44
-51
lines changed

1 file changed

+44
-51
lines changed

packages/plugin-coverage/src/lib/binding.unit.test.ts

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { vol } from 'memfs';
22
import type { PluginAnswer, PluginSetupTree } from '@code-pushup/models';
33
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
44
import { readJsonFile } from '@code-pushup/utils';
5-
import { coverageSetupBinding } from './binding.js';
5+
import { coverageSetupBinding as binding } from './binding.js';
66

77
vi.mock('@code-pushup/utils', async () => {
88
const actual = await vi.importActual('@code-pushup/utils');
@@ -12,24 +12,15 @@ vi.mock('@code-pushup/utils', async () => {
1212
};
1313
});
1414

15-
function generateConfig(
16-
overrides: Record<string, PluginAnswer> = {},
17-
tree?: PluginSetupTree,
18-
) {
19-
return coverageSetupBinding.generateConfig(
20-
{
21-
'coverage.framework': 'vitest',
22-
'coverage.configFile': '',
23-
'coverage.reportPath': 'coverage/lcov.info',
24-
'coverage.testCommand': 'npx vitest run --coverage.enabled',
25-
'coverage.types': ['function', 'branch', 'line'],
26-
'coverage.continueOnFail': true,
27-
'coverage.categories': true,
28-
...overrides,
29-
},
30-
tree,
31-
);
32-
}
15+
const defaultAnswers: Record<string, PluginAnswer> = {
16+
'coverage.framework': 'vitest',
17+
'coverage.configFile': '',
18+
'coverage.reportPath': 'coverage/lcov.info',
19+
'coverage.testCommand': 'npx vitest run --coverage.enabled',
20+
'coverage.types': ['function', 'branch', 'line'],
21+
'coverage.continueOnFail': true,
22+
'coverage.categories': true,
23+
};
3324

3425
function createMockTree(
3526
files: Record<string, string> = {},
@@ -58,9 +49,7 @@ describe('coverageSetupBinding', () => {
5849
])('should detect %s', async file => {
5950
vol.fromJSON({ [file]: '' }, MEMFS_VOLUME);
6051

61-
await expect(
62-
coverageSetupBinding.isRecommended(MEMFS_VOLUME),
63-
).resolves.toBeTrue();
52+
await expect(binding.isRecommended(MEMFS_VOLUME)).resolves.toBeTrue();
6453
});
6554

6655
it.each(['dependencies', 'devDependencies'])(
@@ -70,9 +59,7 @@ describe('coverageSetupBinding', () => {
7059
[field]: { vitest: '^2.0.0' },
7160
});
7261

73-
await expect(
74-
coverageSetupBinding.isRecommended(MEMFS_VOLUME),
75-
).resolves.toBeTrue();
62+
await expect(binding.isRecommended(MEMFS_VOLUME)).resolves.toBeTrue();
7663
},
7764
);
7865

@@ -83,18 +70,14 @@ describe('coverageSetupBinding', () => {
8370
[field]: { jest: '^29.0.0' },
8471
});
8572

86-
await expect(
87-
coverageSetupBinding.isRecommended(MEMFS_VOLUME),
88-
).resolves.toBeTrue();
73+
await expect(binding.isRecommended(MEMFS_VOLUME)).resolves.toBeTrue();
8974
},
9075
);
9176

9277
it('should not recommend when no test framework found', async () => {
9378
vi.mocked(readJsonFile).mockResolvedValue({});
9479

95-
await expect(
96-
coverageSetupBinding.isRecommended(MEMFS_VOLUME),
97-
).resolves.toBeFalse();
80+
await expect(binding.isRecommended(MEMFS_VOLUME)).resolves.toBeFalse();
9881
});
9982
});
10083

@@ -103,7 +86,7 @@ describe('coverageSetupBinding', () => {
10386
vol.fromJSON({ 'vitest.config.ts': '' }, MEMFS_VOLUME);
10487

10588
await expect(
106-
coverageSetupBinding.prompts(MEMFS_VOLUME),
89+
binding.prompts(MEMFS_VOLUME),
10790
).resolves.toIncludeAllPartialMembers([
10891
{ key: 'coverage.framework', default: 'vitest' },
10992
{ key: 'coverage.configFile', default: 'vitest.config.ts' },
@@ -115,7 +98,7 @@ describe('coverageSetupBinding', () => {
11598
vol.fromJSON({ 'jest.config.js': '' }, MEMFS_VOLUME);
11699

117100
await expect(
118-
coverageSetupBinding.prompts(MEMFS_VOLUME),
101+
binding.prompts(MEMFS_VOLUME),
119102
).resolves.toIncludeAllPartialMembers([
120103
{ key: 'coverage.framework', default: 'jest' },
121104
{ key: 'coverage.configFile', default: 'jest.config.js' },
@@ -126,7 +109,7 @@ describe('coverageSetupBinding', () => {
126109
vi.mocked(readJsonFile).mockResolvedValue({});
127110

128111
await expect(
129-
coverageSetupBinding.prompts(MEMFS_VOLUME),
112+
binding.prompts(MEMFS_VOLUME),
130113
).resolves.toIncludeAllPartialMembers([
131114
{ key: 'coverage.framework', default: 'other' },
132115
{ key: 'coverage.reportPath', default: '' },
@@ -137,7 +120,7 @@ describe('coverageSetupBinding', () => {
137120

138121
describe('generateConfig', () => {
139122
it('should generate vitest config', async () => {
140-
const { pluginInit } = await generateConfig();
123+
const { pluginInit } = await binding.generateConfig(defaultAnswers);
141124
expect(pluginInit).toMatchInlineSnapshot(`
142125
"// NOTE: Ensure your test config includes "lcov" in coverage reporters.
143126
await coveragePlugin({
@@ -148,7 +131,8 @@ describe('coverageSetupBinding', () => {
148131
});
149132

150133
it('should generate jest config', async () => {
151-
const { pluginInit } = await generateConfig({
134+
const { pluginInit } = await binding.generateConfig({
135+
...defaultAnswers,
152136
'coverage.framework': 'jest',
153137
'coverage.testCommand': 'npx jest --coverage',
154138
});
@@ -162,59 +146,65 @@ describe('coverageSetupBinding', () => {
162146
});
163147

164148
it('should omit coverageToolCommand when test command is empty', async () => {
165-
const { pluginInit } = await generateConfig({
149+
const { pluginInit } = await binding.generateConfig({
150+
...defaultAnswers,
166151
'coverage.testCommand': '',
167152
});
168153
expect(pluginInit).not.toContain('coverageToolCommand');
169154
});
170155

171156
it('should use default report path when empty', async () => {
172-
const { pluginInit } = await generateConfig({
157+
const { pluginInit } = await binding.generateConfig({
158+
...defaultAnswers,
173159
'coverage.reportPath': '',
174160
});
175161
expect(pluginInit).toContain("'coverage/lcov.info'");
176162
});
177163

178164
it('should use custom report path when provided', async () => {
179-
const { pluginInit } = await generateConfig({
165+
const { pluginInit } = await binding.generateConfig({
166+
...defaultAnswers,
180167
'coverage.reportPath': 'dist/coverage/lcov.info',
181168
});
182169
expect(pluginInit).toContain("'dist/coverage/lcov.info'");
183170
});
184171

185172
it('should omit coverageTypes when all selected', async () => {
186-
const { pluginInit } = await generateConfig();
173+
const { pluginInit } = await binding.generateConfig(defaultAnswers);
187174
expect(pluginInit).not.toContain('coverageTypes');
188175
});
189176

190177
it('should include coverageTypes when subset selected', async () => {
191-
const { pluginInit } = await generateConfig({
178+
const { pluginInit } = await binding.generateConfig({
179+
...defaultAnswers,
192180
'coverage.types': ['branch', 'line'],
193181
});
194182
expect(pluginInit).toContain("coverageTypes: ['branch', 'line']");
195183
});
196184

197185
it('should disable continueOnCommandFail when declined', async () => {
198-
const { pluginInit } = await generateConfig({
186+
const { pluginInit } = await binding.generateConfig({
187+
...defaultAnswers,
199188
'coverage.continueOnFail': false,
200189
});
201190
expect(pluginInit).toContain('continueOnCommandFail: false');
202191
});
203192

204193
it('should omit continueOnCommandFail when default', async () => {
205-
const { pluginInit } = await generateConfig();
194+
const { pluginInit } = await binding.generateConfig(defaultAnswers);
206195
expect(pluginInit).not.toContain('continueOnCommandFail');
207196
});
208197

209198
it('should omit categories when declined', async () => {
210-
const { categories } = await generateConfig({
199+
const { categories } = await binding.generateConfig({
200+
...defaultAnswers,
211201
'coverage.categories': false,
212202
});
213203
expect(categories).toBeUndefined();
214204
});
215205

216206
it('should import from @code-pushup/coverage-plugin', async () => {
217-
const { imports } = await generateConfig();
207+
const { imports } = await binding.generateConfig(defaultAnswers);
218208
expect(imports).toEqual([
219209
{
220210
moduleSpecifier: '@code-pushup/coverage-plugin',
@@ -225,7 +215,8 @@ describe('coverageSetupBinding', () => {
225215
});
226216

227217
describe('lcov reporter configuration', () => {
228-
const VITEST_ANSWERS = {
218+
const vitestAnswers = {
219+
...defaultAnswers,
229220
'coverage.framework': 'vitest',
230221
'coverage.configFile': 'vitest.config.ts',
231222
} as const;
@@ -235,7 +226,7 @@ describe('coverageSetupBinding', () => {
235226
'vitest.config.ts':
236227
"export default { test: { coverage: { reporter: ['lcov'] } } };",
237228
});
238-
const { pluginInit } = await generateConfig(VITEST_ANSWERS, tree);
229+
const { pluginInit } = await binding.generateConfig(vitestAnswers, tree);
239230
expect(pluginInit).not.toContain('NOTE');
240231
});
241232

@@ -244,30 +235,32 @@ describe('coverageSetupBinding', () => {
244235
'vitest.config.ts':
245236
"import { defineConfig } from 'vitest/config';\nexport default defineConfig({ test: { coverage: { reporter: ['text'] } } });",
246237
});
247-
const { pluginInit } = await generateConfig(VITEST_ANSWERS, tree);
238+
const { pluginInit } = await binding.generateConfig(vitestAnswers, tree);
248239
expect(pluginInit).not.toContain('NOTE');
249240
expect(tree.written.get('vitest.config.ts')).toContain('lcov');
250241
});
251242

252243
it('should include comment when framework is other', async () => {
253-
const { pluginInit } = await generateConfig({
244+
const { pluginInit } = await binding.generateConfig({
245+
...defaultAnswers,
254246
'coverage.framework': 'other',
255247
});
256248
expect(pluginInit).toContain('NOTE');
257249
});
258250

259251
it('should include comment when config file cannot be read', async () => {
260252
const tree = createMockTree({});
261-
const { pluginInit } = await generateConfig(VITEST_ANSWERS, tree);
253+
const { pluginInit } = await binding.generateConfig(vitestAnswers, tree);
262254
expect(pluginInit).toContain('NOTE');
263255
});
264256

265257
it('should include comment when magicast cannot modify the file', async () => {
266258
const tree = createMockTree({
267259
'jest.config.js': "module.exports = { coverageReporters: ['text'] };",
268260
});
269-
const { pluginInit } = await generateConfig(
261+
const { pluginInit } = await binding.generateConfig(
270262
{
263+
...defaultAnswers,
271264
'coverage.framework': 'jest',
272265
'coverage.configFile': 'jest.config.js',
273266
},

0 commit comments

Comments
 (0)