-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a unit test that launches the worker and run the app
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
}); |