|
| 1 | +# `@kysely-vitest/core` |
| 2 | + |
| 3 | +`@kysely-vitest/core` provides helper functions to create you custom adapter for `kysely-vitest`. An adapter consists in 3 functions: |
| 4 | + |
| 5 | +- A `kysely` [dialect factory](#create-the-dialect-factory) |
| 6 | +- A `vitest` [plugin function](#create-the-plugin-function) |
| 7 | +- A `test` [function factory](#create-the-test-function-factory) |
| 8 | + |
| 9 | +## Create the Dialect Factory |
| 10 | + |
| 11 | +The dialect factory will be used to initialize the connection to the database when running migrations, seeding the database and running tests. |
| 12 | + |
| 13 | +Here is an example on how to create you dialect factory function: |
| 14 | + |
| 15 | +```ts |
| 16 | +// in src/tests/dialect.ts |
| 17 | +import type { DialectFactory } from "@kysely-vitest/core/types.js"; |
| 18 | +import { MyDialect } from 'kysely/my-dialect'; |
| 19 | + |
| 20 | +export const MY_DIALECT_CONFIG_KEY = "myDialectConfig" as const; |
| 21 | + |
| 22 | +export const myDialectFactory: DialectFactory< |
| 23 | + typeof MY_DIALECT_CONFIG_KEY |
| 24 | +> = (config) => { |
| 25 | + return new MyDialect({ |
| 26 | + // Dialect configuration |
| 27 | + }) |
| 28 | +}; |
| 29 | + |
| 30 | +// Extend vitest types |
| 31 | +declare module "vitest" { |
| 32 | + export type MyDialectConfig = { |
| 33 | + // Dialect options |
| 34 | + }; |
| 35 | + |
| 36 | + export interface ProvidedContext { |
| 37 | + myDialectConfig: MyDialectConfig; |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +## Create the Plugin Function |
| 43 | + |
| 44 | +Once your dialect function has been created, you can create the plugin function that will be used by `vitest` to run migrations, seed the database and configure your `test` function. |
| 45 | + |
| 46 | +Here is an example on how to create your plugin: |
| 47 | + |
| 48 | +```ts |
| 49 | +// in src/tests/plugin.ts |
| 50 | +import { createPlugin } from "@kysely-vitest/core/plugin.js"; |
| 51 | +import { MY_DIALECT_CONFIG_KEY, myDialectFactory } from "./dialect.js"; |
| 52 | + |
| 53 | +export const kyselyPlugin = createPlugin({ |
| 54 | + name: "plugin", |
| 55 | + configKey: MY_DIALECT_CONFIG_KEY, |
| 56 | + dialectFactory: myDialectFactory, |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +Then, you can use your plugin in the `vitest.config.ts` file: |
| 61 | + |
| 62 | +```ts |
| 63 | +// in vitest.config.ts |
| 64 | +import path from "node:path"; |
| 65 | +import { defineConfig } from "vitest/config"; |
| 66 | +import type { DB } from "./src/db.js"; |
| 67 | +import { kyselyPlugin } from "./src/tests/plugin.js"; |
| 68 | + |
| 69 | +export default defineConfig({ |
| 70 | + plugins: [ |
| 71 | + // Other plugins |
| 72 | + kyselyPlugin<DB>({ |
| 73 | + config: { |
| 74 | + // Your dialect configuration |
| 75 | + }, |
| 76 | + migrationFolder: path.resolve(__dirname, "migrations"), |
| 77 | + }), |
| 78 | + ], |
| 79 | + test: { |
| 80 | + // Test configuration |
| 81 | + }, |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +Note: You can use also use a [`seed`](#seeding) function with your plugin. |
| 86 | + |
| 87 | +## Create the Test Function Factory |
| 88 | + |
| 89 | +Once your plugin has been configured, you can create your `test` function. Here is an example of a `dbTest` function: |
| 90 | + |
| 91 | +```ts |
| 92 | +// in src/tests/dbTest.ts |
| 93 | +import { createTestFunction } from "@kysely-vitest/core/test.js"; |
| 94 | +import { MY_DIALECT_CONFIG_KEY, myDialectFactory } from "./dialect.js"; |
| 95 | +import type { DB } from "../db.js"; |
| 96 | + |
| 97 | +export const dbTest = createTestFunction<typeof MY_DIALECT_CONFIG_KEY, DB>({ |
| 98 | + configKey: MY_DIALECT_CONFIG_KEY, |
| 99 | + dialectFactory: myDialectFactory, |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +Then you can use your test function inside your test suites: |
| 104 | + |
| 105 | +```ts |
| 106 | +// in src/myTestSuite.spec.ts |
| 107 | +import { describe, expect } from "vitest"; |
| 108 | +import { dbTest } from "./tests/dbTest.js"; |
| 109 | + |
| 110 | +describe("myTestSuite", () => { |
| 111 | + dbTest("my test", async ({ db }) => { |
| 112 | + // ... |
| 113 | + }); |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +Note: The `db` parameter is a transaction that is rolled back after the test to ensure that each test runs in isolation. |
| 118 | + |
| 119 | +## Seeding |
| 120 | + |
| 121 | +You can provide a seeding function to your adapter to seed your database before running the tests. Here is an example with your custom adapter: |
| 122 | + |
| 123 | +First create your `seed` function in `src/tests/seed.js`: |
| 124 | + |
| 125 | +```ts |
| 126 | +// in src/tests/seed.js |
| 127 | +import type { SeedFunction } from "@kysely-vitest/core/types.js"; |
| 128 | +import { sql } from "kysely"; |
| 129 | +import type { DB } from "../db.js"; |
| 130 | + |
| 131 | +export const seed: SeedFunction<DB> = async (db) => { |
| 132 | + await sql<void>`TRUNCATE TABLE "users" RESTART IDENTITY CASCADE`.execute(db); |
| 133 | + |
| 134 | + await db |
| 135 | + .insertInto("users") |
| 136 | + .values([{ username: "alice" }, { username: "bob" }]) |
| 137 | + .execute(); |
| 138 | +}; |
| 139 | +``` |
| 140 | + |
| 141 | +Then configure the plugin in `vitest.config.ts` to use your `seed` function: |
| 142 | + |
| 143 | +```ts |
| 144 | +// in vitest.config.ts |
| 145 | +import path from "node:path"; |
| 146 | +import { defineConfig } from "vitest/config"; |
| 147 | +import type { DB } from "./src/db.js"; |
| 148 | +import { kyselyPlugin } from "./src/tests/plugin.js"; |
| 149 | +import { seed } from "./src/tests/seed.js"; |
| 150 | + |
| 151 | +export default defineConfig({ |
| 152 | + plugins: [ |
| 153 | + // Other plugins |
| 154 | + kyselyPlugin<DB>({ |
| 155 | + // Configure the plugin |
| 156 | + seed, |
| 157 | + }), |
| 158 | + ], |
| 159 | + test: { |
| 160 | + // Test configuration |
| 161 | + }, |
| 162 | +}); |
| 163 | +``` |
| 164 | + |
| 165 | +Note: The seeding function will be run once before running your tests suite and will not run on test reload. |
0 commit comments