|
4 | 4 | addSentryImport, |
5 | 5 | arrayToObjectShorthand, |
6 | 6 | makeAutoInstrumentMiddlewarePlugin, |
| 7 | + shouldSkipFile, |
7 | 8 | wrapGlobalMiddleware, |
8 | 9 | wrapRouteMiddleware, |
9 | 10 | wrapServerFnMiddleware, |
@@ -90,6 +91,14 @@ createStart(() => ({ requestMiddleware: [getMiddleware()] })); |
90 | 91 |
|
91 | 92 | consoleWarnSpy.mockRestore(); |
92 | 93 | }); |
| 94 | + |
| 95 | + it('does not instrument files matching exclude patterns', () => { |
| 96 | + const plugin = makeAutoInstrumentMiddlewarePlugin({ |
| 97 | + exclude: ['/routes/admin/'], |
| 98 | + }) as PluginWithTransform; |
| 99 | + const result = plugin.transform(createStartFile, '/app/routes/admin/start.ts'); |
| 100 | + expect(result).toBeNull(); |
| 101 | + }); |
93 | 102 | }); |
94 | 103 |
|
95 | 104 | describe('wrapGlobalMiddleware', () => { |
@@ -438,93 +447,33 @@ describe('addSentryImport', () => { |
438 | 447 | }); |
439 | 448 | }); |
440 | 449 |
|
441 | | -describe('exclude option', () => { |
442 | | - const createStartFile = ` |
443 | | -import { createStart } from '@tanstack/react-start'; |
444 | | -createStart(() => ({ requestMiddleware: [authMiddleware] })); |
445 | | -`; |
446 | | - |
447 | | - it('excludes files matching string pattern (substring match)', () => { |
448 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
449 | | - exclude: ['/routes/admin/'], |
450 | | - }) as PluginWithTransform; |
451 | | - |
452 | | - const result = plugin.transform(createStartFile, '/app/routes/admin/start.ts'); |
453 | | - |
454 | | - expect(result).toBeNull(); |
| 450 | +describe('shouldSkipFile', () => { |
| 451 | + it('returns false when exclude is undefined', () => { |
| 452 | + expect(shouldSkipFile('/app/start.ts', undefined, false)).toBe(false); |
455 | 453 | }); |
456 | 454 |
|
457 | | - it('excludes files matching regex pattern', () => { |
458 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
459 | | - exclude: [/\.test\.ts$/], |
460 | | - }) as PluginWithTransform; |
461 | | - |
462 | | - const result = plugin.transform(createStartFile, '/app/start.test.ts'); |
463 | | - |
464 | | - expect(result).toBeNull(); |
| 455 | + it('returns false when exclude is empty array', () => { |
| 456 | + expect(shouldSkipFile('/app/start.ts', [], false)).toBe(false); |
465 | 457 | }); |
466 | 458 |
|
467 | | - it('excludes files matching any of multiple patterns', () => { |
468 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
469 | | - exclude: ['/routes/admin/', /\.test\.ts$/], |
470 | | - }) as PluginWithTransform; |
471 | | - |
472 | | - // String pattern match |
473 | | - const result1 = plugin.transform(createStartFile, '/app/routes/admin/start.ts'); |
474 | | - expect(result1).toBeNull(); |
475 | | - |
476 | | - // Regex pattern match |
477 | | - const result2 = plugin.transform(createStartFile, '/app/start.test.ts'); |
478 | | - expect(result2).toBeNull(); |
| 459 | + it('returns false when file does not match any pattern', () => { |
| 460 | + expect(shouldSkipFile('/app/start.ts', ['/admin/', /\.test\.ts$/], false)).toBe(false); |
479 | 461 | }); |
480 | 462 |
|
481 | | - it('instruments files not matching exclude patterns', () => { |
482 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
483 | | - exclude: ['/routes/admin/', /\.test\.ts$/], |
484 | | - }) as PluginWithTransform; |
485 | | - |
486 | | - const result = plugin.transform(createStartFile, '/app/start.ts'); |
487 | | - |
488 | | - expect(result).not.toBeNull(); |
489 | | - expect(result!.code).toContain('wrapMiddlewaresWithSentry'); |
| 463 | + it('returns true when file matches string pattern', () => { |
| 464 | + expect(shouldSkipFile('/app/routes/admin/start.ts', ['/admin/'], false)).toBe(true); |
490 | 465 | }); |
491 | 466 |
|
492 | | - it('instruments all files when exclude is empty array', () => { |
493 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
494 | | - exclude: [], |
495 | | - }) as PluginWithTransform; |
496 | | - |
497 | | - const result = plugin.transform(createStartFile, '/app/start.ts'); |
498 | | - |
499 | | - expect(result).not.toBeNull(); |
500 | | - expect(result!.code).toContain('wrapMiddlewaresWithSentry'); |
| 467 | + it('returns true when file matches regex pattern', () => { |
| 468 | + expect(shouldSkipFile('/app/start.test.ts', [/\.test\.ts$/], false)).toBe(true); |
501 | 469 | }); |
502 | 470 |
|
503 | | - it('instruments all files when exclude is undefined', () => { |
504 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
505 | | - exclude: undefined, |
506 | | - }) as PluginWithTransform; |
507 | | - |
508 | | - const result = plugin.transform(createStartFile, '/app/start.ts'); |
509 | | - |
510 | | - expect(result).not.toBeNull(); |
511 | | - expect(result!.code).toContain('wrapMiddlewaresWithSentry'); |
512 | | - }); |
513 | | - |
514 | | - it('logs debug message when file is excluded', () => { |
| 471 | + it('logs debug message when skipping file', () => { |
515 | 472 | const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
516 | | - |
517 | | - const plugin = makeAutoInstrumentMiddlewarePlugin({ |
518 | | - debug: true, |
519 | | - exclude: ['/routes/admin/'], |
520 | | - }) as PluginWithTransform; |
521 | | - |
522 | | - plugin.transform(createStartFile, '/app/routes/admin/start.ts'); |
523 | | - |
| 473 | + shouldSkipFile('/app/routes/admin/start.ts', ['/admin/'], true); |
524 | 474 | expect(consoleLogSpy).toHaveBeenCalledWith( |
525 | 475 | expect.stringContaining('Skipping auto-instrumentation for excluded file'), |
526 | 476 | ); |
527 | | - |
528 | 477 | consoleLogSpy.mockRestore(); |
529 | 478 | }); |
530 | 479 | }); |
|
0 commit comments