Skip to content

Commit d260b55

Browse files
authored
test(server-functions): add module level directive tests (#1739)
1 parent 5a166a4 commit d260b55

12 files changed

Lines changed: 107 additions & 20 deletions

tests/server-function/cypress/e2e/server-function.cy.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,29 @@ describe("server-function", () => {
33
cy.visit("/");
44
cy.get("#server-fn-test").contains('{"clientWithIsServer":false}');
55
})
6-
it("should have isServer true in the server function", () => {
7-
cy.visit("/is-server");
6+
it("should have isServer true in the server function - nested", () => {
7+
cy.visit("/is-server-nested");
88
cy.get("#server-fn-test").contains('{"serverFnWithIsServer":true}');
99
})
10-
it("should externalize node builtin in server function", () => {
11-
cy.visit("/node-builtin");
10+
it("should externalize node builtin in server function - nested", () => {
11+
cy.visit("/node-builtin-nested");
1212
cy.get("#server-fn-test").contains('{"serverFnWithNodeBuiltin":"can/externalize"}');
1313
})
14-
it("should externalize npm module in server function", () => {
15-
cy.visit("npm-module");
14+
it("should externalize npm module in server function - nested", () => {
15+
cy.visit("npm-module-nested");
16+
cy.get("#server-fn-test").contains('{"serverFnWithNpmModule":[2,4,6]}');
17+
})
18+
19+
it("should have isServer true in the server function - toplevel", () => {
20+
cy.visit("/is-server-toplevel");
21+
cy.get("#server-fn-test").contains('{"serverFnWithIsServer":true}');
22+
})
23+
it("should externalize node builtin in server function - toplevel", () => {
24+
cy.visit("/node-builtin-toplevel");
25+
cy.get("#server-fn-test").contains('{"serverFnWithNodeBuiltin":"can/externalize"}');
26+
})
27+
it("should externalize npm module in server function - toplevel", () => {
28+
cy.visit("npm-module-toplevel");
1629
cy.get("#server-fn-test").contains('{"serverFnWithNpmModule":[2,4,6]}');
1730
})
1831
});

tests/server-function/src/app.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ export default function App() {
1010
root={props => (
1111
<MetaProvider>
1212
<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>
13+
<ul>
14+
<li><a href="/">Client</a></li>
15+
<li><a href="/is-server-nested">isserver (nested)</a></li>
16+
<li><a href="/is-server-toplevel">isserver (toplevel)</a></li>
17+
<li><a href="/node-builtin-nested">node builtin (nested)</a></li>
18+
<li><a href="/node-builtin-toplevel">node builtin (toplevel)</a></li>
19+
<li><a href="/npm-module-nested">npm module (lodash) (nested)</a></li>
20+
<li><a href="/npm-module-toplevel">npm module (lodash) (toplevel)</a></li>
21+
</ul>
1722
<Suspense>{props.children}</Suspense>
1823
</MetaProvider>
1924
)}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"use server";
2+
3+
import { isServer } from "solid-js/web";
4+
5+
export function serverFnWithIsServer() {
6+
return isServer;
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use server";
2+
3+
import { join } from 'node:path';
4+
5+
export function serverFnWithNodeBuiltin() {
6+
7+
return join('can','externalize');
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use server";
2+
3+
import _ from 'lodash';
4+
5+
export function serverFnWithNpmModule() {
6+
7+
return _.map([1, 2, 3], x => x * 2);
8+
}

tests/server-function/src/routes/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import _ from "lodash";
2-
import { join } from 'path';
3-
import { createEffect, createSignal } from "solid-js";
1+
import { createSignal } from "solid-js";
42
import { isServer } from "solid-js/web";
53

64
export default function App() {

tests/server-function/src/routes/is-server.tsx renamed to tests/server-function/src/routes/is-server-nested.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import _ from "lodash";
2-
import { join } from 'path';
31
import { createEffect, createSignal } from "solid-js";
42
import { isServer } from "solid-js/web";
53

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { createEffect, createSignal } from "solid-js";
2+
import { serverFnWithIsServer } from "~/functions/use-is-server";
3+
4+
export default function App() {
5+
const [output, setOutput] = createSignal<{ serverFnWithIsServer?: boolean }>({});
6+
7+
8+
createEffect(async () => {
9+
const restult = await serverFnWithIsServer();
10+
setOutput(prev => ({ ...prev, serverFnWithIsServer: restult }));
11+
});
12+
13+
14+
return (
15+
<main>
16+
<span id="server-fn-test">{JSON.stringify(output())}</span>
17+
</main>
18+
);
19+
}

tests/server-function/src/routes/node-builtin.tsx renamed to tests/server-function/src/routes/node-builtin-nested.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import _ from "lodash";
2-
import { join } from 'path';
1+
import { join } from 'node:path';
32
import { createEffect, createSignal } from "solid-js";
4-
import { isServer } from "solid-js/web";
53

64
function serverFnWithNodeBuiltin() {
75
"use server";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import { createEffect, createSignal } from "solid-js";
3+
import { serverFnWithNodeBuiltin } from "~/functions/use-node-builtin";
4+
5+
export default function App() {
6+
const [output, setOutput] = createSignal<{ serverFnWithNodeBuiltin?: string }>({});
7+
8+
createEffect(async () => {
9+
const restult = await serverFnWithNodeBuiltin();
10+
setOutput(prev => ({ ...prev, serverFnWithNodeBuiltin: restult }));
11+
});
12+
13+
return (
14+
<main>
15+
<span id="server-fn-test">{JSON.stringify(output())}</span>
16+
</main>
17+
);
18+
}

0 commit comments

Comments
 (0)