|
1 | 1 | import type { Plugin } from 'vite'; |
2 | 2 | import { describe, expect, it, vi } from 'vitest'; |
3 | 3 | import { |
| 4 | + addSentryImport, |
4 | 5 | arrayToObjectShorthand, |
5 | 6 | makeAutoInstrumentMiddlewarePlugin, |
6 | 7 | wrapGlobalMiddleware, |
@@ -61,30 +62,6 @@ createStart(() => ({ requestMiddleware: wrapMiddlewaresWithSentry({ myMiddleware |
61 | 62 | expect(result).toBeNull(); |
62 | 63 | }); |
63 | 64 |
|
64 | | - it('handles files with use server directive', () => { |
65 | | - const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform; |
66 | | - const code = `'use server'; |
67 | | -import { createStart } from '@tanstack/react-start'; |
68 | | -createStart(() => ({ requestMiddleware: [authMiddleware] })); |
69 | | -`; |
70 | | - const result = plugin.transform(code, '/app/start.ts'); |
71 | | - |
72 | | - expect(result).not.toBeNull(); |
73 | | - expect(result!.code).toMatch(/^'use server';\s*\nimport \{ wrapMiddlewaresWithSentry \}/); |
74 | | - }); |
75 | | - |
76 | | - it('handles files with use client directive', () => { |
77 | | - const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform; |
78 | | - const code = `"use client"; |
79 | | -import { createStart } from '@tanstack/react-start'; |
80 | | -createStart(() => ({ requestMiddleware: [authMiddleware] })); |
81 | | -`; |
82 | | - const result = plugin.transform(code, '/app/start.ts'); |
83 | | - |
84 | | - expect(result).not.toBeNull(); |
85 | | - expect(result!.code).toMatch(/^"use client";\s*\nimport \{ wrapMiddlewaresWithSentry \}/); |
86 | | - }); |
87 | | - |
88 | 65 | it('adds import statement when wrapping middlewares', () => { |
89 | 66 | const plugin = makeAutoInstrumentMiddlewarePlugin() as PluginWithTransform; |
90 | 67 | const result = plugin.transform(createStartFile, '/app/start.ts'); |
@@ -352,6 +329,33 @@ export const Route = createFileRoute('/foo')({ |
352 | 329 | }); |
353 | 330 | }); |
354 | 331 |
|
| 332 | +describe('addSentryImport', () => { |
| 333 | + it('prepends import to code without directives', () => { |
| 334 | + const code = 'const foo = 1;'; |
| 335 | + const result = addSentryImport(code); |
| 336 | + |
| 337 | + expect(result).toBe( |
| 338 | + "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\nconst foo = 1;", |
| 339 | + ); |
| 340 | + }); |
| 341 | + |
| 342 | + it('inserts import after use server directive', () => { |
| 343 | + const code = "'use server';\nconst foo = 1;"; |
| 344 | + const result = addSentryImport(code); |
| 345 | + |
| 346 | + expect(result).toMatch(/^'use server';\nimport \{ wrapMiddlewaresWithSentry \}/); |
| 347 | + expect(result).toContain('const foo = 1;'); |
| 348 | + }); |
| 349 | + |
| 350 | + it('inserts import after use client directive', () => { |
| 351 | + const code = '"use client";\nconst foo = 1;'; |
| 352 | + const result = addSentryImport(code); |
| 353 | + |
| 354 | + expect(result).toMatch(/^"use client";\nimport \{ wrapMiddlewaresWithSentry \}/); |
| 355 | + expect(result).toContain('const foo = 1;'); |
| 356 | + }); |
| 357 | +}); |
| 358 | + |
355 | 359 | describe('arrayToObjectShorthand', () => { |
356 | 360 | it('converts single identifier', () => { |
357 | 361 | expect(arrayToObjectShorthand('foo')).toBe('{ foo }'); |
|
0 commit comments