-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
vitestRelating to the Workers Vitest integrationRelating to the Workers Vitest integration
Description
Prior to @cloudflare/vitest-pool-workers v0.12.x we could import files verbatim like this, via a Vite feature:
import dropTables from "../fixtures/drop-all-tables-conditionally.sql?raw";
import migration001 from "../../migrations/0001_foo_schema.sql?raw";
import migration002 from "../../migrations/0002_bar_tables.sql?raw";The above no longer works.
Here is a workaround:
// Note that it depends on `sqlPlugin` in your vitest config.ts to handle .sql imports.
const fixtureModules = import.meta.glob("../fixtures/*.sql", {
import: "default",
eager: true,
});
const dropTables = fixtureModules[
"../fixtures/drop-all-tables-conditionally.sql"
] as string;In order for this to work, add this to the relevant vitest config file (e.g. vitest.integration.config.ts):
// This plugin allows importing .sql files as raw strings,
// something that is no longer supported via ?raw suffix since
// @cloudflare/vitest-pool-workers v0.12.x.
//
// See tests/api/setup.ts for usage
function sqlPlugin() {
return {
name: "vite-plugin-sql",
transform(content: string, id: string) {
if (id.endsWith(".sql")) {
return {
code: `export default ${JSON.stringify(content)};`,
map: null,
};
}
},
};
}
export default defineWorkersConfig({
// Use the plugin in this vitest configuration
plugins: [sqlPlugin()],
// ...
});Metadata
Metadata
Assignees
Labels
vitestRelating to the Workers Vitest integrationRelating to the Workers Vitest integration
Type
Projects
Status
Done