Skip to content

Commit e0b89d9

Browse files
committed
Improve docs
1 parent 4f8443a commit e0b89d9

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,57 @@ test("foo bar", async (t) => {
9797

9898
## Advanced Usage
9999

100+
### Postgres container de-duping
101+
102+
In rare cases, you may want to spawn more than one Postgres container.
103+
104+
Internally, this library uses an AVA "shared worker". A shared worker is a singleton shared with the entire running test suite, and so one `ava-postgres` shared worker maps to exactly one Postgres container.
105+
106+
To spawn separate shared workers and thus additional Postgres containers, you have two options:
107+
108+
**Specify different version strings for the `postgresVersion` option in the factory function**:
109+
110+
```ts
111+
const getTestPostgresDatabase = getTestPostgresDatabaseFactory({
112+
postgresVersion: "14",
113+
})
114+
```
115+
116+
Each unique version will map to a unique shared worker.
117+
118+
**Set the `workerDedupeKey` option in the factory function**:
119+
120+
```ts
121+
const getTestPostgresDatabase = getTestPostgresDatabaseFactory({
122+
workerDedupeKey: "foo",
123+
})
124+
```
125+
126+
Each unique key will map to a unique shared worker.
127+
128+
### Database de-duping
129+
130+
By default, `ava-postgres` will create a new database for each test. If you want to share a database between tests, you can use the `databaseDedupeKey` option:
131+
132+
```ts
133+
import test from "ava"
134+
const getTestPostgresDatabase = getTestPostgresDatabaseFactory({})
135+
136+
test("foo", async (t) => {
137+
const connection1 = await getTestPostgresDatabase(t, null, {
138+
databaseDedupeKey: "foo",
139+
})
140+
const connection2 = await getTestPostgresDatabase(t, null, {
141+
databaseDedupeKey: "foo",
142+
})
143+
t.is(connection1.database, connection2.database)
144+
})
145+
```
146+
147+
This works across the entire test suite.
148+
149+
Note that if unique parameters are passed to the `beforeTemplateIsBaked` (`null` in the above example), separate databases will still be created.
150+
100151
### Bind mounts & `exec`ing in the container
101152

102153
`ava-postgres` uses [testcontainers](https://www.npmjs.com/package/testcontainers) under the hood to manage the Postgres container.

src/public-types.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export interface GetTestPostgresDatabaseFactoryOptions<
4848
}
4949

5050
/**
51-
* Test workers will be de-duped by this key. You probably don't need to set this.
51+
* In rare cases, you may want to spawn more than one Postgres container.
52+
* Internally, this library uses an AVA "shared worker". A shared worker is a singleton shared with the entire running test suite, and so one `ava-postgres` shared worker maps to exactly one Postgres container.
53+
* To spawn separate shared workers and thus additional Postgres containers, you can specify a custom key here.
54+
* Each unique key will map to a unique shared worker/unique Postgres container.
5255
*/
5356
workerDedupeKey?: string
5457
beforeTemplateIsBaked?: (options: {
@@ -64,7 +67,26 @@ export interface GetTestPostgresDatabaseResult extends ConnectionDetails {
6467

6568
export type GetTestPostgresDatabaseOptions = {
6669
/**
67-
* If `getTestPostgresDatabase()` is called multiple times with the same `key` and `params`, the same database is guaranteed to be returned.
70+
* By default, `ava-postgres` will create a new database for each test. If you want to share a database between tests, you can use the `databaseDedupeKey` option.
71+
* This works across the entire test suite.
72+
*
73+
* Note that if unique parameters are passed to the `beforeTemplateIsBaked` (`null` in the above example), separate databases will still be created.
74+
* @example
75+
* ```ts
76+
* import test from "ava"
77+
*
78+
* const getTestPostgresDatabase = getTestPostgresDatabaseFactory({})
79+
*
80+
* test("foo", async (t) => {
81+
* const connection1 = await getTestPostgresDatabase(t, null, {
82+
* databaseDedupeKey: "foo",
83+
* })
84+
* const connection2 = await getTestPostgresDatabase(t, null, {
85+
* databaseDedupeKey: "foo",
86+
* })
87+
* t.is(connection1.database, connection2.database)
88+
* })
89+
* ```
6890
*/
6991
databaseDedupeKey?: string
7092
}

0 commit comments

Comments
 (0)