-
-
Notifications
You must be signed in to change notification settings - Fork 10k
Expand file tree
/
Copy pathAddonVitestService.test.ts
More file actions
708 lines (557 loc) · 27 KB
/
AddonVitestService.test.ts
File metadata and controls
708 lines (557 loc) · 27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
import * as fs from 'node:fs/promises';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { JsPackageManager } from 'storybook/internal/common';
import { getProjectRoot } from 'storybook/internal/common';
import { logger, prompt } from 'storybook/internal/node-logger';
import * as find from 'empathic/find';
// eslint-disable-next-line depend/ban-dependencies
import type { ResultPromise } from 'execa';
import { SupportedBuilder, SupportedFramework } from '../types';
import { AddonVitestService } from './AddonVitestService';
vi.mock('node:fs/promises', { spy: true });
vi.mock('storybook/internal/common', { spy: true });
vi.mock('storybook/internal/node-logger', { spy: true });
vi.mock('empathic/find', { spy: true });
describe('AddonVitestService', () => {
let service: AddonVitestService;
let mockPackageManager: JsPackageManager;
beforeEach(() => {
vi.clearAllMocks();
mockPackageManager = {
getAllDependencies: vi.fn(),
getInstalledVersion: vi.fn(),
runPackageCommand: vi.fn(),
getPackageCommand: vi.fn(),
} as Partial<JsPackageManager> as JsPackageManager;
service = new AddonVitestService(mockPackageManager);
vi.mocked(getProjectRoot).mockReturnValue('/test/project');
// Setup default mocks for logger and prompt
vi.mocked(logger.info).mockImplementation(() => {});
vi.mocked(logger.log).mockImplementation(() => {});
vi.mocked(logger.warn).mockImplementation(() => {});
vi.mocked(prompt.executeTask).mockResolvedValue(undefined);
vi.mocked(prompt.executeTaskWithSpinner).mockResolvedValue(undefined);
vi.mocked(prompt.confirm).mockResolvedValue(true);
});
describe('collectDeps', () => {
beforeEach(() => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion).mockResolvedValue(null);
});
it('should collect base packages when not installed', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // vitest version check
.mockResolvedValueOnce(null) // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
expect(deps).toContain('vitest');
// When vitest version is null, defaults to vitest 4+ behavior
expect(deps).toContain('@vitest/browser-playwright');
expect(deps).toContain('playwright');
expect(deps).toContain('@vitest/coverage-v8');
});
it('should not include base packages if already installed', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({
vitest: '3.0.0',
'@vitest/browser': '3.0.0',
playwright: '1.0.0',
});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest version
.mockResolvedValueOnce('3.0.0') // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
expect(deps).not.toContain('vitest');
expect(deps).not.toContain('@vitest/browser');
expect(deps).not.toContain('playwright');
});
// Note: collectDependencies doesn't add framework-specific packages
// It only collects base vitest packages
it('should collect base packages without framework-specific additions', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // vitest version check
.mockResolvedValueOnce(null) // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
// Should only contain base packages, not framework-specific ones
expect(deps).toContain('vitest');
// When vitest version is null, defaults to vitest 4+ behavior
expect(deps).toContain('@vitest/browser-playwright');
expect(deps).toContain('playwright');
expect(deps).toContain('@vitest/coverage-v8');
expect(deps.every((d) => !d.includes('nextjs-vite'))).toBe(true);
});
it('should not add @storybook/nextjs-vite for non-Next.js frameworks', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // vitest version
.mockResolvedValueOnce(null) // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
expect(deps.every((d) => !d.includes('nextjs-vite'))).toBe(true);
});
it('should not add coverage reporter if v8 already installed', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // vitest version
.mockResolvedValueOnce('3.0.0') // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
expect(deps.every((d) => !d.includes('coverage'))).toBe(true);
});
it('skips coverage if istanbul', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // @vitest/coverage-v8
.mockResolvedValueOnce('3.0.0') // @vitest/coverage-istanbul
.mockResolvedValueOnce(null); // vitest version
const deps = await service.collectDependencies();
expect(deps.every((d) => !d.includes('coverage'))).toBe(true);
});
it('applies version', async () => {
vi.mocked(mockPackageManager.getAllDependencies).mockReturnValue({});
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.2.0') // vitest version check
.mockResolvedValueOnce(null) // @vitest/coverage-v8
.mockResolvedValueOnce(null); // @vitest/coverage-istanbul
const deps = await service.collectDependencies();
expect(deps).toContain('vitest@3.2.0');
// Version 3.2.0 < 4.0.0, so uses @vitest/browser
expect(deps).toContain('@vitest/browser@3.2.0');
expect(deps).toContain('@vitest/coverage-v8@3.2.0');
expect(deps).toContain('playwright'); // no version for playwright
});
});
describe('validatePackageVersions', () => {
it('should return compatible when vitest >=3.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
expect(result.reasons).toBeUndefined();
});
it('should return compatible when vitest prerelease >= 3.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0-beta.1') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
expect(result.reasons).toBeUndefined();
});
it('should return compatible when vitest canary is used', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('0.0.0-833c515fa25cef20905a7f9affb156dfa6f151ab') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
expect(result.reasons).toBeUndefined();
});
it('should return compatible when vitest >=4.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('4.0.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
expect(result.reasons).toBeUndefined();
});
it('should return incompatible when vitest <3.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('2.5.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.some((r) => r.includes('Vitest 3.0.0 or higher'))).toBe(true);
});
it('should return compatible when msw >=2.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce('2.0.0'); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
});
it('should return incompatible when msw <2.0.0', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce('1.9.0'); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.some((r) => r.includes('MSW'))).toBe(true);
});
it('should return compatible when msw not installed', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
});
it('should return compatible when vitest is not installed', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce(null) // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(true);
});
it('should handle multiple validation failures', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('2.0.0') // vitest <3.0.0
.mockResolvedValueOnce('1.0.0'); // msw <2.0.0
const result = await service.validatePackageVersions();
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.length).toBe(2);
});
});
describe('validateCompatibility', () => {
beforeEach(() => {
vi.mocked(mockPackageManager.getInstalledVersion).mockResolvedValue('3.0.0');
vi.mocked(find.any).mockReturnValue(undefined);
});
it('should return compatible for valid Vite-based framework', async () => {
const result = await service.validateCompatibility({
framework: SupportedFramework.REACT_VITE,
builder: SupportedBuilder.VITE,
});
expect(result.compatible).toBe(true);
});
it('should return compatible for react-vite with Vite builder', async () => {
const result = await service.validateCompatibility({
framework: SupportedFramework.REACT_VITE,
builder: SupportedBuilder.VITE,
});
expect(result.compatible).toBe(true);
});
it('should return incompatible for non-Vite builder (except Next.js)', async () => {
const result = await service.validateCompatibility({
framework: SupportedFramework.REACT_WEBPACK5,
builder: SupportedBuilder.WEBPACK5,
});
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('Non-Vite builder'))).toBe(true);
});
it('should return incompatible for Next.js with webpack builder', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validateCompatibility({
framework: SupportedFramework.NEXTJS,
builder: SupportedBuilder.WEBPACK5,
});
// Test addon requires Vite builder, even for Next.js
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('Non-Vite builder'))).toBe(true);
});
it('should return incompatible for unsupported framework', async () => {
const result = await service.validateCompatibility({
framework: SupportedFramework.ANGULAR,
builder: SupportedBuilder.VITE,
});
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('cannot yet be used'))).toBe(true);
});
// Note: validateCompatibility currently doesn't validate Next.js installation
// It only validates builder, framework support, package versions, and config files
it('should return compatible for Next.js framework with valid setup', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('3.0.0') // vitest
.mockResolvedValueOnce(null); // msw
const result = await service.validateCompatibility({
framework: SupportedFramework.NEXTJS_VITE,
builder: SupportedBuilder.VITE,
});
// NEXTJS_VITE framework is in SUPPORTED_FRAMEWORKS and Vite builder is compatible
expect(result.compatible).toBe(true);
});
it('should validate config files when configDir provided', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.json');
const result = await service.validateCompatibility({
framework: SupportedFramework.REACT_VITE,
builder: SupportedBuilder.VITE,
projectRoot: '.storybook',
});
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('JSON workspace'))).toBe(true);
});
it('should skip config file validation when no configDir provided', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.json');
const result = await service.validateCompatibility({
framework: SupportedFramework.REACT_VITE,
builder: SupportedBuilder.VITE,
});
expect(result.compatible).toBe(true);
expect(find.any).not.toHaveBeenCalled();
});
it('should accumulate multiple validation failures', async () => {
vi.mocked(mockPackageManager.getInstalledVersion)
.mockResolvedValueOnce('2.0.0') // vitest <3.0.0
.mockResolvedValueOnce('1.0.0'); // msw <2.0.0
const result = await service.validateCompatibility({
framework: SupportedFramework.ANGULAR,
builder: SupportedBuilder.WEBPACK5,
});
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.length).toBeGreaterThan(2);
});
});
describe('installPlaywright', () => {
beforeEach(() => {
// Mock the logger methods used in installPlaywright
vi.mocked(logger.log).mockImplementation(() => {});
vi.mocked(logger.warn).mockImplementation(() => {});
// Mock getPackageCommand to return a string
vi.mocked(mockPackageManager.getPackageCommand).mockReturnValue(
'npx playwright install chromium'
);
});
it('should install Playwright successfully', async () => {
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockResolvedValue(undefined);
const { errors } = await service.installPlaywright();
expect(errors).toEqual([]);
expect(prompt.confirm).toHaveBeenCalledWith({
message: 'Do you want to install Playwright with Chromium now?',
initialValue: true,
});
expect(prompt.executeTaskWithSpinner).toHaveBeenCalledWith(expect.any(Function), {
id: 'playwright-installation',
intro: 'Installing Playwright browser binaries (press "c" to abort)',
error: expect.stringContaining('An error occurred'),
success: 'Playwright browser binaries installed successfully',
abortable: true,
});
});
it('should execute playwright install command', async () => {
const originalCI = process.env.CI;
delete process.env.CI;
try {
type ChildProcessFactory = (signal?: AbortSignal) => ResultPromise;
let commandFactory: ChildProcessFactory | ChildProcessFactory[];
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockImplementation(
async (factory: ChildProcessFactory | ChildProcessFactory[]) => {
commandFactory = Array.isArray(factory) ? factory[0] : factory;
// Simulate the child process completion
commandFactory();
}
);
await service.installPlaywright();
expect(mockPackageManager.runPackageCommand).toHaveBeenCalledWith({
args: ['playwright', 'install', 'chromium'],
signal: undefined,
stdio: ['inherit', 'pipe', 'pipe'],
});
} finally {
if (originalCI !== undefined) {
process.env.CI = originalCI;
}
}
});
it('should execute playwright install command with --with-deps in CI', async () => {
const originalCI = process.env.CI;
process.env.CI = 'true';
try {
type ChildProcessFactory = (signal?: AbortSignal) => ResultPromise;
let commandFactory: ChildProcessFactory | ChildProcessFactory[];
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockImplementation(
async (factory: ChildProcessFactory | ChildProcessFactory[]) => {
commandFactory = Array.isArray(factory) ? factory[0] : factory;
commandFactory();
}
);
await service.installPlaywright();
expect(mockPackageManager.runPackageCommand).toHaveBeenCalledWith({
args: ['playwright', 'install', 'chromium', '--with-deps'],
signal: undefined,
stdio: ['inherit', 'pipe', 'pipe'],
});
} finally {
if (originalCI === undefined) {
delete process.env.CI;
} else {
process.env.CI = originalCI;
}
}
});
it('should capture error stack when installation fails', async () => {
const error = new Error('Installation failed');
error.stack = 'Error stack trace';
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockRejectedValue(error);
const { errors } = await service.installPlaywright();
expect(errors).toEqual(['Error stack trace']);
});
it('should capture error message when installation fails without stack', async () => {
const error = new Error('Installation failed');
error.stack = undefined;
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockRejectedValue(error);
const { errors } = await service.installPlaywright();
expect(errors).toEqual(['Installation failed']);
});
it('should convert non-Error exceptions to string', async () => {
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockRejectedValue('String error');
const { errors } = await service.installPlaywright();
expect(errors).toEqual(['String error']);
});
it('should skip installation when user declines', async () => {
vi.mocked(prompt.confirm).mockResolvedValue(false);
const { errors } = await service.installPlaywright();
expect(errors).toEqual([]);
expect(prompt.executeTaskWithSpinner).not.toHaveBeenCalled();
expect(logger.warn).toHaveBeenCalledWith('Playwright installation skipped');
});
it('should not skip installation by default', async () => {
vi.mocked(prompt.confirm).mockResolvedValue(true);
vi.mocked(prompt.executeTaskWithSpinner).mockResolvedValue(undefined);
await service.installPlaywright();
expect(prompt.confirm).toHaveBeenCalled();
expect(prompt.executeTaskWithSpinner).toHaveBeenCalled();
});
});
describe('validateConfigFiles', () => {
beforeEach(() => {
vi.mocked(find.any).mockReset();
vi.mocked(find.any).mockReturnValue(undefined);
});
it('should return compatible when no config files found', async () => {
vi.mocked(find.any).mockReturnValue(undefined);
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should reject JSON workspace files', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.json');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.some((r) => r.includes('JSON workspace'))).toBe(true);
});
it('should validate non-JSON workspace files', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.ts');
vi.mocked(fs.readFile).mockResolvedValue('export default ["project1", "project2"]');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
expect(fs.readFile).toHaveBeenCalledWith('vitest.workspace.ts', 'utf8');
});
it('should reject invalid workspace config', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.ts');
vi.mocked(fs.readFile).mockResolvedValue('export default "invalid"');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('invalid workspace'))).toBe(true);
});
it('should reject CommonJS config files (.cts)', async () => {
vi.mocked(find.any).mockReset();
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.cts'); // config
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.length).toBeGreaterThan(0);
expect(result.reasons!.some((r) => r.includes('CommonJS config'))).toBe(true);
});
it('should reject CommonJS config files (.cjs)', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.cjs'); // config
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('CommonJS config'))).toBe(true);
});
it('should validate non-CommonJS config files', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue('export default defineConfig({ test: {} })');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should reject invalid vitest config', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue('export default {}');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('invalid Vitest config'))).toBe(true);
});
it('should validate defineWorkspace expression', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.ts');
vi.mocked(fs.readFile).mockResolvedValue('export default defineWorkspace(["project1"])');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should validate workspace config with object expressions', async () => {
vi.mocked(find.any).mockReturnValueOnce('vitest.workspace.ts');
vi.mocked(fs.readFile).mockResolvedValue('export default [{ test: {} }, "project"]');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should validate config with workspace array in test', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue(
'export default defineConfig({ test: { workspace: [] } })'
);
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should accumulate multiple config validation errors', async () => {
vi.mocked(find.any).mockReset();
vi.mocked(find.any)
.mockReturnValueOnce('vitest.workspace.json') // workspace JSON
.mockReturnValueOnce('vitest.config.cjs'); // config CJS
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons).toBeDefined();
expect(result.reasons!.length).toBe(2);
});
it('should validate mergeConfig with plain object literal', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue(
'export default mergeConfig(viteConfig, { test: { name: "node" } })'
);
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should validate mergeConfig with defineConfig call', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue(
'export default mergeConfig(viteConfig, defineConfig({ test: { name: "node" } }))'
);
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should validate mergeConfig with multiple plain objects', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue(
'export default mergeConfig({ test: {} }, { plugins: [] })'
);
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(true);
});
it('should reject mergeConfig with invalid object (non-object argument)', async () => {
vi.mocked(find.any)
.mockReturnValueOnce(undefined) // workspace
.mockReturnValueOnce('vitest.config.ts'); // config
vi.mocked(fs.readFile).mockResolvedValue('export default mergeConfig(viteConfig, "string")');
const result = await service.validateConfigFiles('.storybook');
expect(result.compatible).toBe(false);
expect(result.reasons!.some((r) => r.includes('invalid Vitest config'))).toBe(true);
});
});
});