neon-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/LICENSE +21 -0
- package/README.md +145 -0
- package/index.ts +152 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 - present Mikael Lirbank and contributors
|
|
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,145 @@
|
|
|
1
|
+
# Neon testing
|
|
2
|
+
|
|
3
|
+
A Vitest utility for running database tests with isolated [Neon](https://neon.com/) branches. Each test file gets its own dedicated PostgreSQL database (Neon branch), ensuring clean, parallel, and reproducible tests.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ๐ **Isolated test environments** - Each test file runs against its own Neon branch
|
|
8
|
+
- ๐งน **Automatic cleanup** - Neon test branches are created and destroyed automatically
|
|
9
|
+
- ๐ก๏ธ **TypeScript native** - No JavaScript support
|
|
10
|
+
- ๐ฏ **ESM only** - No CommonJS support
|
|
11
|
+
|
|
12
|
+
## How it works
|
|
13
|
+
|
|
14
|
+
1. **Branch creation**: Before tests run, a new Neon branch is created with a unique name
|
|
15
|
+
1. **Environment setup**: `DATABASE_URL` is set to point to your test branch
|
|
16
|
+
1. **Test execution**: Your tests run against the isolated database
|
|
17
|
+
1. **Cleanup**: After tests complete, the branch is automatically deleted
|
|
18
|
+
|
|
19
|
+
## Quick start
|
|
20
|
+
|
|
21
|
+
### Prerequisites
|
|
22
|
+
|
|
23
|
+
- A [Neon project](https://console.neon.tech/app/projects) with a database
|
|
24
|
+
- A [Neon API key](https://neon.tech/docs/manage/api-keys) for programmatic access
|
|
25
|
+
|
|
26
|
+
### Install
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
bun add -d neon-testing
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Minimal example
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// database.test.ts
|
|
36
|
+
import { expect, test } from "vitest";
|
|
37
|
+
import { makeNeonTesting } from "neon-testing";
|
|
38
|
+
import { Pool } from "@neondatabase/serverless";
|
|
39
|
+
|
|
40
|
+
// Enable Neon test branch for this test file
|
|
41
|
+
makeNeonTesting({ apiKey: "apiKey", projectId: "projectId" })();
|
|
42
|
+
|
|
43
|
+
test("database operations", async () => {
|
|
44
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
45
|
+
|
|
46
|
+
await pool.query(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`);
|
|
47
|
+
await pool.query(`INSERT INTO users (name) VALUES ('Ellen Ripley')`);
|
|
48
|
+
|
|
49
|
+
const users = await pool.query(`SELECT * FROM users`);
|
|
50
|
+
expect(users.rows).toStrictEqual([{ id: 1, name: "Ellen Ripley" }]);
|
|
51
|
+
|
|
52
|
+
await pool.end();
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Recommended usage
|
|
57
|
+
|
|
58
|
+
#### 1. Configuration
|
|
59
|
+
|
|
60
|
+
Use the `makeNeonTesting` factory to generate a lifecycle function for your tests.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// test-setup.ts
|
|
64
|
+
import { makeNeonTesting } from "neon-testing";
|
|
65
|
+
|
|
66
|
+
// Export a configured lifecycle function to use in test files
|
|
67
|
+
export const withNeonTestBranch = makeNeonTesting({
|
|
68
|
+
apiKey: "apiKey",
|
|
69
|
+
projectId: "projectId",
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
See all available options in [NeonTestingOptions](https://github.com/starmode-base/neon-testing/blob/main/index.ts#L30-L41).
|
|
74
|
+
|
|
75
|
+
#### 2. Enable database testing
|
|
76
|
+
|
|
77
|
+
Then call the exported test lifecycle function in the test files where you need database access.
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
// database.test.ts
|
|
81
|
+
import { expect, test } from "vitest";
|
|
82
|
+
import { withNeonTestBranch } from "./test-setup";
|
|
83
|
+
import { Pool } from "@neondatabase/serverless";
|
|
84
|
+
|
|
85
|
+
// Enable Neon test branch for this test file
|
|
86
|
+
withNeonTestBranch();
|
|
87
|
+
|
|
88
|
+
test("database operations", async () => {
|
|
89
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
90
|
+
|
|
91
|
+
await pool.query(`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT)`);
|
|
92
|
+
await pool.query(`INSERT INTO users (name) VALUES ('Ellen Ripley')`);
|
|
93
|
+
|
|
94
|
+
const users = await pool.query(`SELECT * FROM users`);
|
|
95
|
+
expect(users.rows).toStrictEqual([{ id: 1, name: "Ellen Ripley" }]);
|
|
96
|
+
|
|
97
|
+
await pool.end();
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Override configuration
|
|
102
|
+
|
|
103
|
+
Branch from a specific branch instead of main:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
import { withNeonTestBranch } from "./test-setup";
|
|
107
|
+
|
|
108
|
+
withNeonTestBranch({ parentBranchId: "br-staging-123" });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Don't copy data when branching:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { withNeonTestBranch } from "./test-setup";
|
|
115
|
+
|
|
116
|
+
withNeonTestBranch({ schemaOnly: true });
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
See all available options in [NeonTestingOptions](https://github.com/starmode-base/neon-testing/blob/main/index.ts#L30-L41).
|
|
120
|
+
|
|
121
|
+
## Isolate individual tests
|
|
122
|
+
|
|
123
|
+
Tests within a single test file share the same database instance (Neon branch), so while all test files are isolated, tests within a test file are not. If you prefer individual tests within a test file to be isolated, [simply clean up the database in a beforeEach lifecycle](examples/neon-serverless-http-isolated.test.ts).
|
|
124
|
+
|
|
125
|
+
This works because Vitest runs test files in parallel, but tests within each test file run one at a time.
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
130
|
+
|
|
131
|
+
## Contributing
|
|
132
|
+
|
|
133
|
+
Contributions are welcome! Please open issues or pull requests on [GitHub](https://github.com/starmode-base/neon-testing/pulls).
|
|
134
|
+
|
|
135
|
+
## Support
|
|
136
|
+
|
|
137
|
+
For questions or support, open an issue on [GitHub](https://github.com/starmode-base/neon-testing/issues).
|
|
138
|
+
|
|
139
|
+
## Commercial support
|
|
140
|
+
|
|
141
|
+
Need professional help with database testing, AI integration, or modern web development?
|
|
142
|
+
|
|
143
|
+
**[STฮR MODฮ](https://www.starmode.dev/)** - I run this AI development studio with data scientist and ML/AI expert Spencer Smith. We specialize in building AI-first applications, advanced AI workflows, and agentic networks. We help companies build reliable AI solutions.
|
|
144
|
+
|
|
145
|
+
**[Mikael Lirbank](https://www.lirbank.com/)** - My individual consulting practice for web application development, test automation, code quality, and technical architecture.
|
package/index.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://neon.com/docs/reference/typescript-sdk
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
createApiClient,
|
|
6
|
+
EndpointType,
|
|
7
|
+
type ConnectionDetails,
|
|
8
|
+
} from "@neondatabase/api-client";
|
|
9
|
+
import { afterAll, beforeAll } from "vitest";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a PostgreSQL connection URI from connection parameters
|
|
13
|
+
*
|
|
14
|
+
* @param connectionParameters - The connection parameters object
|
|
15
|
+
* @param type - The type of connection to create (pooler or direct)
|
|
16
|
+
* @returns A PostgreSQL connection URI string
|
|
17
|
+
*/
|
|
18
|
+
function createConnectionUri(
|
|
19
|
+
connectionParameters: ConnectionDetails,
|
|
20
|
+
type: "pooler" | "direct"
|
|
21
|
+
) {
|
|
22
|
+
const { role, password, host, pooler_host, database } =
|
|
23
|
+
connectionParameters.connection_parameters;
|
|
24
|
+
|
|
25
|
+
const hostname = type === "pooler" ? pooler_host : host;
|
|
26
|
+
|
|
27
|
+
return `postgresql://${role}:${password}@${hostname}/${database}?sslmode=require`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface NeonTestingOptions {
|
|
31
|
+
/** The Neon API key, this is used to create and teardown test branches */
|
|
32
|
+
apiKey: string;
|
|
33
|
+
/** The Neon project ID to operate on */
|
|
34
|
+
projectId: string;
|
|
35
|
+
/** The parent branch ID for the new branch */
|
|
36
|
+
parentBranchId?: string;
|
|
37
|
+
/** Whether to create a schema-only branch (default: false) */
|
|
38
|
+
schemaOnly?: boolean;
|
|
39
|
+
/** The type of connection to create (pooler is recommended) */
|
|
40
|
+
endpoint?: "pooler" | "direct";
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Options for overriding test database setup (excludes apiKey) */
|
|
44
|
+
export type NeonTestingOverrides = Omit<Partial<NeonTestingOptions>, "apiKey">;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Factory function that creates a Neon test database setup/teardown function
|
|
48
|
+
* for Vitest test suites.
|
|
49
|
+
*
|
|
50
|
+
* @param apiKey - The Neon API key
|
|
51
|
+
* @param projectId - The Neon project ID
|
|
52
|
+
* @param endpoint - The type of connection to create (pooler or direct)
|
|
53
|
+
* @param parentBranchId - The parent branch ID for the new branch
|
|
54
|
+
* @param schemaOnly - Whether to create a schema-only branch
|
|
55
|
+
* @returns A setup/teardown function for Vitest test suites
|
|
56
|
+
*
|
|
57
|
+
* Side effects:
|
|
58
|
+
* - Sets the `DATABASE_URL` environment variable to the connection URI for the
|
|
59
|
+
* new branch
|
|
60
|
+
* - Deletes the test branch after the test suite runs
|
|
61
|
+
*/
|
|
62
|
+
export function makeNeonTesting(factoryOptions: NeonTestingOptions) {
|
|
63
|
+
const apiClient = createApiClient({ apiKey: factoryOptions.apiKey });
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Delete all test branches
|
|
67
|
+
*/
|
|
68
|
+
async function deleteAllTestBranches() {
|
|
69
|
+
const { data } = await apiClient.listProjectBranches({
|
|
70
|
+
projectId: factoryOptions.projectId,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
for (const branch of data.branches) {
|
|
74
|
+
const isTestBranch =
|
|
75
|
+
data.annotations[branch.id]?.value["integration-test"] === "true";
|
|
76
|
+
|
|
77
|
+
if (isTestBranch) {
|
|
78
|
+
await apiClient.deleteProjectBranch(
|
|
79
|
+
factoryOptions.projectId,
|
|
80
|
+
branch.id
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const testDbSetup = (
|
|
87
|
+
/** Override any factory options except apiKey */
|
|
88
|
+
overrides?: NeonTestingOverrides
|
|
89
|
+
) => {
|
|
90
|
+
// Merge factory options with overrides
|
|
91
|
+
const options = { ...factoryOptions, ...overrides };
|
|
92
|
+
|
|
93
|
+
// Each test file gets its own branch ID and database client
|
|
94
|
+
let branchId: string | undefined;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Create a new test branch
|
|
98
|
+
*
|
|
99
|
+
* @returns The connection URI for the new branch
|
|
100
|
+
*/
|
|
101
|
+
async function createBranch() {
|
|
102
|
+
const { data } = await apiClient.createProjectBranch(options.projectId, {
|
|
103
|
+
branch: {
|
|
104
|
+
name: `test/${crypto.randomUUID()}`,
|
|
105
|
+
parent_id: options.parentBranchId,
|
|
106
|
+
init_source: options.schemaOnly ? "schema-only" : undefined,
|
|
107
|
+
},
|
|
108
|
+
endpoints: [{ type: EndpointType.ReadWrite }],
|
|
109
|
+
annotation_value: {
|
|
110
|
+
"integration-test": "true",
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
branchId = data.branch.id;
|
|
115
|
+
|
|
116
|
+
const [connectionUri] = data.connection_uris ?? [];
|
|
117
|
+
if (!connectionUri) {
|
|
118
|
+
throw new Error("No connection URI found");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return createConnectionUri(connectionUri, options.endpoint ?? "pooler");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Delete the test branch
|
|
126
|
+
*/
|
|
127
|
+
async function deleteBranch() {
|
|
128
|
+
if (!branchId) {
|
|
129
|
+
throw new Error("No branch to delete");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
await apiClient.deleteProjectBranch(options.projectId, branchId);
|
|
133
|
+
branchId = undefined;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
beforeAll(async () => {
|
|
137
|
+
process.env.DATABASE_URL = await createBranch();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
afterAll(async () => {
|
|
141
|
+
await deleteBranch();
|
|
142
|
+
process.env.DATABASE_URL = undefined;
|
|
143
|
+
|
|
144
|
+
// await deleteAllTestBranches();
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Attach the utility
|
|
149
|
+
testDbSetup.deleteAllTestBranches = deleteAllTestBranches;
|
|
150
|
+
|
|
151
|
+
return testDbSetup;
|
|
152
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "neon-testing",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A Vitest utility for running database tests with isolated Neon branches",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"neon",
|
|
7
|
+
"postgres",
|
|
8
|
+
"postgresql",
|
|
9
|
+
"testing",
|
|
10
|
+
"vitest"
|
|
11
|
+
],
|
|
12
|
+
"author": "Mikael Lirbank",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": "https://github.com/starmode-base/neon-testing",
|
|
15
|
+
"homepage": "https://github.com/starmode-base/neon-testing",
|
|
16
|
+
"bugs": "https://github.com/starmode-base/neon-testing/issues",
|
|
17
|
+
"module": "index.ts",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "vitest",
|
|
21
|
+
"format": "prettier --write .",
|
|
22
|
+
"release": "bun publish && git tag v$(bun -p \"require('./package.json').version\") && git push --tags"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@neondatabase/api-client": "^2.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"typescript": "^5",
|
|
29
|
+
"vitest": "^3"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@neondatabase/serverless": "^1.0.1",
|
|
33
|
+
"dotenv": "^16.5.0",
|
|
34
|
+
"pg": "^8.16.0",
|
|
35
|
+
"postgres": "^3.4.7",
|
|
36
|
+
"prettier": "^3.5.3"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"index.ts",
|
|
40
|
+
"README.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
]
|
|
43
|
+
}
|