Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kysely-vitest/core",
"version": "0.1.1",
"version": "0.2.0",
"type": "module",
"repository": {
"type": "https",
Expand Down
41 changes: 41 additions & 0 deletions packages/core/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,47 @@ export default defineConfig({

Note: You can use also use a [`seed`](#seeding) function with your plugin.

## Configuration Provider

The `configProvider` function can be used to cleanup the configuration that will be passed to the test context and configure the managed docker container.

Pass your custom config provider when you configure your plugin:

```ts
// ...
import { configProvider, type MyConfig } from "./config.js";

export const kyselyPlugin = createPlugin<
typeof MY_DIALECT_CONFIG_KEY,
MyConfig
>({
name: "plugin",
configKey: MY_DIALECT_CONFIG_KEY,
configProvider,
dialectFactory: myDialectFactory,
});
```

Then you can create your custom config provider in `src/tests/config.js`:

```ts
// in src/tests/config.js

export const configProvider: ConfigProvider<
typeof MY_DIALECT_CONFIG_KEY,
MyConfig
> = async (config) {
return {
config: {
// ... transform provided config
},
dockerContainer: {
// ... Docker container configuration
} | false
}
}
```

## Create the Test Function Factory

Once your plugin has been configured, you can create your `test` function. Here is an example of a `dbTest` function:
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres-test-docker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kysely-vitest/postgres-test-docker",
"version": "0.1.1",
"version": "0.2.0",
"private": true,
"type": "module",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres-test-external/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kysely-vitest/postgres-test-external",
"version": "0.1.1",
"version": "0.2.0",
"private": true,
"type": "module",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/postgres/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kysely-vitest/postgres",
"version": "0.1.1",
"version": "0.2.0",
"type": "module",
"repository": {
"type": "https",
Expand Down
63 changes: 60 additions & 3 deletions packages/postgres/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,68 @@ describe("myTestSuite", () => {

Note: The `db` parameter is a transaction that is rolled back after the test to ensure that each test runs in isolation.

## Run your Test Suite
## Run your Test Suite with a Managed Container

To run you test suite, you will need to start a PostgreSQL database. The `@kysely-vitest/postgres` plugin will automatically run your migrations on that database.
To run you test suite with a managed container, you can configure the container to use in the `vitest.config.ts` file.

You use the following `docker-compose.yml` file to run your test database:
Here is an example on how to configure a managed container with `@kysely-vitest/postgres`:

```ts
// in vitest.config.ts

export default defineConfig({
plugins: [
// Other plugins
kyselyPostgres<DB>({
config: {
// Will be used as host port, defaults to `5432`
port: 5433,

// Will be used in POSTGRES_DB env, defaults to `testdb`
database: "mydb",

// Will be used in POSTGRES_USER env, defaults to `testuser`
user: "myuser",

// Will be used in POSTGRES_PASSWORD env, defaults to `test`
password: "secret",

// The container will use postgres:latest
dockerContainer: true
},
}),
],
});
```

You can also select a custom `image` or `tag` for the container:

```ts
// in vitest.config.ts

export default defineConfig({
plugins: [
// Other plugins
kyselyPostgres<DB>({
config: {
// ... configuration

// Use custom image / tag
dockerContainer: {
image: "postgres",
tag: "18-alpine",
},
},
}),
],
});
```

## Run your Test Suite with an Unmanaged Container

To run you test suite with an unmanaged container, you will need to start a PostgreSQL database on your own. The `@kysely-vitest/postgres` plugin will automatically run your migrations on that database.

You can use the following `docker-compose.yml` file to run your test database:

```yml
# in docker-compose.yml
Expand Down
3 changes: 1 addition & 2 deletions packages/postgres/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createPlugin } from "@kysely-vitest/core/plugin.js";
import type { PluginConfig } from "./config.js";
import { configProvider } from "./config.js";
import { configProvider, type PluginConfig } from "./config.js";
import { POSTGRES_CONFIG_KEY, postgresDialectFactory } from "./dialect.js";

export const kyselyPostgres = createPlugin<
Expand Down
104 changes: 101 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,68 @@ describe("myTestSuite", () => {

Note: The `db` parameter is a transaction that is rolled back after the test to ensure that each test runs in isolation.

#### Run your Test Suite
#### Run your Test Suite with a Managed Container

To run you test suite, you will need to start a PostgreSQL database. The `@kysely-vitest/postgres` plugin will automatically run your migrations on that database.
To run you test suite with a managed container, you can configure the container to use in the `vitest.config.ts` file.

You use the following `docker-compose.yml` file to run your test database:
Here is an example on how to configure a managed container with `@kysely-vitest/postgres`:

```ts
// in vitest.config.ts

export default defineConfig({
plugins: [
// Other plugins
kyselyPostgres<DB>({
config: {
// Will be used as host port, defaults to `5432`
port: 5433,

// Will be used in POSTGRES_DB env, defaults to `testdb`
database: "mydb",

// Will be used in POSTGRES_USER env, defaults to `testuser`
user: "myuser",

// Will be used in POSTGRES_PASSWORD env, defaults to `test`
password: "secret",

// The container will use postgres:latest
dockerContainer: true
},
}),
],
});
```

You can also select a custom `image` or `tag` for the container:

```ts
// in vitest.config.ts

export default defineConfig({
plugins: [
// Other plugins
kyselyPostgres<DB>({
config: {
// ... configuration

// Use custom image / tag
dockerContainer: {
image: "postgres",
tag: "18-alpine",
},
},
}),
],
});
```

#### Run your Test Suite with an Unmanaged Container

To run you test suite with an unmanaged container, you will need to start a PostgreSQL database on your own. The `@kysely-vitest/postgres` plugin will automatically run your migrations on that database.

You can use the following `docker-compose.yml` file to run your test database:

```yml
# in docker-compose.yml
Expand Down Expand Up @@ -230,6 +287,47 @@ export default defineConfig({

Note: You can use also use a [`seed`](#seeding) function with your plugin.

#### Configuration Provider

The `configProvider` function can be used to cleanup the configuration that will be passed to the test context and configure the managed docker container.

Pass your custom config provider when you configure your plugin:

```ts
// ...
import { configProvider, type MyConfig } from "./config.js";

export const kyselyPlugin = createPlugin<
typeof MY_DIALECT_CONFIG_KEY,
MyConfig
>({
name: "plugin",
configKey: MY_DIALECT_CONFIG_KEY,
configProvider,
dialectFactory: myDialectFactory,
});
```

Then you can create your custom config provider in `src/tests/config.js`:

```ts
// in src/tests/config.js

export const configProvider: ConfigProvider<
typeof MY_DIALECT_CONFIG_KEY,
MyConfig
> = async (config) {
return {
config: {
// ... transform provided config
},
dockerContainer: {
// ... Docker container configuration
} | false
}
}
```

#### Create the Test Function Factory

Once your plugin has been configured, you can create your `test` function. Here is an example of a `dbTest` function:
Expand Down