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
``

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" });
});
`

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") }],
  },
});
``

So how to correctly mock the serverRuntimeConfig?
Was this page helpful?