How can I make miniflare persit R2 data on local in testing?

I have a worker and it need to access R2. I want to run test locally and make R2 load files from local dir. are there any options I can add here?
export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
miniflare: {
r2Buckets: {
MY_BUCKET: 'sing-box-config',
// 'sing-box-config': 'MY_BUCKET',
},
},
},
},
},
});
export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.toml' },
miniflare: {
r2Buckets: {
MY_BUCKET: 'sing-box-config',
// 'sing-box-config': 'MY_BUCKET',
},
},
},
},
},
});
3 Replies
MrBBot
MrBBot•9mo ago
Hey! 👋 Unfortunately, this isn't possible at the moment. This was a deliberate choice to ensure that tests are reproducible, as they always start with an empty environment. If you'd like to seed data before running your tests, I'd encourage you to configure VItest setupFiles (https://vitest.dev/config/#setupfiles) with something like...
// vitest.config.ts
export default defineWorkersProject({
test: {
setupFiles: ["./test/seed-data.ts"],
poolOptions: { ... },
},
});
// vitest.config.ts
export default defineWorkersProject({
test: {
setupFiles: ["./test/seed-data.ts"],
poolOptions: { ... },
},
});
// test/seed-data.ts
import { env } from "cloudflare:test";

await env.MY_BUCKET.put("key", "value");
// ...
// test/seed-data.ts
import { env } from "cloudflare:test";

await env.MY_BUCKET.put("key", "value");
// ...
Arisu 0x1bf52
Arisu 0x1bf52OP•9mo ago
well I will have a try. and which is bucket binding which is bucket name in r2Buckets? BINDING: name or name:BINDING
MrBBot
MrBBot•9mo ago
BINDING: name 🙂
Want results from more Discord servers?
Add your server