@temporal-contract/testing 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +76 -0
- package/config/sql.yaml +6 -0
- package/dist/extension.d.mts +11 -0
- package/dist/extension.mjs +35 -0
- package/dist/global-setup.d.mts +18 -0
- package/dist/global-setup.mjs +70 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Benoit TRAVERS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @temporal-contract/testing
|
|
2
|
+
|
|
3
|
+
Shared testing utilities for temporal-contract integration tests.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Testcontainers Integration**: Automatically starts a Temporal server in a Docker container for integration tests
|
|
8
|
+
- **Vitest Setup**: Pre-configured setup for Vitest with global test lifecycle
|
|
9
|
+
- **Connection Management**: Handles Temporal connection setup and cleanup
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
This package is internal to the monorepo and used by sample projects for integration testing.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add -D @temporal-contract/testing
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### In vitest.config.ts
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { defineConfig } from 'vitest/config';
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
test: {
|
|
28
|
+
globalSetup: './vitest.global-setup.ts',
|
|
29
|
+
testTimeout: 60000,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### In vitest.global-setup.ts
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { setupTemporalTestContainer } from '@temporal-contract/testing';
|
|
38
|
+
|
|
39
|
+
export default setupTemporalTestContainer;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### In your tests
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import { describe, it, expect } from 'vitest';
|
|
46
|
+
import { getTemporalConnection } from '@temporal-contract/testing';
|
|
47
|
+
import { TypedClient } from '@temporal-contract/client';
|
|
48
|
+
|
|
49
|
+
describe('Order Processing Workflow', () => {
|
|
50
|
+
it('should process an order successfully', async () => {
|
|
51
|
+
const connection = await getTemporalConnection();
|
|
52
|
+
const client = TypedClient.create(myContract, { connection });
|
|
53
|
+
|
|
54
|
+
// Your test code here
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API
|
|
60
|
+
|
|
61
|
+
### `setupTemporalTestContainer()`
|
|
62
|
+
|
|
63
|
+
Global setup function that starts a Temporal container before all tests and stops it after all tests.
|
|
64
|
+
|
|
65
|
+
### `getTemporalConnection()`
|
|
66
|
+
|
|
67
|
+
Returns a connection to the Temporal server running in the container.
|
|
68
|
+
|
|
69
|
+
## Requirements
|
|
70
|
+
|
|
71
|
+
- Docker must be running on the host machine
|
|
72
|
+
- Testcontainers requires Docker API access
|
|
73
|
+
|
|
74
|
+
## Environment Variables
|
|
75
|
+
|
|
76
|
+
- `TESTCONTAINERS_RYUK_DISABLED`: Set to `true` to disable Ryuk container (useful in CI)
|
package/config/sql.yaml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Connection } from "@temporalio/client";
|
|
2
|
+
import { NativeConnection } from "@temporalio/worker/lib/connection.js";
|
|
3
|
+
import * as vitest0 from "vitest";
|
|
4
|
+
|
|
5
|
+
//#region src/extension.d.ts
|
|
6
|
+
declare const it: vitest0.TestAPI<{
|
|
7
|
+
clientConnection: Connection;
|
|
8
|
+
workerConnection: NativeConnection;
|
|
9
|
+
}>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { it };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Connection } from "@temporalio/client";
|
|
2
|
+
import { NativeConnection } from "@temporalio/worker/lib/connection.js";
|
|
3
|
+
import { inject, it as it$1 } from "vitest";
|
|
4
|
+
|
|
5
|
+
//#region src/extension.ts
|
|
6
|
+
const it = it$1.extend({
|
|
7
|
+
clientConnection: async ({}, use) => {
|
|
8
|
+
const connection = await getTemporalConnection();
|
|
9
|
+
await use(connection);
|
|
10
|
+
await connection.close();
|
|
11
|
+
},
|
|
12
|
+
workerConnection: async ({}, use) => {
|
|
13
|
+
await use(await getTemporalWorkerConnection());
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Get a connection to the Temporal server (for client)
|
|
18
|
+
* Must be called after setupTemporalTestContainer has been executed
|
|
19
|
+
*/
|
|
20
|
+
function getTemporalConnection() {
|
|
21
|
+
return Connection.connect({ address: getTemporalAddress() });
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get a native connection to the Temporal server (for worker)
|
|
25
|
+
* Must be called after setupTemporalTestContainer has been executed
|
|
26
|
+
*/
|
|
27
|
+
function getTemporalWorkerConnection() {
|
|
28
|
+
return NativeConnection.connect({ address: getTemporalAddress() });
|
|
29
|
+
}
|
|
30
|
+
function getTemporalAddress() {
|
|
31
|
+
return `${inject("__TESTCONTAINERS_TEMPORAL_IP__")}:${inject("__TESTCONTAINERS_TEMPORAL_PORT_7233__")}`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { it };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TestProject } from "vitest/node";
|
|
2
|
+
|
|
3
|
+
//#region src/global-setup.d.ts
|
|
4
|
+
declare module "vitest" {
|
|
5
|
+
interface ProvidedContext {
|
|
6
|
+
__TESTCONTAINERS_TEMPORAL_IP__: string;
|
|
7
|
+
__TESTCONTAINERS_TEMPORAL_PORT_7233__: number;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Setup function for Vitest globalSetup
|
|
12
|
+
* Starts a Temporal server container before all tests
|
|
13
|
+
*/
|
|
14
|
+
declare function setup({
|
|
15
|
+
provide
|
|
16
|
+
}: TestProject): Promise<() => Promise<void>>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { setup as default };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { GenericContainer, Network, Wait } from "testcontainers";
|
|
2
|
+
|
|
3
|
+
//#region src/global-setup.ts
|
|
4
|
+
/**
|
|
5
|
+
* Setup function for Vitest globalSetup
|
|
6
|
+
* Starts a Temporal server container before all tests
|
|
7
|
+
*/
|
|
8
|
+
async function setup({ provide }) {
|
|
9
|
+
console.log("๐ณ Starting Temporal test environment...");
|
|
10
|
+
const network = await new Network().start();
|
|
11
|
+
console.log("๐ณ Starting PostgreSQL container...");
|
|
12
|
+
const postgresContainer = await new GenericContainer("postgres:18.1").withNetwork(network).withNetworkAliases("postgres").withExposedPorts(5432).withEnvironment({
|
|
13
|
+
POSTGRES_DB: "temporal",
|
|
14
|
+
POSTGRES_USER: "temporal",
|
|
15
|
+
POSTGRES_PASSWORD: "temporal"
|
|
16
|
+
}).withHealthCheck({
|
|
17
|
+
test: ["CMD-SHELL", "pg_isready -U temporal"],
|
|
18
|
+
interval: 1e3,
|
|
19
|
+
retries: 30,
|
|
20
|
+
startPeriod: 1e3,
|
|
21
|
+
timeout: 1e3
|
|
22
|
+
}).withWaitStrategy(Wait.forHealthCheck()).withStartupTimeout(12e4).start();
|
|
23
|
+
console.log("โ
PostgreSQL container started");
|
|
24
|
+
console.log("๐ณ Starting Temporal container...");
|
|
25
|
+
const temporalContainer = await new GenericContainer("temporalio/auto-setup:1.29.1").withNetwork(network).withExposedPorts(7233).withEnvironment({
|
|
26
|
+
DB: "postgres12",
|
|
27
|
+
DB_PORT: "5432",
|
|
28
|
+
POSTGRES_SEEDS: "postgres",
|
|
29
|
+
POSTGRES_USER: "temporal",
|
|
30
|
+
POSTGRES_PWD: "temporal",
|
|
31
|
+
BIND_ON_IP: "0.0.0.0",
|
|
32
|
+
TEMPORAL_BROADCAST_ADDRESS: "127.0.0.1"
|
|
33
|
+
}).withHealthCheck({
|
|
34
|
+
test: ["CMD-SHELL", "tctl --address 127.0.0.1:7233 workflow list"],
|
|
35
|
+
interval: 1e3,
|
|
36
|
+
retries: 30,
|
|
37
|
+
startPeriod: 1e3,
|
|
38
|
+
timeout: 1e3
|
|
39
|
+
}).withWaitStrategy(Wait.forHealthCheck()).start();
|
|
40
|
+
console.log("โ
Temporal container started");
|
|
41
|
+
const __TESTCONTAINERS_TEMPORAL_IP__ = temporalContainer.getHost();
|
|
42
|
+
const __TESTCONTAINERS_TEMPORAL_PORT_7233__ = temporalContainer.getMappedPort(7233);
|
|
43
|
+
provide("__TESTCONTAINERS_TEMPORAL_IP__", __TESTCONTAINERS_TEMPORAL_IP__);
|
|
44
|
+
provide("__TESTCONTAINERS_TEMPORAL_PORT_7233__", __TESTCONTAINERS_TEMPORAL_PORT_7233__);
|
|
45
|
+
console.log(`๐ Temporal test environment is ready at ${__TESTCONTAINERS_TEMPORAL_IP__}:${__TESTCONTAINERS_TEMPORAL_PORT_7233__}`);
|
|
46
|
+
return async () => {
|
|
47
|
+
console.log("๐งน Cleaning up Temporal test environment...");
|
|
48
|
+
try {
|
|
49
|
+
await temporalContainer.stop();
|
|
50
|
+
console.log("โ
Temporal container stopped");
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error("โ ๏ธ Error stopping container:", error);
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
await postgresContainer.stop();
|
|
56
|
+
console.log("โ
PostgreSQL container stopped");
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.error("โ ๏ธ Error stopping PostgreSQL container:", error);
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
await network.stop();
|
|
62
|
+
console.log("โ
Network stopped");
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.error("โ ๏ธ Error stopping network:", error);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { setup as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@temporal-contract/testing",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Shared testing utilities for temporal-contract",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./global-setup": {
|
|
8
|
+
"types": "./dist/global-setup.d.mts",
|
|
9
|
+
"import": "./dist/global-setup.mjs"
|
|
10
|
+
},
|
|
11
|
+
"./extension": {
|
|
12
|
+
"types": "./dist/extension.d.mts",
|
|
13
|
+
"import": "./dist/extension.mjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"config"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@temporalio/client": "1.13.2",
|
|
22
|
+
"@temporalio/worker": "1.13.2",
|
|
23
|
+
"testcontainers": "11.10.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "24.10.2",
|
|
27
|
+
"tsdown": "0.17.2",
|
|
28
|
+
"typescript": "5.9.3",
|
|
29
|
+
"vitest": "4.0.15",
|
|
30
|
+
"@temporal-contract/tsconfig": "0.0.1"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"vitest": "^4"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsdown src/global-setup.ts src/extension.ts --format esm --dts --clean",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|