Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Typescript testing docs (#476)
Browse files Browse the repository at this point in the history
* Add Typescript testing docs

* Review comments
jackkleeman committed Nov 4, 2024
1 parent 65eff6b commit beb3a90
Showing 7 changed files with 2,994 additions and 132 deletions.
2,999 changes: 2,877 additions & 122 deletions code_snippets/ts/package-lock.json

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions code_snippets/ts/package.json
Original file line number Diff line number Diff line change
@@ -12,23 +12,26 @@
"app": "node ./dist/app.js",
"app-dev": "ts-node-dev --watch src --respawn --transpile-only ./src/app.ts",
"lint": "eslint --ignore-path .eslintignore --ext .ts .",
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|ts|json)\""
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|ts|json)\"",
"test": "vitest run"
},
"dependencies": {
"@restatedev/restate-cdk": "^1.0.0",
"@restatedev/restate-sdk": "^1.3.0",
"@restatedev/restate-sdk-clients": "^1.3.0",
"@restatedev/restate-sdk": "^1.4.0",
"@restatedev/restate-sdk-clients": "^1.4.0",
"@restatedev/restate-sdk-testcontainers": "^1.4.0",
"express": "^4.19.2"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.12.7",
"esbuild": "^0.18.12",
"ts-node-dev": "^1.1.1",
"typescript": "^5.0.2",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"esbuild": "^0.18.12",
"eslint": "^8.35.0",
"prettier": "^2.8.4"
"prettier": "^2.8.4",
"ts-node-dev": "^1.1.1",
"typescript": "^5.0.2",
"vitest": "^2.1.4"
}
}
2 changes: 1 addition & 1 deletion code_snippets/ts/src/develop/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as restate from "@restatedev/restate-sdk";

const router = restate.object({
export const router = restate.object({
name: "State",
handlers: {
greet: async (ctx: restate.ObjectContext, name: string) => {
63 changes: 63 additions & 0 deletions code_snippets/ts/src/develop/testing.test.ts
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>
});
2 changes: 1 addition & 1 deletion docs/develop/ts/clients.mdx
Original file line number Diff line number Diff line change
@@ -106,4 +106,4 @@ CODE_LOAD::ts/src/develop/clients/ingress.ts#service_attach

<Admonition type={"info"} title={"Attaching to workflows"}>
[Have a look at the workflow documentation to learn how to attach to workflow executions.](/develop/ts/workflows#submitting-workflows-with-sdk-clients)
</Admonition>
</Admonition>
2 changes: 1 addition & 1 deletion docs/develop/ts/logging.mdx
Original file line number Diff line number Diff line change
@@ -22,4 +22,4 @@ To avoid this, you can use the Restate context logger, which is a wrapper around
CODE_LOAD::ts/src/operate/monitoring.ts
```

This logger uses the same level filtering as described above.
This logger uses the same level filtering as described above.
41 changes: 41 additions & 0 deletions docs/develop/ts/testing.mdx
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
```

0 comments on commit beb3a90

Please sign in to comment.