You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/sdk.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,7 +112,7 @@ Your own methods can access the Agent's environment variables and bindings on `t
112
112
113
113
You can create and run an instance of an Agent directly from a Worker in one of three ways:
114
114
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.
116
116
2. Calling `getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
117
117
3. The [Durable Objects stub API](/durable-objects/api/id/), which provides a lower level API for creating and retrieving Agents.
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:
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.
expect(awaitresponse.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.
0 commit comments