-
|
Setting up vitest with miniflare can be quite laborious, considering any bindings from the wrangler config need to be redefined in a different syntax. Is there a reason it doesn't just detect the bindings and convert them for you? Currently I have something like this, which seems like I shouldn't have to do myself: import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
import { experimental_readRawConfig } from "wrangler";
const wranglerConfigPath = "./wrangler.jsonc";
const wranglerConfig = experimental_readRawConfig({
config: wranglerConfigPath,
});
export const testEnvironment = "production";
const wranglerEnvConfig = testEnvironment
? wranglerConfig?.rawConfig?.env?.[testEnvironment]
: null;
const getRateLimits = () => {
return (
// @ts-ignore
wranglerEnvConfig.unsafe.bindings
// @ts-ignore
.filter((binding) => binding.type === "ratelimit")
.reduce(
// @ts-ignore
(rateLimits, { name, simple }) => {
// @ts-ignore
rateLimits[name] = { simple };
return rateLimits;
},
{},
)
);
};
const getServiceBindings = () => {
// @ts-ignore
return wranglerEnvConfig.services.reduce((bindings, { binding, service }) => {
// @ts-ignore
return Object.assign({}, bindings, {
[service]: binding,
});
}, {});
};
// @see https://github.com/cloudflare/workers-sdk/blob/main/fixtures/vitest-pool-workers-examples/basics-integration-auxiliary
const getWorkers = () => {
// @ts-ignore
return wranglerEnvConfig.services.map(({ service: name }) => {
// @ts-ignore
const {
compatibility_flags: compatibilityFlags,
compatibility_date: compatibilityDate,
} = wranglerConfig.rawConfig;
return {
name,
scriptPath: "./dist/index.js", // Built by `global-setup.ts`
compatibilityDate,
compatibilityFlags,
};
});
};
const getKvNamespaces = () => {
// @ts-ignore
return wranglerEnvConfig.kv_namespaces.map(({ binding }) => binding);
};
const getAnalyticsDatasets = () => {
// @ts-ignore
return wranglerEnvConfig.analytics_engine_datasets.reduce(
// @ts-ignore
(datasets, { name, dataset }) => {
// @ts-ignore
datasets[name] = { dataset };
return datasets;
},
{},
);
};
export default defineWorkersConfig({
test: {
// globalSetup: ["./test/global-setup.ts"],
poolOptions: {
workers: {
singleWorker: true,
wrangler: {
configPath: wranglerConfigPath,
environment: testEnvironment,
},
miniflare: {
// serviceBindings: getServiceBindings(),
// workers: getWorkers(),
kvNamespaces: getKvNamespaces(),
analyticsEngineDatasets: getAnalyticsDatasets(),
ratelimits: getRateLimits(),
},
},
},
},
}); |
Beta Was this translation helpful? Give feedback.
Answered by
penalosa
Oct 7, 2025
Replies: 1 comment
-
|
You shouldn't need to do any of that—the Vitest integration will read bindings defined in the |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
penalosa
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You shouldn't need to do any of that—the Vitest integration will read bindings defined in the
wrangler.configPathconfig property