|
1 | 1 | import * as fs from 'node:fs'; |
2 | 2 | import * as path from 'node:path'; |
3 | 3 |
|
4 | | -import { afterEach, describe, expect, it } from 'vitest'; |
| 4 | +import { once } from 'storybook/internal/node-logger'; |
| 5 | + |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
5 | 7 |
|
6 | 8 | import { dedent } from 'ts-dedent'; |
7 | 9 | import ts from 'typescript'; |
@@ -340,6 +342,140 @@ describe('multi-project management', () => { |
340 | 342 | expect(project).toBeDefined(); |
341 | 343 | }); |
342 | 344 |
|
| 345 | + it( |
| 346 | + 'recycles the shared program when heap usage crosses the threshold', |
| 347 | + { timeout: 30_000 }, |
| 348 | + () => { |
| 349 | + tempDir = createTempDir(); |
| 350 | + |
| 351 | + const files = writeFiles(tempDir, { |
| 352 | + 'tsconfig.json': tsconfigJSON(), |
| 353 | + 'Button.tsx': dedent` |
| 354 | + import React from 'react'; |
| 355 | + export const Button = (_props: { label: string }) => <button />; |
| 356 | + `, |
| 357 | + 'Button.stories.tsx': dedent` |
| 358 | + import { Button } from './Button'; |
| 359 | + export default { component: Button }; |
| 360 | + `, |
| 361 | + }); |
| 362 | + |
| 363 | + // ratio 0 → threshold 0 → heapUsed is always ≥ 0 → recycle fires after every batchExtract. |
| 364 | + manager = new ComponentMetaManager(ts, 0); |
| 365 | + const componentPath = path.join(tempDir, 'Button.tsx'); |
| 366 | + const before = manager.getProjectForFile(componentPath); |
| 367 | + |
| 368 | + const entries: StoryRef[] = [ |
| 369 | + { |
| 370 | + storyPath: files['Button.stories.tsx'], |
| 371 | + component: { |
| 372 | + componentName: 'Button', |
| 373 | + importName: 'Button', |
| 374 | + path: componentPath, |
| 375 | + isPackage: false, |
| 376 | + }, |
| 377 | + }, |
| 378 | + ]; |
| 379 | + manager.batchExtract(entries); |
| 380 | + |
| 381 | + // The disposed program was cleared, so the next lookup rebuilds a fresh instance. |
| 382 | + const after = manager.getProjectForFile(componentPath); |
| 383 | + expect(after).not.toBe(before); |
| 384 | + } |
| 385 | + ); |
| 386 | + |
| 387 | + it( |
| 388 | + 'warns once, with guidance to raise the memory limit, when it recycles under pressure', |
| 389 | + { timeout: 30_000 }, |
| 390 | + () => { |
| 391 | + tempDir = createTempDir(); |
| 392 | + |
| 393 | + const files = writeFiles(tempDir, { |
| 394 | + 'tsconfig.json': tsconfigJSON(), |
| 395 | + 'Button.tsx': dedent` |
| 396 | + import React from 'react'; |
| 397 | + export const Button = (_props: { label: string }) => <button />; |
| 398 | + `, |
| 399 | + 'Button.stories.tsx': dedent` |
| 400 | + import { Button } from './Button'; |
| 401 | + export default { component: Button }; |
| 402 | + `, |
| 403 | + }); |
| 404 | + |
| 405 | + // Spy on the one-time `once.warn` channel: routing the warning through it is what makes it |
| 406 | + // surface once per process (the dedupe itself is node-logger's responsibility, tested there). |
| 407 | + const onceWarnSpy = vi.spyOn(once, 'warn').mockImplementation(() => undefined); |
| 408 | + try { |
| 409 | + // ratio 0 → threshold 0 → recycle fires on every batchExtract. |
| 410 | + manager = new ComponentMetaManager(ts, 0); |
| 411 | + const componentPath = path.join(tempDir, 'Button.tsx'); |
| 412 | + manager.getProjectForFile(componentPath); |
| 413 | + |
| 414 | + const entries: StoryRef[] = [ |
| 415 | + { |
| 416 | + storyPath: files['Button.stories.tsx'], |
| 417 | + component: { |
| 418 | + componentName: 'Button', |
| 419 | + importName: 'Button', |
| 420 | + path: componentPath, |
| 421 | + isPackage: false, |
| 422 | + }, |
| 423 | + }, |
| 424 | + ]; |
| 425 | + |
| 426 | + manager.batchExtract(entries); |
| 427 | + |
| 428 | + expect(onceWarnSpy).toHaveBeenCalled(); |
| 429 | + expect(onceWarnSpy.mock.calls[0][0]).toContain('--max-old-space-size'); |
| 430 | + } finally { |
| 431 | + onceWarnSpy.mockRestore(); |
| 432 | + once.clear(); |
| 433 | + } |
| 434 | + } |
| 435 | + ); |
| 436 | + |
| 437 | + it( |
| 438 | + 'keeps the shared program while heap usage stays below the threshold', |
| 439 | + { timeout: 30_000 }, |
| 440 | + () => { |
| 441 | + tempDir = createTempDir(); |
| 442 | + |
| 443 | + const files = writeFiles(tempDir, { |
| 444 | + 'tsconfig.json': tsconfigJSON(), |
| 445 | + 'Button.tsx': dedent` |
| 446 | + import React from 'react'; |
| 447 | + export const Button = (_props: { label: string }) => <button />; |
| 448 | + `, |
| 449 | + 'Button.stories.tsx': dedent` |
| 450 | + import { Button } from './Button'; |
| 451 | + export default { component: Button }; |
| 452 | + `, |
| 453 | + }); |
| 454 | + |
| 455 | + // ratio Infinity → threshold Infinity → heapUsed is always below it → recycle never fires. |
| 456 | + manager = new ComponentMetaManager(ts, Number.POSITIVE_INFINITY); |
| 457 | + const componentPath = path.join(tempDir, 'Button.tsx'); |
| 458 | + const before = manager.getProjectForFile(componentPath); |
| 459 | + |
| 460 | + const entries: StoryRef[] = [ |
| 461 | + { |
| 462 | + storyPath: files['Button.stories.tsx'], |
| 463 | + component: { |
| 464 | + componentName: 'Button', |
| 465 | + importName: 'Button', |
| 466 | + path: componentPath, |
| 467 | + isPackage: false, |
| 468 | + }, |
| 469 | + }, |
| 470 | + ]; |
| 471 | + manager.batchExtract(entries); |
| 472 | + |
| 473 | + // No recycle → the same program instance is reused across extractions. |
| 474 | + const after = manager.getProjectForFile(componentPath); |
| 475 | + expect(after).toBe(before); |
| 476 | + } |
| 477 | + ); |
| 478 | + |
343 | 479 | it('extracts via Path 1 (importId + JSX in story file)', { timeout: 30_000 }, () => { |
344 | 480 | tempDir = createTempDir(); |
345 | 481 |
|
|
0 commit comments