Skip to content

Commit

Permalink
Create a unit test that launches the worker and run the app
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed Jun 21, 2024
1 parent 8332f94 commit f61bce7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
70 changes: 70 additions & 0 deletions packages/kernel/src/worker-runtime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import path from "node:path";
import { test } from "vitest";
import { startWorkerEnv, PostMessageFn } from "./worker-runtime";
import stliteServerWheelUrl from "stlite_server.whl"; // This is the alias from vitest.config.ts
import streamlitWheelUrl from "streamlit.whl"; // This is the alias from vitest.config.ts

const pyodideUrl = path.resolve("../../node_modules/pyodide/pyodide.mjs"); // Installed at the Yarn workspace root;

function getWheelInstallPath(wheelImportUrl: string): string {
// `wheelImportUrl` is like `/path/to/stlite_server.whl` that is a URL path.
// We need to convert it to a local file path so that it can be referred to in the test environment i.e. Node.js.
// Also, we need to add `file://` scheme to it so that `micropip.install()` can install it.
return "file://" + path.resolve("." + wheelImportUrl);
}

test(
"E2E test running an app",
async () => {
console.log({
stliteServerWheelUrl,
streamlitWheelUrl,
});
const loadedPromise = new Promise<void>((resolve, reject) => {
const postMessage: PostMessageFn = (message) => {
if (message.type === "event:loaded") {
resolve();
} else if (message.type === "event:error") {
reject(message.data.error);
}
};
const onMessage = startWorkerEnv(pyodideUrl, postMessage);
onMessage(
new MessageEvent("message", {
data: {
type: "initData",
data: {
files: {
"app.py": {
data: `
import streamlit as st
st.title("Hello World")
`,
},
},
entrypoint: "app.py",
wheels: {
stliteServer: getWheelInstallPath(
stliteServerWheelUrl as unknown as string,
),
streamlit: getWheelInstallPath(
streamlitWheelUrl as unknown as string,
),
},
archives: [],
requirements: [],
moduleAutoLoad: false,
prebuiltPackageNames: [],
},
},
}),
);
});

await loadedPromise;
},
{
timeout: 60000,
},
);
14 changes: 14 additions & 0 deletions packages/kernel/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
/// <reference types="vitest" />

import { defineConfig } from "vite";
import path from "node:path";

export default defineConfig({
test: {
environment: "jsdom", // We use jsdom because happy-dom does not work well with iframe.
setupFiles: ["./setupTests.ts"],
},
resolve: {
alias: {
"stlite_server.whl": path.resolve(
__dirname,
"./py/stlite-server/dist/stlite_server-0.1.0-py3-none-any.whl",
),
"streamlit.whl": path.resolve(
__dirname,
"./py/streamlit/lib/dist/streamlit-1.35.0-cp312-none-any.whl",
),
},
},
assetsInclude: ["**/*.whl"],
});

0 comments on commit f61bce7

Please sign in to comment.