Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: client-side simulation does not work in test #157

Merged
merged 3 commits into from
Jan 12, 2024
Merged
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
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({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously we called createEnv before "simulate" the window object below, which mean we already get env in server mode.

Nothing change to the test result, but at least the client-side simulation is fixed.

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(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically we don't have to check this in this test case, but I figure why not🤷

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