Skip to content

Commit 9daa75e

Browse files
authored
Merge pull request #1722 from birkskyum/expand-server-function-test
Add externalization test to server function suite
2 parents 600c115 + 1586391 commit 9daa75e

8 files changed

Lines changed: 179 additions & 42 deletions

File tree

pnpm-lock.yaml

Lines changed: 46 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
describe("server-function", () => {
2-
it("should isServer false on the client and true in the server function", () => {
2+
it("should have isServer false in the client", () => {
33
cy.visit("/");
4-
cy.get("#server-fn-test").contains(`{"client":false,"serverFn":true}`);
4+
cy.get("#server-fn-test").contains('{"clientWithIsServer":false}');
5+
})
6+
it("should have isServer true in the server function", () => {
7+
cy.visit("/is-server");
8+
cy.get("#server-fn-test").contains('{"serverFnWithIsServer":true}');
9+
})
10+
it("should externalize node builtin in server function", () => {
11+
cy.visit("/node-builtin");
12+
cy.get("#server-fn-test").contains('{"serverFnWithNodeBuiltin":"can/externalize"}');
13+
})
14+
it("should externalize npm module in server function", () => {
15+
cy.visit("npm-module");
16+
cy.get("#server-fn-test").contains('{"serverFnWithNpmModule":[2,4,6]}');
517
})
618
});

tests/server-function/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
"test": "pnpm run dev & cypress run"
1414
},
1515
"dependencies": {
16+
"@solidjs/meta": "^0.29.4",
17+
"@solidjs/router": "^0.15.3",
1618
"@solidjs/start": "workspace:*",
1719
"@solidjs/testing-library": "^0.8.10",
1820
"@testing-library/jest-dom": "^6.6.2",
1921
"@testing-library/user-event": "^14.5.2",
2022
"@vitest/ui": "^2.1.4",
2123
"jsdom": "^25.0.1",
24+
"lodash": "^4.17.21",
2225
"solid-js": "catalog:",
2326
"vinxi": "catalog:",
2427
"vite-plugin-solid": "catalog:",
@@ -31,9 +34,10 @@
3134
"node": ">=18"
3235
},
3336
"devDependencies": {
34-
"cypress-vite": "^1.6.0",
3537
"@cypress/code-coverage": "^3.13.10",
38+
"@types/lodash": "^4.17.14",
3639
"cypress": "^14.0.0",
37-
"cypress-plugin-tab": "^1.0.5"
40+
"cypress-plugin-tab": "^1.0.5",
41+
"cypress-vite": "^1.6.0"
3842
}
3943
}

tests/server-function/src/app.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
import { createEffect, createSignal } from "solid-js";
2-
import { isServer } from "solid-js/web";
1+
import { MetaProvider, Title } from "@solidjs/meta";
2+
import { Router } from "@solidjs/router";
3+
import { FileRoutes } from "@solidjs/start/router";
4+
import { Suspense } from "solid-js";
35
import "./app.css";
46

5-
function useServer() {
6-
"use server";
7-
return isServer;
8-
}
97
export default function App() {
10-
const [output, setOutput] = createSignal<{ client?: boolean; serverFn?: boolean }>({});
11-
12-
setOutput(prev => ({ ...prev, client: isServer }));
13-
14-
createEffect(async () => {
15-
const restult = await useServer();
16-
setOutput(prev => ({ ...prev, serverFn: restult }));
17-
});
18-
198
return (
20-
<main>
21-
<h1>Hello world!</h1>
22-
<span id="server-fn-test">{JSON.stringify(output())}</span>
23-
</main>
9+
<Router
10+
root={props => (
11+
<MetaProvider>
12+
<Title>SolidStart - Basic</Title>
13+
<a href="/">Client</a>
14+
<a href="/is-server">isserver</a>
15+
<a href="/node-builtin">node builtin</a>
16+
<a href="/npm-module">npm module (lodash)</a>
17+
<Suspense>{props.children}</Suspense>
18+
</MetaProvider>
19+
)}
20+
>
21+
<FileRoutes />
22+
</Router>
2423
);
2524
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import _ from "lodash";
2+
import { join } from 'path';
3+
import { createEffect, createSignal } from "solid-js";
4+
import { isServer } from "solid-js/web";
5+
6+
export default function App() {
7+
const [output, setOutput] = createSignal<{ clientWithIsServer?: boolean; }>({});
8+
9+
setOutput(prev => ({ ...prev, clientWithIsServer: isServer }));
10+
11+
return (
12+
<main>
13+
<span id="server-fn-test">{JSON.stringify(output())}</span>
14+
</main>
15+
);
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import _ from "lodash";
2+
import { join } from 'path';
3+
import { createEffect, createSignal } from "solid-js";
4+
import { isServer } from "solid-js/web";
5+
6+
function serverFnWithIsServer() {
7+
"use server";
8+
9+
return isServer;
10+
}
11+
12+
export default function App() {
13+
const [output, setOutput] = createSignal<{ serverFnWithIsServer?: boolean }>({});
14+
15+
16+
createEffect(async () => {
17+
const restult = await serverFnWithIsServer();
18+
setOutput(prev => ({ ...prev, serverFnWithIsServer: restult }));
19+
});
20+
21+
22+
return (
23+
<main>
24+
<span id="server-fn-test">{JSON.stringify(output())}</span>
25+
</main>
26+
);
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import _ from "lodash";
2+
import { join } from 'path';
3+
import { createEffect, createSignal } from "solid-js";
4+
import { isServer } from "solid-js/web";
5+
6+
function serverFnWithNodeBuiltin() {
7+
"use server";
8+
9+
return join('can','externalize');
10+
}
11+
12+
export default function App() {
13+
const [output, setOutput] = createSignal<{ serverFnWithNodeBuiltin?: string }>({});
14+
15+
16+
17+
createEffect(async () => {
18+
const restult = await serverFnWithNodeBuiltin();
19+
setOutput(prev => ({ ...prev, serverFnWithNodeBuiltin: restult }));
20+
});
21+
22+
return (
23+
<main>
24+
<span id="server-fn-test">{JSON.stringify(output())}</span>
25+
</main>
26+
);
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import _ from "lodash";
2+
import { join } from 'path';
3+
import { createEffect, createSignal } from "solid-js";
4+
import { isServer } from "solid-js/web";
5+
6+
function serverFnWithNpmModule() {
7+
"use server";
8+
9+
return _.map([1, 2, 3], x => x * 2);
10+
}
11+
12+
export default function App() {
13+
const [output, setOutput] = createSignal<{ serverFnWithNpmModule?: number[] }>({});
14+
15+
createEffect(async () => {
16+
const restult = await serverFnWithNpmModule();
17+
setOutput(prev => ({ ...prev, serverFnWithNpmModule: restult }));
18+
});
19+
20+
return (
21+
<main>
22+
<span id="server-fn-test">{JSON.stringify(output())}</span>
23+
</main>
24+
);
25+
}

0 commit comments

Comments
 (0)