Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/build/virtual/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ export default function storage(nitro: Nitro) {

const driverImports = [...new Set(mounts.map((m) => m.driver))];

const tracingEnabled = !!(
typeof nitro.options.tracingChannel === "object" && nitro.options.tracingChannel?.unstorage
);
Comment thread
pi0 marked this conversation as resolved.

return /* js */ `
import { createStorage } from 'unstorage'
${tracingEnabled ? `import { withTracing } from 'unstorage/tracing'` : ""}
Comment thread
pi0 marked this conversation as resolved.
import { assets } from '#nitro/virtual/server-assets'

${driverImports.map((i) => genImport(i, genSafeVariableName(i))).join("\n")}
Expand All @@ -39,7 +44,7 @@ export function initStorage() {
`storage.mount('${m.path}', ${genSafeVariableName(m.driver)}(${JSON.stringify(m.opts)}))`
)
.join("\n")}
return storage
return ${tracingEnabled ? "withTracing(storage)" : "storage"}
}
`;
},
Expand Down
1 change: 1 addition & 0 deletions src/config/resolvers/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export async function resolveTracingOptions(options: NitroOptions) {
options.tracingChannel = {
srvx: true,
h3: true,
unstorage: true,
...(typeof options.tracingChannel === "object" ? options.tracingChannel : {}),
};
Comment thread
pi0 marked this conversation as resolved.
options.plugins = options.plugins || [];
Expand Down
1 change: 1 addition & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ export interface ServerAssetDir {
export interface TracingOptions {
srvx?: boolean;
h3?: boolean;
unstorage?: boolean;
}

// Storage mounts
Expand Down
38 changes: 38 additions & 0 deletions test/unit/virtual-storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import type { Nitro } from "nitro/types";

import storage from "../../src/build/virtual/storage.ts";

function createNitroStub(tracingChannel: Nitro["options"]["tracingChannel"]): Nitro {
return {
options: {
dev: true,
preset: "nitro-dev",
storage: {},
devStorage: {},
tracingChannel,
},
} as unknown as Nitro;
}

describe("virtual/storage template", () => {
it("does not wrap storage when tracingChannel is disabled", () => {
const template = storage(createNitroStub(undefined)).template();
expect(template).not.toContain("withTracing");
expect(template).not.toContain("unstorage/tracing");
expect(template).toContain("return storage");
});

it("does not wrap storage when tracingChannel.unstorage is false", () => {
const template = storage(
createNitroStub({ srvx: true, h3: true, unstorage: false })
).template();
expect(template).not.toContain("withTracing");
});

Copilot AI Apr 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests cover unstorage: false and unstorage: true, but they don't cover the defaulting behavior introduced in the resolver (i.e., tracing enabled with tracingChannel set but without an explicit unstorage property). Add a test case where tracingChannel is an object missing unstorage (and, if supported, a case where tracingChannel is true) to ensure the template behavior matches the intended defaults.

Suggested change
it("wraps storage with withTracing when tracingChannel.unstorage is omitted", () => {
const template = storage(createNitroStub({ srvx: true, h3: true })).template();
expect(template).toContain(`import { withTracing } from 'unstorage/tracing'`);
expect(template).toContain("return withTracing(storage)");
});

Copilot uses AI. Check for mistakes.
it("wraps storage with withTracing when tracingChannel.unstorage is true", () => {
const template = storage(createNitroStub({ srvx: true, h3: true, unstorage: true })).template();
expect(template).toContain(`import { withTracing } from 'unstorage/tracing'`);
expect(template).toContain("return withTracing(storage)");
});
});
Loading