-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add Typescript testing docs (#476)
* Add Typescript testing docs * Review comments
1 parent
65eff6b
commit beb3a90
Showing
7 changed files
with
2,994 additions
and
132 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { RestateTestEnvironment } from "@restatedev/restate-sdk-testcontainers"; | ||
import { router } from "./state.js"; | ||
import * as clients from "@restatedev/restate-sdk-clients"; | ||
import { describe, it, beforeAll, afterAll, expect } from "vitest"; | ||
|
||
// <start_setup> | ||
describe("ExampleObject", () => { | ||
// import { RestateTestEnvironment } from "@restatedev/restate-sdk-testcontainers"; | ||
let restateTestEnvironment: RestateTestEnvironment; | ||
// import * as clients from "@restatedev/restate-sdk-clients"; | ||
let restateIngress: clients.Ingress; | ||
|
||
beforeAll(async () => { | ||
restateTestEnvironment = await RestateTestEnvironment.start( | ||
(restateServer) => restateServer.bind(router) | ||
); | ||
restateIngress = clients.connect({ url: restateTestEnvironment.baseUrl() }); | ||
}, 20_000); | ||
|
||
afterAll(async () => { | ||
if (restateTestEnvironment !== undefined) { | ||
await restateTestEnvironment.stop(); | ||
} | ||
}); | ||
// <end_setup> | ||
|
||
// <start_methods> | ||
it("Can call methods", async () => { | ||
const client = restateIngress.objectClient(router, "myKey"); | ||
|
||
await client.greet("Test!"); | ||
}); | ||
// <end_methods> | ||
|
||
// <start_state> | ||
it("Can read state", async () => { | ||
const state = restateTestEnvironment.stateOf(router, "myKey"); | ||
|
||
expect(await state.getAll()).toStrictEqual({}); | ||
expect(await state.get("count")).toBeNull(); | ||
}); | ||
|
||
it("Can write state", async () => { | ||
const state = restateTestEnvironment.stateOf(router, "myKey"); | ||
|
||
await state.setAll({ | ||
count: 123, | ||
}); | ||
await state.set("count", 321); | ||
}); | ||
// <end_state> | ||
|
||
// <start_typedstate> | ||
type ServiceState = { count: number }; | ||
|
||
it("Can operate on typed state", async () => { | ||
const state = restateTestEnvironment.stateOf<ServiceState>(router, "myKey"); | ||
|
||
await state.setAll({ count: 1 }); | ||
await state.set("count", 2); | ||
}); | ||
// <end_typedstate> | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
sidebar_position: 14 | ||
description: "Test your services." | ||
--- | ||
|
||
# Testing | ||
|
||
The Typescript SDK has an companion library which makes it easy to test against a Restate container: | ||
[`@restatedev/restate-sdk-testcontainers`](https://www.npmjs.com/package/@restatedev/restate-sdk-testcontainers). | ||
|
||
## Usage | ||
|
||
### Setup | ||
`RestateTestEnvironment.start` creates a Restate container and executes a user-provided closure to register services. | ||
An optional second argument allows you to specify a custom [Testcontainer](https://node.testcontainers.org/) for Restate. | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/testing.test.ts#setup | ||
``` | ||
|
||
### Calling services | ||
The Restate ingress client can be used as usual (see the [clients documentation](./clients)) | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/testing.test.ts#methods | ||
``` | ||
|
||
### Checking and mutating state | ||
The `stateOf` method on the `RestateTestEnvironment` class can be used to obtain a handle on the Virtual Object / Workflow state | ||
for a particular key. | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/testing.test.ts#state | ||
``` | ||
|
||
### Typed state | ||
`stateOf` can be provided with a type for the services state, to allow for type-safe state operations. | ||
|
||
```typescript | ||
CODE_LOAD::ts/src/develop/testing.test.ts#typedstate | ||
``` |