Skip to content

Commit

Permalink
test(server-functions): add module level directive tests (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
birkskyum authored Jan 28, 2025
1 parent 5a166a4 commit d260b55
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 20 deletions.
25 changes: 19 additions & 6 deletions tests/server-function/cypress/e2e/server-function.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ describe("server-function", () => {
cy.visit("/");
cy.get("#server-fn-test").contains('{"clientWithIsServer":false}');
})
it("should have isServer true in the server function", () => {
cy.visit("/is-server");
it("should have isServer true in the server function - nested", () => {
cy.visit("/is-server-nested");
cy.get("#server-fn-test").contains('{"serverFnWithIsServer":true}');
})
it("should externalize node builtin in server function", () => {
cy.visit("/node-builtin");
it("should externalize node builtin in server function - nested", () => {
cy.visit("/node-builtin-nested");
cy.get("#server-fn-test").contains('{"serverFnWithNodeBuiltin":"can/externalize"}');
})
it("should externalize npm module in server function", () => {
cy.visit("npm-module");
it("should externalize npm module in server function - nested", () => {
cy.visit("npm-module-nested");
cy.get("#server-fn-test").contains('{"serverFnWithNpmModule":[2,4,6]}');
})

it("should have isServer true in the server function - toplevel", () => {
cy.visit("/is-server-toplevel");
cy.get("#server-fn-test").contains('{"serverFnWithIsServer":true}');
})
it("should externalize node builtin in server function - toplevel", () => {
cy.visit("/node-builtin-toplevel");
cy.get("#server-fn-test").contains('{"serverFnWithNodeBuiltin":"can/externalize"}');
})
it("should externalize npm module in server function - toplevel", () => {
cy.visit("npm-module-toplevel");
cy.get("#server-fn-test").contains('{"serverFnWithNpmModule":[2,4,6]}');
})
});
13 changes: 9 additions & 4 deletions tests/server-function/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ export default function App() {
root={props => (
<MetaProvider>
<Title>SolidStart - Basic</Title>
<a href="/">Client</a>
<a href="/is-server">isserver</a>
<a href="/node-builtin">node builtin</a>
<a href="/npm-module">npm module (lodash)</a>
<ul>
<li><a href="/">Client</a></li>
<li><a href="/is-server-nested">isserver (nested)</a></li>
<li><a href="/is-server-toplevel">isserver (toplevel)</a></li>
<li><a href="/node-builtin-nested">node builtin (nested)</a></li>
<li><a href="/node-builtin-toplevel">node builtin (toplevel)</a></li>
<li><a href="/npm-module-nested">npm module (lodash) (nested)</a></li>
<li><a href="/npm-module-toplevel">npm module (lodash) (toplevel)</a></li>
</ul>
<Suspense>{props.children}</Suspense>
</MetaProvider>
)}
Expand Down
7 changes: 7 additions & 0 deletions tests/server-function/src/functions/use-is-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use server";

import { isServer } from "solid-js/web";

export function serverFnWithIsServer() {
return isServer;
}
8 changes: 8 additions & 0 deletions tests/server-function/src/functions/use-node-builtin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use server";

import { join } from 'node:path';

export function serverFnWithNodeBuiltin() {

return join('can','externalize');
}
8 changes: 8 additions & 0 deletions tests/server-function/src/functions/use-npm-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use server";

import _ from 'lodash';

export function serverFnWithNpmModule() {

return _.map([1, 2, 3], x => x * 2);
}
4 changes: 1 addition & 3 deletions tests/server-function/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import _ from "lodash";
import { join } from 'path';
import { createEffect, createSignal } from "solid-js";
import { createSignal } from "solid-js";
import { isServer } from "solid-js/web";

export default function App() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from "lodash";
import { join } from 'path';
import { createEffect, createSignal } from "solid-js";
import { isServer } from "solid-js/web";

Expand Down
19 changes: 19 additions & 0 deletions tests/server-function/src/routes/is-server-toplevel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createEffect, createSignal } from "solid-js";
import { serverFnWithIsServer } from "~/functions/use-is-server";

export default function App() {
const [output, setOutput] = createSignal<{ serverFnWithIsServer?: boolean }>({});


createEffect(async () => {
const restult = await serverFnWithIsServer();
setOutput(prev => ({ ...prev, serverFnWithIsServer: restult }));
});


return (
<main>
<span id="server-fn-test">{JSON.stringify(output())}</span>
</main>
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _ from "lodash";
import { join } from 'path';
import { join } from 'node:path';
import { createEffect, createSignal } from "solid-js";
import { isServer } from "solid-js/web";

function serverFnWithNodeBuiltin() {
"use server";
Expand Down
18 changes: 18 additions & 0 deletions tests/server-function/src/routes/node-builtin-toplevel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

import { createEffect, createSignal } from "solid-js";
import { serverFnWithNodeBuiltin } from "~/functions/use-node-builtin";

export default function App() {
const [output, setOutput] = createSignal<{ serverFnWithNodeBuiltin?: string }>({});

createEffect(async () => {
const restult = await serverFnWithNodeBuiltin();
setOutput(prev => ({ ...prev, serverFnWithNodeBuiltin: restult }));
});

return (
<main>
<span id="server-fn-test">{JSON.stringify(output())}</span>
</main>
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _ from "lodash";
import { join } from 'path';
import { createEffect, createSignal } from "solid-js";
import { isServer } from "solid-js/web";

function serverFnWithNpmModule() {
"use server";
Expand Down
17 changes: 17 additions & 0 deletions tests/server-function/src/routes/npm-module-toplevel.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createEffect, createSignal } from "solid-js";
import { serverFnWithNpmModule } from "~/functions/use-npm-module";

export default function App() {
const [output, setOutput] = createSignal<{ serverFnWithNpmModule?: number[] }>({});

createEffect(async () => {
const restult = await serverFnWithNpmModule();
setOutput(prev => ({ ...prev, serverFnWithNpmModule: restult }));
});

return (
<main>
<span id="server-fn-test">{JSON.stringify(output())}</span>
</main>
);
}

0 comments on commit d260b55

Please sign in to comment.