neon-testing 2.1.0 → 2.1.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/README.md +74 -34
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
# Neon
|
|
1
|
+
# Neon Testing
|
|
2
2
|
|
|
3
3
|
[](https://github.com/starmode-base/neon-testing/actions/workflows/test.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/neon-testing)
|
|
5
|
+
[](https://github.com/starmode-base/neon-testing/releases)
|
|
4
6
|
|
|
5
|
-
A [Vitest](https://vitest.dev/) utility for seamless integration tests with [Neon Postgres](https://neon.com/).
|
|
7
|
+
A [Vitest](https://vitest.dev/) utility for seamless integration tests with [Neon Postgres](https://neon.com/). <!-- A [STΛR MODΞ](https://starmode.dev) open-source project. -->
|
|
6
8
|
|
|
7
9
|
Each test file runs against its own isolated PostgreSQL database (Neon branch), ensuring clean, parallel, and reproducible testing of code that interacts with a database. Because it uses a real, isolated clone of your production database, you can test code logic that depends on database features, such as transaction rollbacks, unique constraints, and more.
|
|
8
10
|
|
|
@@ -41,12 +43,12 @@ If you prefer individual tests to be isolated, you can [reset the database](exam
|
|
|
41
43
|
### Install
|
|
42
44
|
|
|
43
45
|
```sh
|
|
44
|
-
bun add -d neon-testing
|
|
46
|
+
bun add -d neon-testing vitest
|
|
45
47
|
```
|
|
46
48
|
|
|
47
49
|
### Minimal example
|
|
48
50
|
|
|
49
|
-
```
|
|
51
|
+
```ts
|
|
50
52
|
// minimal.test.ts
|
|
51
53
|
import { expect, test } from "vitest";
|
|
52
54
|
import { makeNeonTesting } from "neon-testing";
|
|
@@ -77,7 +79,7 @@ test("database operations", async () => {
|
|
|
77
79
|
|
|
78
80
|
First, add the Vite plugin to clear any existing `DATABASE_URL` environment variable before tests run, ensuring tests use isolated test databases.
|
|
79
81
|
|
|
80
|
-
```
|
|
82
|
+
```ts
|
|
81
83
|
// vitest.config.ts or vite.config.ts
|
|
82
84
|
import { defineConfig } from "vitest/config";
|
|
83
85
|
import { neonTesting } from "neon-testing/utils";
|
|
@@ -87,13 +89,13 @@ export default defineConfig({
|
|
|
87
89
|
});
|
|
88
90
|
```
|
|
89
91
|
|
|
90
|
-
This plugin is recommended but not required. Without it, tests might accidentally use your existing `DATABASE_URL` (from `.env` files or environment variables) instead of the isolated test databases that
|
|
92
|
+
This plugin is recommended but not required. Without it, tests might accidentally use your existing `DATABASE_URL` (from `.env` files or environment variables) instead of the isolated test databases that Neon Testing creates. This can happen if you forget to call `withNeonTestBranch()` in a test file where database writes happen.
|
|
91
93
|
|
|
92
94
|
#### 2. Configuration
|
|
93
95
|
|
|
94
96
|
Use the `makeNeonTesting` factory to generate a lifecycle function for your tests.
|
|
95
97
|
|
|
96
|
-
```
|
|
98
|
+
```ts
|
|
97
99
|
// test-setup.ts
|
|
98
100
|
import { makeNeonTesting } from "neon-testing";
|
|
99
101
|
|
|
@@ -108,7 +110,7 @@ export const withNeonTestBranch = makeNeonTesting({
|
|
|
108
110
|
|
|
109
111
|
Then call the exported test lifecycle function in the test files where you need database access.
|
|
110
112
|
|
|
111
|
-
```
|
|
113
|
+
```ts
|
|
112
114
|
// recommended.test.ts
|
|
113
115
|
import { expect, test } from "vitest";
|
|
114
116
|
import { withNeonTestBranch } from "./test-setup";
|
|
@@ -150,18 +152,66 @@ This library works with any database driver that supports Neon Postgres and Vite
|
|
|
150
152
|
|
|
151
153
|
## Configuration
|
|
152
154
|
|
|
153
|
-
You configure
|
|
155
|
+
You configure Neon Testing in two places:
|
|
154
156
|
|
|
155
157
|
- **Base settings** in `makeNeonTesting()`
|
|
156
158
|
- **Optional overrides** in `withNeonTestBranch()`
|
|
157
159
|
|
|
158
|
-
|
|
160
|
+
Configure these in `makeNeonTesting()` and optionally override per test file via `withNeonTestBranch()`.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
export interface NeonTestingOptions {
|
|
164
|
+
/**
|
|
165
|
+
* The Neon API key, this is used to create and teardown test branches
|
|
166
|
+
*
|
|
167
|
+
* https://neon.com/docs/manage/api-keys#creating-api-keys
|
|
168
|
+
*/
|
|
169
|
+
apiKey: string;
|
|
170
|
+
/**
|
|
171
|
+
* The Neon project ID to operate on
|
|
172
|
+
*
|
|
173
|
+
* https://console.neon.tech/app/projects
|
|
174
|
+
*/
|
|
175
|
+
projectId: string;
|
|
176
|
+
/**
|
|
177
|
+
* The parent branch ID for the new branch. If omitted or empty, the branch
|
|
178
|
+
* will be created from the project's default branch.
|
|
179
|
+
*/
|
|
180
|
+
parentBranchId?: string;
|
|
181
|
+
/**
|
|
182
|
+
* Whether to create a schema-only branch (default: false)
|
|
183
|
+
*/
|
|
184
|
+
schemaOnly?: boolean;
|
|
185
|
+
/**
|
|
186
|
+
* The type of connection to create (pooler is recommended)
|
|
187
|
+
*/
|
|
188
|
+
endpoint?: "pooler" | "direct";
|
|
189
|
+
/**
|
|
190
|
+
* Delete the test branch in afterAll (default: true)
|
|
191
|
+
*
|
|
192
|
+
* Disabling this will leave each test branch in the Neon project after the
|
|
193
|
+
* test suite runs
|
|
194
|
+
*/
|
|
195
|
+
deleteBranch?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Automatically close Neon WebSocket connections opened during tests before
|
|
198
|
+
* deleting the branch (default: false)
|
|
199
|
+
*
|
|
200
|
+
* Suppresses the specific Neon WebSocket "Connection terminated unexpectedly"
|
|
201
|
+
* error that may surface when deleting a branch with open WebSocket
|
|
202
|
+
* connections
|
|
203
|
+
*/
|
|
204
|
+
autoCloseWebSockets?: boolean;
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
See all available options in [NeonTestingOptions](index.ts#L31-L73).
|
|
159
209
|
|
|
160
210
|
### Base configuration
|
|
161
211
|
|
|
162
212
|
Configure the base settings in `makeNeonTesting()`:
|
|
163
213
|
|
|
164
|
-
```
|
|
214
|
+
```ts
|
|
165
215
|
import { makeNeonTesting } from "neon-testing";
|
|
166
216
|
|
|
167
217
|
export const withNeonTestBranch = makeNeonTesting({
|
|
@@ -174,26 +224,20 @@ export const withNeonTestBranch = makeNeonTesting({
|
|
|
174
224
|
|
|
175
225
|
Override the base configuration in specific test files with `withNeonTestBranch()`:
|
|
176
226
|
|
|
177
|
-
```
|
|
227
|
+
```ts
|
|
178
228
|
import { withNeonTestBranch } from "./test-setup";
|
|
179
229
|
|
|
180
230
|
withNeonTestBranch({ parentBranchId: "br-staging-123" });
|
|
181
231
|
```
|
|
182
232
|
|
|
183
|
-
##
|
|
184
|
-
|
|
185
|
-
It is easy to run Neon integration in CI/CD
|
|
186
|
-
|
|
187
|
-
### GitHub Actions
|
|
233
|
+
## Continuous integration
|
|
188
234
|
|
|
189
|
-
|
|
235
|
+
It's easy to run Neon integration tests in CI/CD pipelines:
|
|
190
236
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
- Add `vitest run` to the `build` script in [package.json](https://github.com/starmode-base/template-tanstack-start/blob/83c784e164b55fd8d59c5b57b907251e5eb03de1/app/package.json#L11l)
|
|
196
|
-
- Add `vitest run` to the _Build Command_ in the Vercel dashboard
|
|
237
|
+
- **GitHub Actions** — see the [example workflow](.github/workflows/test.yml)
|
|
238
|
+
- **Vercel** — either
|
|
239
|
+
- add `vitest run` to the `build` script in [package.json](https://github.com/starmode-base/template-tanstack-start/blob/83c784e164b55fd8d59c5b57b907251e5eb03de1/app/package.json#L11), or
|
|
240
|
+
- add `vitest run` to the _Build Command_ in the Vercel dashboard
|
|
197
241
|
|
|
198
242
|
## Utilities
|
|
199
243
|
|
|
@@ -201,20 +245,20 @@ Two options:
|
|
|
201
245
|
|
|
202
246
|
The `deleteAllTestBranches()` function is a utility that deletes all test branches from your Neon project. This is useful for cleanup when tests fail unexpectedly and leave orphaned test branches.
|
|
203
247
|
|
|
204
|
-
```
|
|
248
|
+
```ts
|
|
205
249
|
import { withNeonTestBranch } from "./test-setup";
|
|
206
250
|
|
|
207
251
|
// Access the cleanup utility
|
|
208
252
|
await withNeonTestBranch.deleteAllTestBranches();
|
|
209
253
|
```
|
|
210
254
|
|
|
211
|
-
The function identifies test branches by looking for the `integration-test: true` annotation that
|
|
255
|
+
The function identifies test branches by looking for the `integration-test: true` annotation that Neon Testing automatically adds to all test branches it creates.
|
|
212
256
|
|
|
213
257
|
### lazySingleton()
|
|
214
258
|
|
|
215
259
|
The `lazySingleton()` function creates a lazy singleton from a factory function. This is useful for managing database connections efficiently:
|
|
216
260
|
|
|
217
|
-
```
|
|
261
|
+
```ts
|
|
218
262
|
import { lazySingleton } from "neon-testing/utils";
|
|
219
263
|
import { neon } from "@neondatabase/serverless";
|
|
220
264
|
|
|
@@ -250,12 +294,8 @@ bun run release
|
|
|
250
294
|
|
|
251
295
|
The command will abort if there are uncommitted changes in the working tree, or if the `version` in [package.json](package.json) has not been incremented.
|
|
252
296
|
|
|
253
|
-
##
|
|
254
|
-
|
|
255
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
256
|
-
|
|
257
|
-
## Need help?
|
|
297
|
+
## Author
|
|
258
298
|
|
|
259
|
-
Hi, I
|
|
299
|
+
Hi, I'm [Mikael Lirbank](https://www.lirbank.com/). I build robust, reliable, high-quality AI systems. I care deeply about quality—AI evals, robust test suites, clean data models, and clean architecture.
|
|
260
300
|
|
|
261
|
-
|
|
301
|
+
Need help building elegant systems? [I'm happy to help](https://www.lirbank.com/).
|