-
I need to test a compiled JavaScript SDK file using Browser mode. I thought the first step would be to load the library from the disk to make it available for the tests. I used the Files Handling Built-in Commands. So I created a helper file with code like this: // helper.js
import { server } from "@vitest/browser/context";
const { readFile } = server.commands;
export default async (config) => {
const src = await readFile("../../../../dist/alloy.js");
const s = document.createElement("script");
s.textContent = src;
document.body.appendChild(s);
return window.alloy;
}; But then I saw that the path is resolved relative to the test file and not the helper file. If I move a test file to a different folder (with a different depth), the test will start failing because the SDK file won't be found on the disk anymore. Is there a better way to load this library inside the test than using relative paths. Any ideas? Thank you in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think you can pass an absolute path based on // helper.js
import { server } from "@vitest/browser/context";
await server.commands.readFile(server.config.root + "/dist/alloy.js"); |
Beta Was this translation helpful? Give feedback.
-
We can rethink how to resolve the import, maybe being relative to root is a better default. |
Beta Was this translation helpful? Give feedback.
I think you can pass an absolute path based on
config.root