Skip to content

Commit 5cdca06

Browse files
[Vitest Integration] document known issues with isolated storage (#20033)
Co-authored-by: ToriLindsay <[email protected]>
1 parent 089312f commit 5cdca06

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/content/docs/workers/testing/vitest-integration/known-issues.mdx

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,48 @@ Dynamic `import()` statements do not work inside `export default { ... }` handle
2626

2727
Durable Object alarms are not reset between test runs and do not respect isolated storage. Ensure you delete or run all alarms with [`runDurableObjectAlarm()`](/workers/testing/vitest-integration/test-apis/#durable-objects) scheduled in each test before finishing the test.
2828

29-
### Durable Objects and `isolatedStorage`
29+
### WebSockets
3030

3131
Using WebSockets with Durable Objects with the [`isolatedStorage`](/workers/testing/vitest-integration/isolation-and-concurrency) flag turned on is not supported. You must set `isolatedStorage: false` in your `vitest.config.ts` file.
3232

33-
### Returning non-primitive values from RPC methods
33+
### Isolated storage
3434

35-
In order to return non-primitive values (objects or classes extending `RpcTarget`, for instance) from RPC methods, you must use the `using` keyword. Refer to [https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle#explicit-resource-management](https://developers.cloudflare.com/workers/runtime-apis/rpc/lifecycle#explicit-resource-management) for more details. An [example test](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/rpc/test/unit.test.ts#L155) is included in the `workers-sdk` repository.
35+
When the `isolatedStorage` flag is enabled (the default), the test runner will undo any writes to the storage at the end of the test as detailed in the [isolation and concurrency documentation](/workers/testing/vitest-integration/isolation-and-concurrency/). However, Cloudflare recommends that you consider the following actions to avoid any common issues:
36+
37+
## Await all storage operations
38+
39+
Always `await` all `Promise`s that read or write to storage services.
40+
41+
```ts
42+
// Example: Seed data
43+
beforeAll(async () => {
44+
await env.KV.put('message', 'test message');
45+
await env.R2.put('file', 'hello-world');
46+
});
47+
```
48+
49+
## Explicitly signal resource disposal
50+
51+
When calling RPC methods of a Service Worker or Durable Object that return non-primitive values (such as objects or classes extending `RpcTarget`), use the `using` keyword to explicitly signal when resources can be disposed of. See [this example test](https://github.com/cloudflare/workers-sdk/tree/main/fixtures/vitest-pool-workers-examples/rpc/test/unit.test.ts#L155) and refer to [explicit-resource-management](/workers/runtime-apis/rpc/lifecycle#explicit-resource-management) for more details.
52+
53+
```ts
54+
using result = await stub.getCounter();
55+
```
56+
57+
## Consume response bodies
58+
59+
When making requests via `fetch` or `R2.get()`, consume the entire response body, even if you are not asserting its content. For example:
60+
61+
```ts
62+
test('check if file exists', async () => {
63+
await env.R2.put('file', 'hello-world');
64+
const response = await env.R2.get('file');
65+
66+
expect(response).not.toBe(null);
67+
// Consume the response body even if you are not asserting it
68+
await response.text()
69+
});
70+
```
3671

3772
### Module resolution
3873

0 commit comments

Comments
 (0)