Skip to content

Commit fbe1d28

Browse files
committed
testing
1 parent 1036423 commit fbe1d28

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

src/content/docs/agents/api-reference/sdk.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Your own methods can access the Agent's environment variables and bindings on `t
112112

113113
You can create and run an instance of an Agent directly from a Worker in one of three ways:
114114

115-
1. Using the `routeAgentRequest` helper: this will automatically map requests to an individual Agent based on the `/agents/:agent/:name` URL pattern.
115+
1. Using the `routeAgentRequest` helper: this will automatically map requests to an individual Agent based on the `/agents/:agent/:name` URL pattern. The value of `:agent` will be the name of your Agent class converted to `kebab-case`, and the value of `:name` will be the name of the Agent instance you want to create or retrieve.
116116
2. Calling `getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
117117
3. The [Durable Objects stub API](/durable-objects/api/id/), which provides a lower level API for creating and retrieving Agents.
118118

src/content/docs/agents/getting-started/testing-your-agent.mdx

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,137 @@ sidebar:
88

99
import { Render, PackageManagers, WranglerConfig } from "~/components"
1010

11-
TODO
11+
Because Agents run on Cloudflare Workers and Durable Objects, they can be tested using the same tools and techniques as Workers and Durable Objects.
12+
13+
## Writing and runnning tests
14+
15+
### Setup
16+
17+
:::note
18+
19+
The `@cloudflare/agents-starter` template and new Cloudflare Workers projects already include the relevant `vitest` and `@cloudflare/vitest-pool-workers` packages, as well as a valid `vitest.config.js` file.
20+
21+
:::
22+
23+
Before you write your first test, install the necessary packages:
24+
25+
```sh
26+
npm install [email protected] --save-dev --save-exact
27+
npm install @cloudflare/vitest-pool-workers --save-dev
28+
```
29+
30+
Ensure that your `vitest.config.js` file is identical to the following:
31+
32+
```js
33+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
34+
35+
export default defineWorkersConfig({
36+
test: {
37+
poolOptions: {
38+
workers: {
39+
wrangler: { configPath: "./wrangler.toml" },
40+
},
41+
},
42+
},
43+
});
44+
```
45+
46+
### Add the Agent configuration
47+
48+
Add a `durableObjects` configuration to `vitest.config.js` with the name of your Agent class:
49+
50+
```js
51+
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
52+
53+
export default defineWorkersConfig({
54+
test: {
55+
poolOptions: {
56+
workers: {
57+
main: './src/index.ts',
58+
miniflare: {
59+
durableObjects: {
60+
NAME: 'MyAgent',
61+
},
62+
},
63+
},
64+
},
65+
},
66+
});
67+
```
68+
69+
### Write a test
70+
71+
:::note
72+
73+
Review the [Vitest documentation](https://vitest.dev/) for more information on testing, including the test API reference and advanced testing techniques.
74+
75+
:::
76+
77+
Tests use the `vitest` framework. A basic test suite for your Agent can validate how your Agent responds to requests, but can also unit test your Agent's methods and state.
78+
79+
```ts
80+
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
81+
import { describe, it, expect } from 'vitest';
82+
import worker from '../src';
83+
import { Env } from '../src';
84+
85+
interface ProvidedEnv extends Env {}
86+
87+
describe('make a request to my Agent', () => {
88+
// Unit testing approach
89+
it('responds with state', async () => {
90+
// Provide a valid URL that your Worker can use to route to your Agent
91+
// If you are using routeAgentRequest, this will be /agent/:agent/:name
92+
const request = new Request<unknown, IncomingRequestCfProperties>('http://example.com/agent/my-agent/agent-123');
93+
const ctx = createExecutionContext();
94+
const response = await worker.fetch(request, env, ctx);
95+
await waitOnExecutionContext(ctx);
96+
expect(await response.text()).toMatchObject({ hello: 'from your agent' });
97+
});
98+
99+
it('also responds with state', async () => {
100+
const request = new Request('http://example.com/agent/my-agent/agent-123');
101+
const response = await SELF.fetch(request);
102+
expect(await response.text()).toMatchObject({ hello: 'from your agent' });
103+
});
104+
});
105+
```
106+
107+
### Run tests
108+
109+
Running tests is done using the `vitest` CLI:
110+
111+
```sh
112+
$ npm run test
113+
# or run vitest directly
114+
$ npx vitest
115+
```
116+
```sh output
117+
MyAgent
118+
✓ should return a greeting (1 ms)
119+
120+
Test Files 1 passed (1)
121+
```
122+
123+
Review the [documentation on testing](/workers/testing/vitest-integration/get-started/write-your-first-test/) for additional examples and test configuration.
124+
125+
## Running Agents locally
126+
127+
You can also run an Agent locally using the `wrangler` CLI:
128+
129+
```sh
130+
$ npx wrangler dev
131+
```
132+
```sh output
133+
Your Worker and resources are simulated locally via Miniflare. For more information, see: https://developers.cloudflare.com/workers/testing/local-development.
134+
135+
Your worker has access to the following bindings:
136+
- Durable Objects:
137+
- MyAgent: MyAgent
138+
Starting local server...
139+
[wrangler:inf] Ready on http://localhost:53645
140+
```
141+
142+
This spins up a local development server that runs the same runtime as Cloudflare Workers, and allows you to iterate on your Agent's code and test it locally without deploying it.
143+
144+
Visit the [`wrangler dev`](https://developers.cloudflare.com/workers/wrangler/commands/#dev) docs to review the CLI flags and configuration options.

src/content/docs/agents/getting-started/writing-your-first-agent.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66

77
---
88

9-
import { Render, PackageManagers, WranglerConfig } from "~/components"
9+
import { Render, PackageManagers, TypeScriptExample, WranglerConfig } from "~/components"
1010

1111
:::note
1212

0 commit comments

Comments
 (0)