Vitest Test can't load serverRuntimeConfig

Hey im currently running into this error:
FAIL src/server/tests/userid.test.ts [ src/server/tests/userid.test.ts ]
TypeError: Cannot destructure property 'serverRuntimeConfig' of 'default(...)' as it is undefined.
❯ src/server/trpc/router/distribution-report.ts:20:9
18| import { loadMessages } from "utils/i18n";
19|
20| const { serverRuntimeConfig } = getConfig();
| ^
21|
22| /**
❯ src/server/trpc/router/index.ts:9:31
❯ src/server/tests/userid.test.ts:2:31
FAIL src/server/tests/userid.test.ts [ src/server/tests/userid.test.ts ]
TypeError: Cannot destructure property 'serverRuntimeConfig' of 'default(...)' as it is undefined.
❯ src/server/trpc/router/distribution-report.ts:20:9
18| import { loadMessages } from "utils/i18n";
19|
20| const { serverRuntimeConfig } = getConfig();
| ^
21|
22| /**
❯ src/server/trpc/router/index.ts:9:31
❯ src/server/tests/userid.test.ts:2:31
`` This is my userid.test.ts:
import { type inferProcedureInput } from "@trpc/server";
import { createInnerTRPCContext } from "server/trpc/context";
import { AppRouter, appRouter } from "server/trpc/router";
import { expect, test } from "vitest";

test("example router", async () => {
const ctx = createInnerTRPCContext({
session: {
user: { token: "123", role: "ADMIN" },
expires: "1",
},
});
const caller = appRouter.createCaller(ctx);

type Input = inferProcedureInput<AppRouter["user"]["getById"]>;
const input: Input = "1";
// i know that the code below won't work, but thats not the problem of this error.
const example = await caller.user.getById(input);

expect(example).toMatchObject({ greeting: "Hello test" });
});
import { type inferProcedureInput } from "@trpc/server";
import { createInnerTRPCContext } from "server/trpc/context";
import { AppRouter, appRouter } from "server/trpc/router";
import { expect, test } from "vitest";

test("example router", async () => {
const ctx = createInnerTRPCContext({
session: {
user: { token: "123", role: "ADMIN" },
expires: "1",
},
});
const caller = appRouter.createCaller(ctx);

type Input = inferProcedureInput<AppRouter["user"]["getById"]>;
const input: Input = "1";
// i know that the code below won't work, but thats not the problem of this error.
const example = await caller.user.getById(input);

expect(example).toMatchObject({ greeting: "Hello test" });
});
` vitest.config.ts
import { defineConfig, configDefaults } from "vitest/config";
import { resolve } from "path";
import tsconfigPaths from "vite-tsconfig-paths";
import react from "@vitejs/plugin-react";
import { loadEnvConfig } from "@next/env";

loadEnvConfig(process.cwd());

export default defineConfig({
plugins: [tsconfigPaths(), react()],
test: {
environment: "node",
exclude: [...configDefaults.exclude],
},
resolve: {
alias: [{ find: "@", replacement: resolve(__dirname, "./src") }],
},
});
import { defineConfig, configDefaults } from "vitest/config";
import { resolve } from "path";
import tsconfigPaths from "vite-tsconfig-paths";
import react from "@vitejs/plugin-react";
import { loadEnvConfig } from "@next/env";

loadEnvConfig(process.cwd());

export default defineConfig({
plugins: [tsconfigPaths(), react()],
test: {
environment: "node",
exclude: [...configDefaults.exclude],
},
resolve: {
alias: [{ find: "@", replacement: resolve(__dirname, "./src") }],
},
});
`` So how to correctly mock the serverRuntimeConfig?
1 Reply
froxx
froxx13mo ago
Judging your file naming, it looks like you are testing behaviour in your user scope. Your error is occuring in .../router/distribution-report.ts which may not be related to your test suite. This code may only get executed because you load your whole appRouter in your user test. You could prevent that by just importing the regarding sub-router (userRouter?) there. This does not answer the actual question, but might help you with your current situation. Maybe it's a generally bad idea to access Next's config data there, but I have no hint on what to do instead. Maybe someone else might have another idea on that.