@zerotal/testing 1.0.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # Changelog — @zerotal/testing
2
+
3
+ All notable changes to this package are documented here. The format is
4
+ based on [Keep a Changelog](https://keepachangelog.com/); this package
5
+ follows the Zerotal monorepo's unified versioning.
6
+
7
+ **Maturity: `stable`**
8
+
9
+ ## [Unreleased]
10
+
11
+ ## [1.0.0] — 2026-08-05
12
+
13
+ _First public release._
14
+
15
+ ### Notes
16
+
17
+ - Conforms to the Zerotal package conventions (provider in `src/provider/`, PascalCase config factory, `ZerotalError`-based errors, test coverage).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zerotal
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,121 @@
1
+ # @zerotal/testing
2
+
3
+ > A complete testing toolkit for Zerotal — HTTP integration tests, transactional database isolation, factories, fakes, and a data generator.
4
+
5
+ `@zerotal/testing` builds on Bun's test runner. It boots your app for real HTTP integration tests via `TestApp`, isolates each test with transactional rollback, ships database/storage assertions and model factories, and re-exports the Mail/Queue/Notification fakes so you can assert on side effects without real I/O.
6
+
7
+ Part of the [Zerotal](../../README.md) framework. Requires **Bun ≥ 1.3.14**.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ bun add -d @zerotal/testing
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### A first HTTP test
18
+
19
+ `createTestApp()` boots your application on a random port and returns a `TestApp` client:
20
+
21
+ ```typescript
22
+ import { describe, it, beforeAll, afterAll } from "bun:test";
23
+ import { createTestApp, type TestApp, assertDatabaseHas } from "@zerotal/testing";
24
+ import { app } from "../bootstrap/app.ts";
25
+ import { UserFactory } from "../database/factories/UserFactory.ts";
26
+
27
+ let testApp: TestApp;
28
+ beforeAll(async () => {
29
+ testApp = await createTestApp(() => app);
30
+ });
31
+ afterAll(() => testApp.close());
32
+
33
+ describe("POST /posts", () => {
34
+ it("creates a post for an authenticated user", async () => {
35
+ const user = await UserFactory.create();
36
+
37
+ const res = await testApp.actingAs(user).post("/posts", { title: "Hello", slug: "hello" });
38
+
39
+ res.assertCreated();
40
+ await assertDatabaseHas("posts", { slug: "hello" });
41
+ });
42
+ });
43
+ ```
44
+
45
+ ### Database isolation
46
+
47
+ ```typescript
48
+ import { refreshDatabase, assertDatabaseHas, assertDatabaseMissing } from "@zerotal/testing";
49
+ import { SQL } from "bun";
50
+
51
+ const db = new SQL(":memory:");
52
+
53
+ describe("User", () => {
54
+ refreshDatabase({
55
+ connection: db,
56
+ setup: (c) => c`CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT)`,
57
+ });
58
+
59
+ it("creates a user", async () => {
60
+ await User.create({ email: "a@b.com" });
61
+ await assertDatabaseHas("users", { email: "a@b.com" });
62
+ }); // ← rolled back; next test starts clean
63
+ });
64
+ ```
65
+
66
+ ### Fakes — assert on side effects
67
+
68
+ ```typescript
69
+ import { MailFake } from "@zerotal/testing";
70
+
71
+ let mailer: MailFake;
72
+ beforeEach(() => {
73
+ mailer = MailFake.install();
74
+ });
75
+ afterEach(() => mailer.restore());
76
+
77
+ it("sends a welcome email", async () => {
78
+ await UserRegistrationService.register({ email: "alice@example.com" });
79
+ mailer.assertSent(WelcomeMail);
80
+ });
81
+ ```
82
+
83
+ ### Resetting framework state
84
+
85
+ ```typescript
86
+ import { resetTestState } from "@zerotal/testing";
87
+
88
+ afterEach(() => resetTestState()); // createTestApp()/testApp.close() call this for you
89
+ ```
90
+
91
+ ## Exports
92
+
93
+ The package exposes two subpaths:
94
+
95
+ ### `@zerotal/testing` (`.`)
96
+
97
+ | Export | Kind | Description |
98
+ | ------------------------------------------------------------------- | -------------- | ------------------------------------------------------------------------------------------------- |
99
+ | `createTestApp`, `TestApp` | helper / class | Boot the app and drive HTTP requests (`actingAs`, `get`/`post`/…). |
100
+ | `TestResponse` | class | Fluent assertions on responses (`assertCreated`, `assertOk`, …). |
101
+ | `withDatabase` | helper | Run a callback against a temporary connection. |
102
+ | `refreshDatabase` | helper | Suite-level transactional rollback. Type: `RefreshDatabaseOptions`. |
103
+ | `resetTestState` | helper | Dispose the `Application` and clear router/ORM observers/global scopes. |
104
+ | `assertDatabaseHas`, `assertDatabaseMissing`, `assertDatabaseCount` | assertions | Row-level database assertions. |
105
+ | `assertStoredFile`, `assertMissingFile` | assertions | Storage assertions. |
106
+ | `Factory`, `FactoryBatch` | classes | Model factories. Type: `FactoryPayload`. |
107
+ | `fake` | object | Random data generator for arranging state. |
108
+ | `MailFake`, `QueueFake`, `NotificationFake` | fakes | In-memory fakes re-exported from `@zerotal/mail`, `@zerotal/queue`, and `@zerotal/notifications`. |
109
+
110
+ ### `@zerotal/testing/preload` (`./preload`)
111
+
112
+ A preload module (`bun test --preload @zerotal/testing/preload`) that auto-wires the DB connection per test worker from `ZT_DB_URL`, so `withDatabase()` and `DB.table()` work without manual `beforeAll` setup.
113
+
114
+ ## Documentation
115
+
116
+ - [Testing overview](../../docs/testing/index.md)
117
+ - [HTTP Tests](../../docs/testing/http.md)
118
+ - [Console Tests](../../docs/testing/console.md)
119
+ - [Browser Tests](../../docs/testing/browser.md)
120
+ - [Database Testing](../../docs/testing/database.md)
121
+ - [Mocking & Fakes](../../docs/testing/mocking.md)
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@zerotal/testing",
3
+ "version": "1.0.0",
4
+ "license": "MIT",
5
+ "maturity": "stable",
6
+ "private": false,
7
+ "type": "module",
8
+ "main": "./src/index.ts",
9
+ "types": "./src/index.ts",
10
+ "exports": {
11
+ ".": "./src/index.ts",
12
+ "./preload": "./src/preload.ts"
13
+ },
14
+ "files": [
15
+ "CHANGELOG.md",
16
+ "src",
17
+ "!src/**/*.test.ts",
18
+ "!src/**/*.test.tsx",
19
+ "!src/**/*.spec.ts",
20
+ "!src/**/__fixtures__/**"
21
+ ],
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "engines": {
26
+ "bun": ">=1.3.14"
27
+ },
28
+ "scripts": {
29
+ "test": "bun test",
30
+ "typecheck": "tsc --noEmit"
31
+ },
32
+ "dependencies": {
33
+ "@zerotal/core": "1.0.0",
34
+ "@zerotal/orm": "1.0.0",
35
+ "@zerotal/queue": "1.0.0",
36
+ "@zerotal/notifications": "1.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "typescript": "^5.8.0",
40
+ "@zerotal/session": "1.0.0"
41
+ },
42
+ "description": "Testing utilities for Zerotal — an in-process test app, HTTP helpers, and database refresh.",
43
+ "keywords": [
44
+ "zerotal",
45
+ "bun",
46
+ "typescript",
47
+ "framework"
48
+ ],
49
+ "repository": {
50
+ "type": "git",
51
+ "url": "git+https://github.com/zerotaldev/zerotal.git",
52
+ "directory": "packages/testing"
53
+ },
54
+ "homepage": "https://github.com/zerotaldev/zerotal/tree/main/packages/testing#readme",
55
+ "bugs": "https://github.com/zerotaldev/zerotal/issues"
56
+ }