Skip to content

Commit 07f4f30

Browse files
committed
update tests
1 parent 478e1a2 commit 07f4f30

File tree

2 files changed

+23
-74
lines changed

2 files changed

+23
-74
lines changed

packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function applyWrap(
9191
/**
9292
* Checks if a file should be skipped from auto-instrumentation based on exclude patterns.
9393
*/
94-
function shouldSkipFile(id: string, exclude: Array<string | RegExp> | undefined, debug: boolean): boolean {
94+
export function shouldSkipFile(id: string, exclude: Array<string | RegExp> | undefined, debug: boolean): boolean {
9595
// file doesn't match exclude patterns, don't skip
9696
if (!exclude || exclude.length === 0 || !stringMatchesSomePattern(id, exclude)) {
9797
return false;

packages/tanstackstart-react/test/vite/autoInstrumentMiddleware.test.ts

Lines changed: 22 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
addSentryImport,
55
arrayToObjectShorthand,
66
makeAutoInstrumentMiddlewarePlugin,
7+
shouldSkipFile,
78
wrapGlobalMiddleware,
89
wrapRouteMiddleware,
910
wrapServerFnMiddleware,
@@ -90,6 +91,14 @@ createStart(() => ({ requestMiddleware: [getMiddleware()] }));
9091

9192
consoleWarnSpy.mockRestore();
9293
});
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+
});
93102
});
94103

95104
describe('wrapGlobalMiddleware', () => {
@@ -438,93 +447,33 @@ describe('addSentryImport', () => {
438447
});
439448
});
440449

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);
455453
});
456454

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);
465457
});
466458

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);
479461
});
480462

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);
490465
});
491466

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);
501469
});
502470

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', () => {
515472
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);
524474
expect(consoleLogSpy).toHaveBeenCalledWith(
525475
expect.stringContaining('Skipping auto-instrumentation for excluded file'),
526476
);
527-
528477
consoleLogSpy.mockRestore();
529478
});
530479
});

0 commit comments

Comments
 (0)