Skip to content

Commit 1d54f39

Browse files
committed
Add some env:server test
1 parent c5d950c commit 1d54f39

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

apps/tests/src/e2e/server-function.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,10 @@ test.describe("server-function", () => {
8181
await page.goto("http://localhost:3000/server-function-blob");
8282
await expect(page.locator("#server-fn-test")).toContainText('{"result":true}');
8383
});
84+
85+
// TODO not sure if this is the correct place
86+
test("should build with a env:server", async ({ page }) => {
87+
await page.goto("http://localhost:3000/server-env");
88+
await expect(page.locator("#server-fn-test")).toContainText('{"result":true}');
89+
});
8490
});

apps/tests/src/env.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare module "env:server" {
2+
export const SERVER_EXAMPLE: string;
3+
}
4+
declare module "env:server/runtime" {
5+
const env: {
6+
NODE_ENV: string;
7+
};
8+
9+
export default env;
10+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { SERVER_EXAMPLE } from "env:server";
2+
import env from "env:server/runtime";
3+
import { createEffect, createSignal } from "solid-js";
4+
5+
async function getServerCompiledEnv() {
6+
"use server";
7+
8+
return await Promise.resolve(SERVER_EXAMPLE);
9+
}
10+
11+
async function getServerRuntimeEnv() {
12+
"use server";
13+
14+
return await Promise.resolve(env.NODE_ENV);
15+
}
16+
17+
async function checkServerEnvOnClient() {
18+
try {
19+
await import("env:server");
20+
return false;
21+
} catch {
22+
return true;
23+
}
24+
}
25+
26+
export default function App() {
27+
const [output, setOutput] = createSignal<{ result?: boolean }>({});
28+
29+
createEffect(async () => {
30+
const resultA = await getServerCompiledEnv();
31+
const resultB = await getServerRuntimeEnv();
32+
const checkImport = await checkServerEnvOnClient();
33+
setOutput(prev => ({ ...prev, result: !!resultA && !!resultB && checkImport }));
34+
});
35+
36+
return (
37+
<main>
38+
<span id="server-fn-test">{JSON.stringify(output())}</span>
39+
</main>
40+
);
41+
}

apps/tests/vite.config.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
import { defineConfig } from "vite";
2-
import { solidStart } from "../../packages/start/src/config";
32
import { nitroV2Plugin } from "../../packages/start-nitro-v2-vite-plugin/src";
3+
import { solidStart } from "../../packages/start/src/config";
44

55
export default defineConfig({
66
server: {
77
port: 3000,
88
},
9-
plugins: [solidStart(), nitroV2Plugin()],
9+
plugins: [
10+
solidStart({
11+
env: {
12+
server: {
13+
load() {
14+
return {
15+
SERVER_EXAMPLE: "This is a server example.",
16+
};
17+
},
18+
},
19+
client: {
20+
load() {
21+
return {
22+
CLIENT_EXAMPLE: "This is a client example.",
23+
};
24+
},
25+
},
26+
},
27+
}),
28+
nitroV2Plugin(),
29+
],
1030
});

0 commit comments

Comments
 (0)