Unit/Integration Testing WebSockets
Hi, ๐
Does anyone have a good reference for unit/integration testing websockets with durable objects? I have found examples/documents for request/response, but I haven't found a good example using websockets.
7 Replies
Marak's answer is great for testing against a live test server. Were you looking for more of an in-process unit/integration test approach?
Yes, and my apologies for cross posting, I realized that I was looking for a solution using vitest. So I posted there as well
Using SELF.fetch from cloudflare:test is clear, but I am unclear as to how I could do the same with a websocket.
I wish I had a well documented library with a blog post that described how to use it, but all I have is my own rather messy way of doing it in my current big project.
My test harness and details are specific to my use case which is an MCP server library (named Lumenize). I should probably make the testing part more generic but rather than have you wait for me to do that, or not, hopefully, you can get some value out of this by just removing the MCP-specific stuff and a little bit of adaptation.
Here is how I run an in-process integration test: [https://github.com/lumenize/lumenize/blob/main/lumenize-monolith/test/test-utils.ts#L376-L426]
Take a look here for how I mock the environment: [https://github.com/lumenize/lumenize/blob/main/lumenize-monolith/test/test-utils.ts#L198-L374]
Here is the smallest example test file using the above. It tests that the code assigns the correct connection tags: [https://github.com/lumenize/lumenize/blob/main/lumenize-monolith/test/integration-connection-tags.test.ts]
Here is a full MCP lifecycle test (huge, but uses all of the mock and
runTestInLumenize
capabilities): [https://github.com/lumenize/lumenize/blob/main/lumenize-monolith/test/integration-entity-lifecycle.test.ts]
You can also look at how I've configured vitest in that repo.
If those help, I will split them out into a library and write up a blog post for others to use. Let me know.GitHub
lumenize/lumenize-monolith/test/test-utils.ts at main ยท lumenize/l...
Lumenize MCP-first backend as a service. Contribute to lumenize/lumenize development by creating an account on GitHub.
Ohh, I almost forgot. This is key. I monkey patch WebSocket that is provided by the vitest environment to proxy the request directly to the in-process running DO so nothing goes out to localhost. Here's where I do that: [https://github.com/lumenize/lumenize/blob/main/lumenize-monolith/test/test-utils.ts#L510-L631]
GitHub
lumenize/lumenize-monolith/test/test-utils.ts at main ยท lumenize/l...
Lumenize MCP-first backend as a service. Contribute to lumenize/lumenize development by creating an account on GitHub.
Note, there are two different WebSocket monkey patches. The one above is for in-process integration testing. The other one is for live tests against a local running server. The only reason I need the second monkey patch is so I can more conveniently inject cookies that look like http-only cookies.
Thanks @Larry this is incredible. I will try and groc what you have. Maybe one day it can move upstream into the cloudflare:test module
If you like it, I will put together a pull request to get it into cloudflare:test