@tsed/testcontainers-postgres 8.29.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.
Files changed (2) hide show
  1. package/package.json +36 -0
  2. package/readme.md +301 -0
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@tsed/testcontainers-postgres",
3
+ "description": "A module to test Ts.ED code that use PostgreSQL as database using Test Containers",
4
+ "type": "module",
5
+ "version": "8.29.0",
6
+ "source": "./src/index.ts",
7
+ "main": "./lib/esm/index.js",
8
+ "module": "./lib/esm/index.js",
9
+ "typings": "./lib/types/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "tsed-source": "./src/index.ts",
13
+ "types": "./lib/types/index.d.ts",
14
+ "import": "./lib/esm/index.js",
15
+ "default": "./lib/esm/index.js"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "build": "yarn build:ts",
20
+ "build:ts": "tsc --build tsconfig.esm.json"
21
+ },
22
+ "dependencies": {
23
+ "tslib": "2.8.1"
24
+ },
25
+ "devDependencies": {
26
+ "typescript": "6.0.2"
27
+ },
28
+ "peerDependencies": {},
29
+ "repository": "https://github.com/tsedio/tsed",
30
+ "bugs": {
31
+ "url": "https://github.com/tsedio/tsed/issues"
32
+ },
33
+ "homepage": "https://github.com/tsedio/tsed/tree/production/packages/testcontainers/postgres",
34
+ "author": "Romain Lenzotti",
35
+ "license": "MIT"
36
+ }
package/readme.md ADDED
@@ -0,0 +1,301 @@
1
+ <p style="text-align: center" align="center">
2
+ <a href="https://tsed.dev" target="_blank"><img src="https://tsed.dev/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
3
+ </p>
4
+
5
+ <div align="center">
6
+ <h1>TestContainers Postgres</h1>
7
+
8
+ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
9
+ [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
10
+ [![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
11
+ [![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)
12
+
13
+ </div>
14
+
15
+ <div align="center">
16
+ <a href="https://tsed.dev/">Website</a>
17
+ <span>&nbsp;&nbsp;โ€ข&nbsp;&nbsp;</span>
18
+ <a href="https://tsed.dev/getting-started/">Getting started</a>
19
+ <span>&nbsp;&nbsp;โ€ข&nbsp;&nbsp;</span>
20
+ <a href="https://slack.tsed.dev">Slack</a>
21
+ <span>&nbsp;&nbsp;โ€ข&nbsp;&nbsp;</span>
22
+ <a href="https://twitter.com/TsED_io">Twitter</a>
23
+ </div>
24
+
25
+ <hr />
26
+
27
+ A package for the [Ts.ED](https://tsed.dev/) framework to help you test your code using the [TestContainers](https://node.testcontainers.org/) library.
28
+
29
+ > **Note**: This package does **not** depend on `@tsed/platform-http`. You can use it with any test framework.
30
+
31
+ ---
32
+
33
+ ## โœจ Features
34
+
35
+ - ๐Ÿš€ **Spin up a PostgreSQL server** using TestContainers for your tests
36
+ - ๐Ÿ›‘ **Automatically stop** the PostgreSQL server after your tests
37
+ - ๐Ÿ”„ **Reset** the PostgreSQL server between test runs
38
+ - ๐Ÿท๏ธ **Database support** to isolate databases and avoid collisions between tests
39
+
40
+ ---
41
+
42
+ ## ๐Ÿ“ฆ Installation
43
+
44
+ Install with your favorite package manager:
45
+
46
+ ```sh npm
47
+ npm install --save-dev @tsedio/testcontainers-postgres
48
+ ```
49
+
50
+ ```sh yarn
51
+ yarn add --dev @tsedio/testcontainers-postgres
52
+ ```
53
+
54
+ ```sh pnpm
55
+ pnpm add --dev @tsedio/testcontainers-postgres
56
+ ```
57
+
58
+ ```sh bun
59
+ bun add --dev @tsedio/testcontainers-postgres
60
+ ```
61
+
62
+ ---
63
+
64
+ ## โš™๏ธ Configuration
65
+
66
+ Set up a global test lifecycle to manage the PostgreSQL container.
67
+
68
+ ### ๐Ÿงช Vitest
69
+
70
+ Add a global setup file in your `vitest.config.ts`:
71
+
72
+ ```ts
73
+ import {defineConfig} from "vitest/config";
74
+
75
+ export default defineConfig({
76
+ test: {
77
+ globalSetup: [import.meta.resolve("@tsed/testcontainers-postgres/vitest/setup")]
78
+ }
79
+ });
80
+ ```
81
+
82
+ ---
83
+
84
+ ### ๐Ÿงช Jest (not recommended)
85
+
86
+ Add `globalSetup` and `globalTeardown` to your Jest config:
87
+
88
+ ```json
89
+ {
90
+ "globalSetup": ["jest.setup.js"],
91
+ "globalTeardown": ["jest.teardown.js"]
92
+ }
93
+ ```
94
+
95
+ Example setup/teardown files:
96
+
97
+ ```ts
98
+ // jest.config.js
99
+ module.exports = {
100
+ globalSetup: ["jest.setup.js"],
101
+ globalTeardown: ["jest.teardown.js"]
102
+ };
103
+
104
+ // jest.setup.js
105
+ import {TestContainersPostgres} from "@tsedio/testcontainers-postgres";
106
+
107
+ export default async () => {
108
+ await TestContainersPostgres.startContainer();
109
+ };
110
+
111
+ // jest.teardown.js
112
+ import {TestContainersPostgres} from "@tsedio/testcontainers-postgres";
113
+
114
+ export default async () => {
115
+ await TestContainersPostgres.stopContainer();
116
+ };
117
+ ```
118
+
119
+ ---
120
+
121
+ ## ๐Ÿ› ๏ธ Basic Usage
122
+
123
+ ```ts
124
+ import {TestContainersPostgres} from "@tsedio/testcontainers-postgres";
125
+
126
+ describe("MyTest", () => {
127
+ beforeEach(() => {
128
+ const connectionString = TestContainersPostgres.getUrl();
129
+ console.log(connectionString);
130
+ });
131
+
132
+ afterEach(async () => {
133
+ await TestContainersPostgres.reset();
134
+ });
135
+ });
136
+ ```
137
+
138
+ ---
139
+
140
+ ## ๐Ÿค Usage with TypeORM
141
+
142
+ ### ๐Ÿงฉ Unit Test Example
143
+
144
+ ```ts
145
+ import {PlatformTest} from "@tsed/platform-http/testing";
146
+ import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
147
+ import {TestContainersPostgres} from "@tsedio/testcontainers-postgres";
148
+
149
+ @Entity()
150
+ export class User {
151
+ @PrimaryGeneratedColumn()
152
+ id: number;
153
+
154
+ @Column({unique: true})
155
+ email: string;
156
+
157
+ @Column()
158
+ name: string;
159
+ }
160
+
161
+ describe("User", () => {
162
+ beforeEach(() =>
163
+ PlatformTest.create({
164
+ typeorm: [
165
+ {
166
+ name: "default",
167
+ type: "postgres",
168
+ entities: [User],
169
+ synchronize: true,
170
+ ...TestContainersPostgres.getPostgresOptions()
171
+ }
172
+ ]
173
+ })
174
+ );
175
+
176
+ afterEach(async () => {
177
+ await TestContainersPostgres.reset(); // reset the database
178
+ await PlatformTest.reset();
179
+ });
180
+
181
+ it("should create a user", async () => {
182
+ const userRepository = PlatformTest.get(getRepository(User));
183
+
184
+ // GIVEN
185
+ const user = userRepository.create({
186
+ email: "test@test.com",
187
+ name: "Test User"
188
+ });
189
+
190
+ // WHEN
191
+ await userRepository.save(user);
192
+
193
+ // THEN
194
+ expect(user.id).toBeDefined();
195
+ });
196
+ });
197
+ ```
198
+
199
+ ### ๐Ÿ”— Integration Test Example
200
+
201
+ Use `PlatformTest.bootstrap` and `TestContainersPostgres.getPostgresOptions()` to start the PostgreSQL server before your tests:
202
+
203
+ ```ts
204
+ beforeEach(() =>
205
+ PlatformTest.bootstrap(Server, {
206
+ typeorm: [
207
+ {
208
+ name: "default",
209
+ type: "postgres",
210
+ entities: [User],
211
+ synchronize: true,
212
+ ...TestContainersPostgres.getPostgresOptions()
213
+ }
214
+ ]
215
+ })
216
+ );
217
+ ```
218
+
219
+ ---
220
+
221
+ ## ๐Ÿ”„ Usage with MikroORM
222
+
223
+ Example using `@tsedio/testcontainers-postgres` with MikroORM:
224
+
225
+ ```ts
226
+ import {EntityManager, MikroORM} from "@mikro-orm/core";
227
+ import {defineConfig} from "@mikro-orm/postgresql";
228
+ import {PlatformTest} from "@tsed/platform-http/testing";
229
+ import {TestContainersPostgres} from "@tsedio/testcontainers-postgres";
230
+
231
+ beforeEach(async () => {
232
+ const postgresOptions = TestContainersPostgres.getPostgresOptions();
233
+
234
+ await PlatformTest.bootstrap(Server, {
235
+ disableComponentScan: true,
236
+ imports: [MikroOrmModule],
237
+ mikroOrm: [
238
+ defineConfig({
239
+ type: "postgresql",
240
+ host: postgresOptions.host,
241
+ port: postgresOptions.port,
242
+ user: postgresOptions.user,
243
+ password: postgresOptions.password,
244
+ dbName: postgresOptions.database,
245
+ entities: [User],
246
+ subscribers: [UnmanagedEventSubscriber1, new UnmanagedEventSubscriber2()]
247
+ })
248
+ ]
249
+ });
250
+ });
251
+ ```
252
+
253
+ ---
254
+
255
+ ## ๐Ÿ’ก Tips
256
+
257
+ - ๐Ÿงน Use `TestContainersPostgres.reset()` to clear the database between tests.
258
+ - ๐Ÿท๏ธ Use `TestContainersPostgres.reset(dbName)` to drop and recreate a specific database.
259
+ - ๐Ÿงผ Use `TestContainersPostgres.cleanTable(tableName)` to truncate a specific table.
260
+ - ๐ŸŒ The `getPostgresOptions()` method returns connection options compatible with most PostgreSQL clients.
261
+
262
+ ---
263
+
264
+ ## ๐Ÿ“š Resources
265
+
266
+ - [Ts.ED Documentation](https://tsed.dev/) ๐Ÿ“–
267
+ - [TestContainers Node.js](https://node.testcontainers.org/) ๐Ÿณ
268
+
269
+ ## Contributors
270
+
271
+ <a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
272
+
273
+ ## Backers
274
+
275
+ Thank you to all our backers! ๐Ÿ™ [[Become a backer](https://opencollective.com/tsed#backer)]
276
+
277
+ <a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
278
+
279
+ ## Sponsors
280
+
281
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your
282
+ website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
283
+
284
+ ## License
285
+
286
+ The MIT License (MIT)
287
+
288
+ Copyright (c) 2016 - 2024 Romain Lenzotti
289
+
290
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
291
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
292
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
293
+ persons to whom the Software is furnished to do so, subject to the following conditions:
294
+
295
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
296
+ Software.
297
+
298
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
299
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
300
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
301
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.