Skip to content

Commit

Permalink
fix: client-side simulation does not work in test (#157)
Browse files Browse the repository at this point in the history
Co-authored-by: Julius Marminge <[email protected]>
  • Loading branch information
chungweileong94 and juliusmarminge authored Jan 12, 2024
1 parent f4d5aeb commit 6b6aca3
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/core/test/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,19 @@ describe("shared can be accessed on both server and client", () => {
FOO_BAR: "foo",
};

const env = createEnv({
shared: {
NODE_ENV: z.enum(["development", "production", "test"]),
},
clientPrefix: "FOO_",
server: { BAR: z.string() },
client: { FOO_BAR: z.string() },
runtimeEnv: process.env,
});
function lazyCreateEnv() {
return createEnv({
shared: {
NODE_ENV: z.enum(["development", "production", "test"]),
},
clientPrefix: "FOO_",
server: { BAR: z.string() },
client: { FOO_BAR: z.string() },
runtimeEnv: process.env,
});
}

expectTypeOf(env).toEqualTypeOf<
expectTypeOf(lazyCreateEnv).returns.toEqualTypeOf<
Readonly<{
NODE_ENV: "development" | "production" | "test";
BAR: string;
Expand All @@ -359,6 +361,8 @@ describe("shared can be accessed on both server and client", () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
globalThis.window = undefined as any;

const env = lazyCreateEnv();

expect(env).toMatchObject({
NODE_ENV: "development",
BAR: "bar",
Expand All @@ -373,10 +377,13 @@ describe("shared can be accessed on both server and client", () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any
globalThis.window = {} as any;

expect(env).toMatchObject({
NODE_ENV: "development",
FOO_BAR: "foo",
});
const env = lazyCreateEnv();

expect(() => env.BAR).toThrowErrorMatchingInlineSnapshot(
`"❌ Attempted to access a server-side environment variable on the client"`
);
expect(env.FOO_BAR).toBe("foo");
expect(env.NODE_ENV).toBe("development");

globalThis.window = window;
});
Expand Down

2 comments on commit 6b6aca3

@vercel
Copy link

@vercel vercel bot commented on 6b6aca3 Jan 12, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

t3-env – ./docs

env.t3.wtf
t3-env-git-main-t3-oss.vercel.app
t3-env.vercel.app
t3-env-t3-oss.vercel.app
env.t3.gg

@vercel
Copy link

@vercel vercel bot commented on 6b6aca3 Jan 12, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

t3-env-nextjs – ./examples/nextjs

t3-env-nextjs-t3-oss.vercel.app
t3-env-nextjs-git-main-t3-oss.vercel.app
t3-env-nextjs.vercel.app

Please sign in to comment.