|
| 1 | +import type { ConfigEnv, UserConfig } from 'vite'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { sentryRemixVitePlugin } from '../../src/vite'; |
| 4 | + |
| 5 | +// Stub the orchestrion plugin so these stay pure wiring tests (no apm code transformer pulled in), |
| 6 | +// mirroring the real plugin's two shapes. |
| 7 | +const orchestrionConfig = vi.fn((_config: UserConfig, env: ConfigEnv) => |
| 8 | + env.command === 'serve' ? null : { ssr: { noExternal: ['mysql'] } }, |
| 9 | +); |
| 10 | +const orchestrionConfigResolved = vi.fn(); |
| 11 | +const orchestrionTransform = vi.fn(() => ({ code: 'transformed' })); |
| 12 | + |
| 13 | +const orchestrionVite = vi.fn((options?: { buildTimeInstrumentation?: boolean }) => |
| 14 | + options?.buildTimeInstrumentation === false |
| 15 | + ? { name: 'sentry-orchestrion-disabled' } |
| 16 | + : { |
| 17 | + name: 'code-transformer', |
| 18 | + enforce: 'pre', |
| 19 | + config: orchestrionConfig, |
| 20 | + configResolved: orchestrionConfigResolved, |
| 21 | + transform: orchestrionTransform, |
| 22 | + }, |
| 23 | +); |
| 24 | + |
| 25 | +vi.mock('@sentry/server-utils/orchestrion/vite', () => ({ |
| 26 | + sentryOrchestrionPlugin: (options?: { buildTimeInstrumentation?: boolean }) => orchestrionVite(options), |
| 27 | +})); |
| 28 | + |
| 29 | +const NODE_CONFIG = { ssr: { target: 'node' } } as UserConfig; |
| 30 | +const WORKER_CONFIG = { ssr: { target: 'webworker' } } as UserConfig; |
| 31 | +const BUILD_ENV = { command: 'build', mode: 'production' } as ConfigEnv; |
| 32 | +const SERVE_ENV = { command: 'serve', mode: 'development' } as ConfigEnv; |
| 33 | + |
| 34 | +/** Calls a hook declared in either the bare-function or the `{ handler }` form. */ |
| 35 | +function callHook(hook: unknown, ...args: unknown[]): unknown { |
| 36 | + const handler = typeof hook === 'function' ? hook : (hook as { handler: (...a: unknown[]) => unknown }).handler; |
| 37 | + return (handler as (...a: unknown[]) => unknown)(...args); |
| 38 | +} |
| 39 | + |
| 40 | +describe('sentryRemixVitePlugin', () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns the route manifest plugin and the orchestrion plugin', () => { |
| 46 | + const plugins = sentryRemixVitePlugin(); |
| 47 | + |
| 48 | + expect(plugins.map(plugin => plugin.name)).toEqual(['sentry-remix-route-manifest', 'code-transformer']); |
| 49 | + expect(orchestrionVite).toHaveBeenCalledWith({ buildTimeInstrumentation: undefined }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('adds an inert orchestrion plugin when `buildTimeInstrumentation` is `false`', () => { |
| 53 | + const plugins = sentryRemixVitePlugin({ buildTimeInstrumentation: false }); |
| 54 | + |
| 55 | + expect(orchestrionVite).toHaveBeenCalledWith({ buildTimeInstrumentation: false }); |
| 56 | + expect(plugins.map(plugin => plugin.name)).toContain('sentry-orchestrion-disabled'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('keeps the upstream `enforce: "pre"` but defers its `config` hook to the end', () => { |
| 60 | + const orchestrion = sentryRemixVitePlugin()[1] as { enforce?: string; config?: { order?: string } }; |
| 61 | + |
| 62 | + expect(orchestrion.enforce).toBe('pre'); |
| 63 | + expect(orchestrion.config?.order).toBe('post'); |
| 64 | + }); |
| 65 | + |
| 66 | + // The dev server keeps instrumented deps external and lets the runtime `--import` hook inject the |
| 67 | + // channels, which the orchestrion plugin decides from `env.command` — so the wrapper has to pass |
| 68 | + // it through untouched. |
| 69 | + it('forwards the config env so the plugin can opt out in the dev server', () => { |
| 70 | + const orchestrion = sentryRemixVitePlugin()[1]!; |
| 71 | + |
| 72 | + expect(callHook(orchestrion.config, NODE_CONFIG, SERVE_ENV)).toBeNull(); |
| 73 | + expect(orchestrionConfig).toHaveBeenCalledWith(NODE_CONFIG, SERVE_ENV); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('worker targets', () => { |
| 77 | + it('applies the orchestrion hooks for node builds', () => { |
| 78 | + const orchestrion = sentryRemixVitePlugin()[1]!; |
| 79 | + |
| 80 | + expect(callHook(orchestrion.config, NODE_CONFIG, BUILD_ENV)).toEqual({ ssr: { noExternal: ['mysql'] } }); |
| 81 | + |
| 82 | + callHook(orchestrion.configResolved, NODE_CONFIG); |
| 83 | + expect(orchestrionConfigResolved).toHaveBeenCalledTimes(1); |
| 84 | + |
| 85 | + expect(callHook(orchestrion.transform, 'code', 'mysql.js', { ssr: true })).toEqual({ code: 'transformed' }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('skips force-bundling and transforming for webworker builds', () => { |
| 89 | + const orchestrion = sentryRemixVitePlugin()[1]!; |
| 90 | + |
| 91 | + expect(callHook(orchestrion.config, WORKER_CONFIG, BUILD_ENV)).toBeNull(); |
| 92 | + expect(orchestrionConfig).not.toHaveBeenCalled(); |
| 93 | + |
| 94 | + callHook(orchestrion.configResolved, WORKER_CONFIG); |
| 95 | + expect(orchestrionConfigResolved).not.toHaveBeenCalled(); |
| 96 | + |
| 97 | + expect(callHook(orchestrion.transform, 'code', 'mysql.js', { ssr: true })).toBeNull(); |
| 98 | + expect(orchestrionTransform).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + // Framework plugins can set `ssr.target` after our `config` hook ran, so `configResolved` is |
| 102 | + // what actually has to keep the transform out of a worker bundle. |
| 103 | + it('skips the transform when the worker target only shows up in the resolved config', () => { |
| 104 | + const orchestrion = sentryRemixVitePlugin()[1]!; |
| 105 | + |
| 106 | + callHook(orchestrion.config, {} as UserConfig, BUILD_ENV); |
| 107 | + callHook(orchestrion.configResolved, WORKER_CONFIG); |
| 108 | + |
| 109 | + expect(callHook(orchestrion.transform, 'code', 'mysql.js', { ssr: true })).toBeNull(); |
| 110 | + expect(orchestrionTransform).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments