diff --git a/dev-packages/e2e-tests/test-applications/nuxt-4/app/middleware/redirect-to-param-page.ts b/dev-packages/e2e-tests/test-applications/nuxt-4/app/middleware/redirect-to-param-page.ts new file mode 100644 index 000000000000..3d68c7427df1 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nuxt-4/app/middleware/redirect-to-param-page.ts @@ -0,0 +1,3 @@ +import { defineNuxtRouteMiddleware, navigateTo } from '#imports'; + +export default defineNuxtRouteMiddleware(() => navigateTo('/test-param/redirect-target')); diff --git a/dev-packages/e2e-tests/test-applications/nuxt-4/app/pages/redirect-in-route-middleware.vue b/dev-packages/e2e-tests/test-applications/nuxt-4/app/pages/redirect-in-route-middleware.vue new file mode 100644 index 000000000000..4efd556135d3 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nuxt-4/app/pages/redirect-in-route-middleware.vue @@ -0,0 +1,9 @@ + + + diff --git a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/errors.ssr.streaming-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/errors.ssr.streaming-lifecycle.test.ts index b30ebd221644..b40c961dc867 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/errors.ssr.streaming-lifecycle.test.ts +++ b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/errors.ssr.streaming-lifecycle.test.ts @@ -1,6 +1,11 @@ -import { HTTP_RESPONSE_STATUS_CODE, URL_PATH } from '@sentry/conventions/attributes'; +import { HTTP_RESPONSE_STATUS_CODE, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; import { expect, test } from '@playwright/test'; -import { waitForEnvelopeItem, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; +import { + collectStreamedSpansUntilSegment, + waitForEnvelopeItem, + waitForError, + waitForStreamedSpan, +} from '@sentry-internal/test-utils'; import type { SerializedStreamedSpan } from '@sentry-internal/test-utils'; import { isViteServer, NITRO_ONLY } from './isViteServer'; @@ -84,6 +89,33 @@ test.describe('page that fails during SSR', () => { expect(rootSpan.attributes[HTTP_RESPONSE_STATUS_CODE]).toEqual({ type: 'integer', value: 500 }); }); + test('sends a span for the failed render and a `renderErrorPage` span for the render of the error page', async ({ + request, + }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-4', + span => span.attributes[URL_PATH]?.value === SSR_ERROR_PATH, + span => span.name === 'renderErrorPage', + ); + + await request.get(SSR_ERROR_PATH); + + const spans = await spansPromise; + const rootSpan = spans.find(span => span.is_segment); + const renderSpans = spans + .filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.function.nuxt.render') + .map(span => ({ + name: span.name, + status: span.status, + isChildOfRoot: span.parent_span_id === rootSpan?.span_id, + })); + + expect(renderSpans).toEqual([ + { name: 'renderPage', status: 'error', isChildOfRoot: true }, + { name: 'renderErrorPage', status: 'ok', isChildOfRoot: true }, + ]); + }); + test('names the root span after the page route', async ({ request }) => { test.fail( true, diff --git a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.static-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.static-lifecycle.test.ts index 451ab2f1a4d9..c3227bf94a43 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.static-lifecycle.test.ts +++ b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.static-lifecycle.test.ts @@ -1,4 +1,5 @@ -import { SENTRY_OP, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; +import { HTTP_ROUTE, SENTRY_OP, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; +import { FUNCTION } from '@sentry/conventions/op'; import { expect, test } from '@playwright/test'; import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; @@ -21,6 +22,23 @@ test('sends a server action transaction on pageload', async ({ page }) => { ); }); +test('sends a `renderPage` span for the page render as a child of the server transaction', async ({ request }) => { + const transactionPromise = waitForTransaction('nuxt-4', transactionEvent => { + return transactionEvent.contexts?.trace?.data?.[URL_PATH] === '/test-param/render-span'; + }); + + await request.get('/test-param/render-span'); + + const transaction = await transactionPromise; + const renderSpans = transaction.spans?.filter(span => span.origin === 'auto.function.nuxt.render'); + + expect(renderSpans).toHaveLength(1); + expect(renderSpans?.[0]?.description).toBe('renderPage'); + expect(renderSpans?.[0]?.op).toBe(FUNCTION); + expect(renderSpans?.[0]?.parent_span_id).toBe(transaction.contexts?.trace?.span_id); + expect(renderSpans?.[0]?.data[HTTP_ROUTE]).toBe('/test-param/:param()'); +}); + [ // Nuxt build output that the HTTP integration does not ignore. '/_nuxt/builds/latest.json', diff --git a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.streaming-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.streaming-lifecycle.test.ts index 5523bced86b5..31164c4c08f1 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.streaming-lifecycle.test.ts +++ b/dev-packages/e2e-tests/test-applications/nuxt-4/tests/tracing.server.streaming-lifecycle.test.ts @@ -1,7 +1,9 @@ -import { URL_PATH } from '@sentry/conventions/attributes'; +import { CODE_FUNCTION_NAME, ERROR_TYPE, HTTP_ROUTE, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; +import { FUNCTION } from '@sentry/conventions/op'; import { expect, test } from '@playwright/test'; import { collectStreamedSpans, + collectStreamedSpansUntilSegment, getSpanOp, waitForStreamedSpan, waitForStreamedSpans, @@ -10,6 +12,8 @@ import { IMPORT_SURFACES } from './importSurfaces'; import { isViteServer, NITRO_ONLY } from './isViteServer'; import { SERVER_ORIGIN } from './runtime'; +const RENDER_ORIGIN = 'auto.function.nuxt.render'; + test('sends a server root span on pageload', async ({ page }) => { const serverSpanPromise = waitForStreamedSpan('nuxt-4', span => { return span.is_segment && span.name.includes('GET /test-param/'); @@ -23,6 +27,61 @@ test('sends a server root span on pageload', async ({ page }) => { expect(serverSpan.attributes['sentry.origin']?.value).toBe(SERVER_ORIGIN); }); +test('sends a `renderPage` span for the page render as a child of the server root span', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-4', + span => span.attributes[URL_PATH]?.value === '/test-param/render-span', + span => span.name === 'renderPage', + ); + + await request.get('/test-param/render-span'); + + const spans = await spansPromise; + const rootSpan = spans.find(span => span.is_segment); + const renderSpans = spans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === RENDER_ORIGIN); + + expect(renderSpans).toHaveLength(1); + expect(renderSpans[0]?.name).toBe('renderPage'); + expect(renderSpans[0]?.status).toBe('ok'); + expect(renderSpans[0]?.parent_span_id).toBe(rootSpan?.span_id); + expect(getSpanOp(renderSpans[0]!)).toBe(FUNCTION); + expect(renderSpans[0]?.attributes[CODE_FUNCTION_NAME]).toEqual({ type: 'string', value: 'renderPage' }); + expect(renderSpans[0]?.attributes[HTTP_ROUTE]).toEqual({ type: 'string', value: '/test-param/:param()' }); +}); + +test('keeps the ok status of the render span when a route middleware redirects', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-4', + span => span.attributes[URL_PATH]?.value === '/redirect-in-route-middleware', + span => span.name === 'renderPage', + ); + + const response = await request.get('/redirect-in-route-middleware', { maxRedirects: 0 }); + + const renderSpan = (await spansPromise).find(span => span.name === 'renderPage'); + + expect(response.status()).toBe(302); + expect(renderSpan?.status).toBe('ok'); + expect(renderSpan?.attributes[ERROR_TYPE]).toBeUndefined(); + // The router is already at the target of the redirect, so the span has no route. + expect(renderSpan?.attributes[HTTP_ROUTE]).toBeUndefined(); +}); + +test('keeps the ok status of the render span for a URL that matches no page', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-4', + span => span.attributes[URL_PATH]?.value === '/no-page-for-this-url', + span => span.name === 'renderPage', + ); + + const response = await request.get('/no-page-for-this-url'); + + const renderSpan = (await spansPromise).find(span => span.name === 'renderPage'); + + expect(response.status()).toBe(404); + expect(renderSpan?.status).toBe('ok'); +}); + [ // Nuxt build output that the HTTP integration does not ignore. '/_nuxt/builds/latest.json', diff --git a/dev-packages/e2e-tests/test-applications/nuxt-5/app/middleware/redirect-to-param-page.ts b/dev-packages/e2e-tests/test-applications/nuxt-5/app/middleware/redirect-to-param-page.ts new file mode 100644 index 000000000000..3d68c7427df1 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nuxt-5/app/middleware/redirect-to-param-page.ts @@ -0,0 +1,3 @@ +import { defineNuxtRouteMiddleware, navigateTo } from '#imports'; + +export default defineNuxtRouteMiddleware(() => navigateTo('/test-param/redirect-target')); diff --git a/dev-packages/e2e-tests/test-applications/nuxt-5/app/pages/redirect-in-route-middleware.vue b/dev-packages/e2e-tests/test-applications/nuxt-5/app/pages/redirect-in-route-middleware.vue new file mode 100644 index 000000000000..4efd556135d3 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/nuxt-5/app/pages/redirect-in-route-middleware.vue @@ -0,0 +1,9 @@ + + + diff --git a/dev-packages/e2e-tests/test-applications/nuxt-5/tests/errors.ssr.test.ts b/dev-packages/e2e-tests/test-applications/nuxt-5/tests/errors.ssr.test.ts index 8b3e573b116a..34501279299c 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-5/tests/errors.ssr.test.ts +++ b/dev-packages/e2e-tests/test-applications/nuxt-5/tests/errors.ssr.test.ts @@ -1,6 +1,11 @@ -import { HTTP_RESPONSE_STATUS_CODE, URL_PATH } from '@sentry/conventions/attributes'; +import { HTTP_RESPONSE_STATUS_CODE, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; import { expect, test } from '@playwright/test'; -import { waitForEnvelopeItem, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; +import { + collectStreamedSpansUntilSegment, + waitForEnvelopeItem, + waitForError, + waitForStreamedSpan, +} from '@sentry-internal/test-utils'; import type { SerializedStreamedSpan } from '@sentry-internal/test-utils'; type EnvelopeItem = Parameters[1]>[0]; @@ -79,6 +84,33 @@ test.describe('page that fails during SSR', () => { expect(rootSpan.attributes[HTTP_RESPONSE_STATUS_CODE]).toEqual({ type: 'integer', value: 500 }); }); + test('sends a span for the failed render and a `renderErrorPage` span for the render of the error page', async ({ + request, + }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-5', + span => span.attributes[URL_PATH]?.value === SSR_ERROR_PATH, + span => span.name === 'renderErrorPage', + ); + + await request.get(SSR_ERROR_PATH); + + const spans = await spansPromise; + const rootSpan = spans.find(span => span.is_segment); + const renderSpans = spans + .filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.function.nuxt.render') + .map(span => ({ + name: span.name, + status: span.status, + isChildOfRoot: span.parent_span_id === rootSpan?.span_id, + })); + + expect(renderSpans).toEqual([ + { name: 'renderPage', status: 'error', isChildOfRoot: true }, + { name: 'renderErrorPage', status: 'ok', isChildOfRoot: true }, + ]); + }); + test('names the root span after the page route', async ({ request }) => { test.fail(true, 'Known bug: the root span of a failed render gets no page route, so its name is `GET`'); diff --git a/dev-packages/e2e-tests/test-applications/nuxt-5/tests/tracing.server.test.ts b/dev-packages/e2e-tests/test-applications/nuxt-5/tests/tracing.server.test.ts index 100ca864b4ff..e8c97b890c09 100644 --- a/dev-packages/e2e-tests/test-applications/nuxt-5/tests/tracing.server.test.ts +++ b/dev-packages/e2e-tests/test-applications/nuxt-5/tests/tracing.server.test.ts @@ -1,7 +1,9 @@ -import { URL_PATH } from '@sentry/conventions/attributes'; +import { CODE_FUNCTION_NAME, ERROR_TYPE, HTTP_ROUTE, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; +import { FUNCTION } from '@sentry/conventions/op'; import { expect, test } from '@playwright/test'; import { collectStreamedSpans, + collectStreamedSpansUntilSegment, getSpanOp, waitForStreamedSpan, waitForStreamedSpans, @@ -9,6 +11,8 @@ import { import { IMPORT_SURFACES } from './importSurfaces'; import { SERVER_ORIGIN } from './runtime'; +const RENDER_ORIGIN = 'auto.function.nuxt.render'; + test('sends a server root span on pageload', async ({ page }) => { const serverSpanPromise = waitForStreamedSpan('nuxt-5', span => { return span.is_segment && span.name.includes('GET /test-param/'); @@ -22,6 +26,61 @@ test('sends a server root span on pageload', async ({ page }) => { expect(serverSpan.attributes['sentry.origin']?.value).toBe(SERVER_ORIGIN); }); +test('sends a `renderPage` span for the page render as a child of the server root span', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-5', + span => span.attributes[URL_PATH]?.value === '/test-param/render-span', + span => span.name === 'renderPage', + ); + + await request.get('/test-param/render-span'); + + const spans = await spansPromise; + const rootSpan = spans.find(span => span.is_segment); + const renderSpans = spans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === RENDER_ORIGIN); + + expect(renderSpans).toHaveLength(1); + expect(renderSpans[0]?.name).toBe('renderPage'); + expect(renderSpans[0]?.status).toBe('ok'); + expect(renderSpans[0]?.parent_span_id).toBe(rootSpan?.span_id); + expect(getSpanOp(renderSpans[0]!)).toBe(FUNCTION); + expect(renderSpans[0]?.attributes[CODE_FUNCTION_NAME]).toEqual({ type: 'string', value: 'renderPage' }); + expect(renderSpans[0]?.attributes[HTTP_ROUTE]).toEqual({ type: 'string', value: '/test-param/:param()' }); +}); + +test('keeps the ok status of the render span when a route middleware redirects', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-5', + span => span.attributes[URL_PATH]?.value === '/redirect-in-route-middleware', + span => span.name === 'renderPage', + ); + + const response = await request.get('/redirect-in-route-middleware', { maxRedirects: 0 }); + + const renderSpan = (await spansPromise).find(span => span.name === 'renderPage'); + + expect(response.status()).toBe(302); + expect(renderSpan?.status).toBe('ok'); + expect(renderSpan?.attributes[ERROR_TYPE]).toBeUndefined(); + // The router is already at the target of the redirect, so the span has no route. + expect(renderSpan?.attributes[HTTP_ROUTE]).toBeUndefined(); +}); + +test('keeps the ok status of the render span for a URL that matches no page', async ({ request }) => { + const spansPromise = collectStreamedSpansUntilSegment( + 'nuxt-5', + span => span.attributes[URL_PATH]?.value === '/no-page-for-this-url', + span => span.name === 'renderPage', + ); + + const response = await request.get('/no-page-for-this-url'); + + const renderSpan = (await spansPromise).find(span => span.name === 'renderPage'); + + expect(response.status()).toBe(404); + expect(renderSpan?.status).toBe('ok'); +}); + [ // Nuxt build output that the HTTP integration does not ignore. '/_nuxt/builds/latest.json', diff --git a/packages/nuxt/src/module.ts b/packages/nuxt/src/module.ts index 3c1c1118003c..b4b7b2dc90fa 100644 --- a/packages/nuxt/src/module.ts +++ b/packages/nuxt/src/module.ts @@ -10,6 +10,7 @@ import { setupCloudflareEntry } from './vite/cloudflare'; import { addTunnelPlugin, setupNitroServer } from './vite/nitroServer'; import { setupOrchestrion } from './vite/orchestrion'; import { NUXT_RELEASE_RUNTIME_CONFIG_KEY, setupSourceMaps } from './vite/sourceMaps'; +import { enableNuxtTracingChannels } from './vite/tracingChannel'; import { findCloudflareOptionsFile, findDefaultSdkInitFile, resolveServerApi } from './vite/utils'; import { setupViteServer } from './vite/viteServer'; import { stringifyForCodeInjection, warnOnConflictingSourceMapOptions } from '@sentry/core'; @@ -92,6 +93,11 @@ export default defineNuxtModule({ const nuxtMajor = parseInt((nuxt as unknown as { _version: string })._version?.split('.')[0] ?? '3', 10); const isMinNuxtV4 = nuxtMajor >= 4; + // Only when the module also sets up the server SDK, which subscribes to the channels. + if (serverConfigFile || cloudflareOptionsFile) { + await enableNuxtTracingChannels(nuxt, serverApi); + } + // Orchestrion runs on both the Node path (gated on a server config file) and the Cloudflare path // (which has no server config file, the SDK is set up via `withSentry` or `sentryCloudflareNitroPlugin`). The // Cloudflare detection happens inside, keyed off the resolved Nitro preset. diff --git a/packages/nuxt/src/vite/tracingChannel.ts b/packages/nuxt/src/vite/tracingChannel.ts new file mode 100644 index 000000000000..5c542578a920 --- /dev/null +++ b/packages/nuxt/src/vite/tracingChannel.ts @@ -0,0 +1,53 @@ +import { hasNuxtCompatibility } from '@nuxt/kit'; +import type { Nuxt, NuxtConfig } from '@nuxt/schema'; +import type { ServerApi } from './utils'; + +type TracingChannelValue = NuxtConfig['tracingChannel']; + +// The SDK's own instrumentation makes these spans. It also works on Nitro v2, which has no channels. +const NITRO_CHANNEL_GROUPS_OFF = { srvx: false, h3: false, unstorage: false, db0: false }; + +/** + * Turns on the `nuxt.*` tracing channels for `nuxtTracingChannelIntegration`. + * A `tracingChannel` value in the Nuxt config of the app wins. + */ +export async function enableNuxtTracingChannels(nuxt: Nuxt, serverApi: ServerApi): Promise { + // The option exists since Nuxt 4.5, but the `nuxt.request` channel came in 4.7. + if (!(await hasNuxtCompatibility({ nuxt: '>=4.7.0' }, nuxt))) { + return; + } + + // The resolved option is `false` for both "not set" and `false`. Only the raw layer config tells them apart. + const layerConfigs = nuxt.options._layers.map(layer => layer.config); + const userValue = mergeLayerValues(layerConfigs.map(config => config?.tracingChannel)); + // oxlint-disable-next-line typescript/no-deprecated -- Nuxt still reads it + const deprecatedUserValue = mergeLayerValues(layerConfigs.map(config => config?.nitro?.tracingChannel)); + + if (typeof userValue === 'boolean' || typeof deprecatedUserValue === 'boolean') { + return; + } + + // Another module or a CLI override set the option. Keep that value. + if (!userValue && !deprecatedUserValue && nuxt.options.tracingChannel) { + return; + } + + nuxt.options.tracingChannel = { + nuxt: true, + // Only Nitro v3 has these groups. + ...(serverApi === 'nitro3' && NITRO_CHANNEL_GROUPS_OFF), + ...userValue, + ...deprecatedUserValue, + }; +} + +/** Merges the layer values as Nuxt does. The first layer that sets a key wins, and the app is the first layer. */ +function mergeLayerValues(values: TracingChannelValue[]): TracingChannelValue { + return values.reduce((merged, value) => { + if (typeof merged === 'object' && typeof value === 'object') { + return { ...value, ...merged }; + } + + return merged ?? value; + }, undefined); +} diff --git a/packages/nuxt/test/vite/tracingChannel.test.ts b/packages/nuxt/test/vite/tracingChannel.test.ts new file mode 100644 index 000000000000..b3a4e23edcd3 --- /dev/null +++ b/packages/nuxt/test/vite/tracingChannel.test.ts @@ -0,0 +1,173 @@ +import type { Nuxt } from '@nuxt/schema'; +import { describe, expect, it } from 'vitest'; +import { enableNuxtTracingChannels } from '../../src/vite/tracingChannel'; + +type LayerConfig = { tracingChannel?: unknown; nitro?: { tracingChannel?: unknown } }; + +interface MockNuxtOptions { + version?: string; + /** The raw config of each layer, the app first. */ + layers?: Array; + /** What Nuxt resolved from the layers. `false` for an app that sets no option. */ + resolved?: unknown; +} + +function createMockNuxt({ version = '4.7.0', layers = [{}], resolved = false }: MockNuxtOptions = {}): Nuxt { + return { + _version: version, + options: { tracingChannel: resolved, _layers: layers.map(config => ({ config })) }, + // The version check of `@nuxt/kit` calls the `kit:compatibility` hook. + callHook: () => Promise.resolve(), + } as unknown as Nuxt; +} + +const ONLY_NUXT_CHANNELS = { nuxt: true, srvx: false, h3: false, unstorage: false, db0: false }; + +describe('enableNuxtTracingChannels', () => { + it('turns on only the Nuxt channel group when the app sets no option', async () => { + const nuxt = createMockNuxt(); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual(ONLY_NUXT_CHANNELS); + }); + + it.each(['nitro2', 'nuxt'] as const)('writes no Nitro channel group on the `%s` server API', async serverApi => { + const nuxt = createMockNuxt(); + + await enableNuxtTracingChannels(nuxt, serverApi); + + expect(nuxt.options.tracingChannel).toEqual({ nuxt: true }); + }); + + it.each([ + { userValue: { h3: true }, expected: { ...ONLY_NUXT_CHANNELS, h3: true } }, + { userValue: { nuxt: false }, expected: { ...ONLY_NUXT_CHANNELS, nuxt: false } }, + { + userValue: { nuxt: false, unstorage: true, db0: true }, + expected: { ...ONLY_NUXT_CHANNELS, nuxt: false, unstorage: true, db0: true }, + }, + { userValue: {}, expected: ONLY_NUXT_CHANNELS }, + ])('keeps the keys that the app sets: $userValue', async ({ userValue, expected }) => { + const nuxt = createMockNuxt({ layers: [{ tracingChannel: userValue }], resolved: { nuxt: true, ...userValue } }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual(expected); + }); + + it.each([ + { userValue: false, resolved: false }, + { userValue: true, resolved: { nuxt: true } }, + ])('keeps the value of Nuxt when the app sets the whole option to $userValue', async ({ userValue, resolved }) => { + const nuxt = createMockNuxt({ layers: [{ tracingChannel: userValue }], resolved }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toBe(resolved); + }); + + it('keeps a value that no layer sets, for example the value of another module', async () => { + const valueOfOtherModule = { nuxt: true, h3: true }; + const nuxt = createMockNuxt({ resolved: valueOfOtherModule }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toBe(valueOfOtherModule); + }); + + describe('with layers', () => { + it('keeps the keys that an extended layer sets', async () => { + const nuxt = createMockNuxt({ layers: [{}, { tracingChannel: { unstorage: true } }] }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual({ ...ONLY_NUXT_CHANNELS, unstorage: true }); + }); + + it('prefers the key of the app over the same key of an extended layer', async () => { + const nuxt = createMockNuxt({ + layers: [{ tracingChannel: { h3: true } }, { tracingChannel: { h3: false, db0: true } }], + }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual({ ...ONLY_NUXT_CHANNELS, h3: true, db0: true }); + }); + + it('keeps the value of Nuxt when only an extended layer sets the whole option to false', async () => { + const nuxt = createMockNuxt({ layers: [{}, { tracingChannel: false }] }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toBe(false); + }); + + // As in Nuxt, the object of the app replaces the boolean of the layer. + it('ignores the boolean of an extended layer when the app sets keys', async () => { + const nuxt = createMockNuxt({ layers: [{ tracingChannel: { h3: true } }, { tracingChannel: false }] }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual({ ...ONLY_NUXT_CHANNELS, h3: true }); + }); + + it('reads a layer that has no config', async () => { + const nuxt = createMockNuxt({ layers: [{}, null] }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual(ONLY_NUXT_CHANNELS); + }); + }); + + describe('with the deprecated `nitro.tracingChannel` option', () => { + it('keeps the keys that the app sets', async () => { + const nuxt = createMockNuxt({ layers: [{ nitro: { tracingChannel: { unstorage: true } } }] }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual({ ...ONLY_NUXT_CHANNELS, unstorage: true }); + }); + + // Same order as in Nuxt. + it('prefers its key over the same key of `tracingChannel`', async () => { + const nuxt = createMockNuxt({ + layers: [{ tracingChannel: { h3: true, db0: true }, nitro: { tracingChannel: { h3: false } } }], + }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toEqual({ ...ONLY_NUXT_CHANNELS, h3: false, db0: true }); + }); + + it.each([ + { userValue: false, resolved: false }, + { userValue: true, resolved: {} }, + ])('keeps the value of Nuxt when the app sets the whole option to $userValue', async ({ userValue, resolved }) => { + const nuxt = createMockNuxt({ layers: [{ nitro: { tracingChannel: userValue } }], resolved }); + + await enableNuxtTracingChannels(nuxt, 'nitro3'); + + expect(nuxt.options.tracingChannel).toBe(resolved); + }); + }); + + describe('Nuxt version', () => { + it.each(['3.17.0', '4.5.0', '4.6.4'])('writes nothing on Nuxt %s', async version => { + const nuxt = createMockNuxt({ version }); + + await enableNuxtTracingChannels(nuxt, 'nitro2'); + + expect(nuxt.options.tracingChannel).toBe(false); + }); + + it.each(['4.7.0', '4.10.2', '5.0.0-29384756.f4716665'])('writes the option on Nuxt %s', async version => { + const nuxt = createMockNuxt({ version }); + + await enableNuxtTracingChannels(nuxt, 'nitro2'); + + expect(nuxt.options.tracingChannel).toEqual({ nuxt: true }); + }); + }); +});