Worker vitest push test file to R2
Hey, I am writing an implementation that utilises pre-uploaded files to R2. As part of this I want to write integration tests for this that can run locally with a test file. I was looking at having vitest push the file into the local R2 bucket before the tests run, but due to not having fs access I don't believe this is possible. Does anyone have any guidance on how I could go about doing this? The entire codebase relies on a single file existing in the bucket, which is uploaded in production as an external process. Cheers!
2 Replies
The test process should give you direct access to the bindings, including R2, within Node(or whatever runtime you are using for testing). You should then be able to read a file from the file system to be able to start the process
I get access to the R2 bindings, but I believe as the vitest test code runs within workerd, there is no access to the filesystem. When trying to use node:fs I get errors at runtime indicating the import could not be found within vitest
So I couldn't figure out any way around this by reading the file directly within the tests, also passing the file through vitest globalSetup doesn't work due to the implementation of that, as it appears to use a websocket and can't pass the file data across that connection. So I went with running an express server which hosts the static files from the test data directory, and passes through the connection information to vitest using the globalSetup and provide/inject, then the vitest beforeAll connects to that server and pushes the file into the local R2 bucket