diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50a21cf..6105d6f 100755 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - deno: [v1.x, canary] + deno: [v1.x] fail-fast: true services: postgres: diff --git a/README.md b/README.md index d541df0..cd2fb8e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Migrate -[![version](https://img.shields.io/badge/release-0.2.2-success)](https://deno.land/x/migrate@0.2.2) +[![version](https://img.shields.io/badge/release-0.2.3-success)](https://deno.land/x/migrate@0.2.3) [![CI](https://github.com/udibo/migrate/workflows/CI/badge.svg)](https://github.com/udibo/migrate/actions?query=workflow%3ACI) [![codecov](https://codecov.io/gh/udibo/migrate/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/migrate) [![license](https://img.shields.io/github/license/udibo/migrate)](https://github.com/udibo/migrate/blob/master/LICENSE) @@ -21,9 +21,9 @@ Currently migrate is only implemented for Postgres. The main entrypoint is ```ts // Import from Deno's third party module registry -import { PostgresMigrate } from "https://deno.land/x/migrate@0.2.2/postgres.ts"; +import { PostgresMigrate } from "https://deno.land/x/migrate@0.2.3/postgres.ts"; // Import from GitHub -import { PostgresMigrate } "https://raw.githubusercontent.com/udibo/migrate/0.2.2/postgres.ts"; +import { PostgresMigrate } "https://raw.githubusercontent.com/udibo/migrate/0.2.3/postgres.ts"; ``` ## Usage @@ -34,7 +34,7 @@ To use the command line interface, you must create a script that will initialize the Migrate instance and call the run command from [cli.ts](cli.ts). An example can be found [here](#postgres-cli). -See [deno docs](https://doc.deno.land/https/deno.land/x/migrate@0.2.2/cli.ts) +See [deno docs](https://doc.deno.land/https/deno.land/x/migrate@0.2.3/cli.ts) for more information. #### Command: init @@ -160,7 +160,7 @@ different ways to use the migrate module. Only one is required to use the migrate tool. See -[deno docs](https://doc.deno.land/https/deno.land/x/migrate@0.2.2/postgres.ts) +[deno docs](https://doc.deno.land/https/deno.land/x/migrate@0.2.3/postgres.ts) for more information. #### Postgres script diff --git a/basic_test.ts b/basic_test.ts index e1d96f0..8967b1f 100644 --- a/basic_test.ts +++ b/basic_test.ts @@ -1,6 +1,6 @@ import { delay, resolve } from "./deps.ts"; import { PostgresMigrate } from "./postgres.ts"; -import { assertEquals, test, TestSuite } from "./test_deps.ts"; +import { assertEquals, describe, it } from "./test_deps.ts"; import { cleanupInit, exampleMigrationsDir, @@ -9,14 +9,14 @@ import { } from "./test_postgres.ts"; import "./basic.ts"; -const applyTests = new TestSuite({ +const applyTests = describe({ name: "apply", - async beforeEach(context: InitializedMigrateTest) { - context.migrate = new PostgresMigrate({ + async beforeEach() { + this.migrate = new PostgresMigrate({ ...options, migrationsDir: exampleMigrationsDir, }); - const { migrate } = context; + const { migrate } = this; await cleanupInit(migrate); try { await migrate.connect(); @@ -27,15 +27,16 @@ const applyTests = new TestSuite({ await migrate.end(); } }, - async afterEach({ migrate }: InitializedMigrateTest) { - await migrate.end(); + async afterEach() { + await this.migrate.end(); }, }); -test( +it( applyTests, "creates migration table and applies all migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate_basic.ts"), @@ -70,7 +71,8 @@ Done }, ); -test(applyTests, "applies unapplied migrations", async ({ migrate }) => { +it(applyTests, "applies unapplied migrations", async function () { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.load(); @@ -113,7 +115,8 @@ Done } }); -test(applyTests, "no unapplied migrations", async ({ migrate }) => { +it(applyTests, "no unapplied migrations", async function () { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.load(); diff --git a/cli_test.ts b/cli_test.ts index 5790bf8..6b450b8 100644 --- a/cli_test.ts +++ b/cli_test.ts @@ -1,6 +1,6 @@ import { delay, resolve } from "./deps.ts"; import { PostgresMigrate } from "./postgres.ts"; -import { assertEquals, test, TestSuite } from "./test_deps.ts"; +import { assertEquals, describe, it } from "./test_deps.ts"; import { cleanupInit, exampleMigrationsDir, @@ -9,14 +9,14 @@ import { } from "./test_postgres.ts"; import "./cli.ts"; -const cliTests = new TestSuite({ +const cliTests = describe({ name: "CLI", - async beforeEach(context: InitializedMigrateTest) { - context.migrate = new PostgresMigrate({ + async beforeEach() { + this.migrate = new PostgresMigrate({ ...options, migrationsDir: exampleMigrationsDir, }); - const { migrate } = context; + const { migrate } = this; await cleanupInit(migrate); try { await migrate.connect(); @@ -27,20 +27,21 @@ const cliTests = new TestSuite({ await migrate.end(); } }, - async afterEach({ migrate }: InitializedMigrateTest) { - await migrate.end(); + async afterEach() { + await this.migrate.end(); }, }); -const cliInitTests = new TestSuite({ +const cliInitTests = describe({ name: "init", suite: cliTests, }); -test( +it( cliInitTests, "creates migration table if it does not exist yet", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -65,10 +66,11 @@ Created migration table }, ); -test( +it( cliInitTests, "migration table already exists", - async ({ migrate }) => { + async function () { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.end(); @@ -97,20 +99,22 @@ Migration table already exists }, ); -const cliLoadTests = new TestSuite({ +const cliLoadTests = describe({ name: "load", suite: cliTests, - async beforeEach({ migrate }: InitializedMigrateTest) { + async beforeEach() { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.end(); }, }); -test( +it( cliLoadTests, "new migrations only", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -140,10 +144,11 @@ Done }, ); -test( +it( cliLoadTests, "moved migration", - async ({ migrate }) => { + async function () { + const { migrate } = this; await migrate.connect(); await migrate.load(); await migrate.client.queryArray @@ -184,10 +189,11 @@ Done }, ); -test( +it( cliLoadTests, "deleted migration", - async ({ migrate }) => { + async function () { + const { migrate } = this; await migrate.connect(); await migrate.load(); await migrate.client.queryArray` @@ -230,10 +236,11 @@ Done }, ); -const cliStatusTests = new TestSuite({ +const cliStatusTests = describe({ name: "status", suite: cliTests, - async beforeEach({ migrate }: InitializedMigrateTest) { + async beforeEach() { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.client.queryArray` @@ -248,10 +255,11 @@ const cliStatusTests = new TestSuite({ }, }); -test( +it( cliStatusTests, "without details", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -281,10 +289,11 @@ Status: }, ); -test( +it( cliStatusTests, "with details", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -318,10 +327,11 @@ Status: }, ); -const cliListTests = new TestSuite({ +const cliListTests = describe({ name: "list", suite: cliTests, - async beforeEach({ migrate }: InitializedMigrateTest) { + async beforeEach() { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.client.queryArray` @@ -342,10 +352,11 @@ function decodeListOutput(output: Uint8Array): string { .replace(/applied at: [^\n]*\n/g, "applied at: {DATE}\n"); } -test( +it( cliListTests, "all migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -381,10 +392,11 @@ All migrations: }, ); -test( +it( cliListTests, "applied migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -419,10 +431,11 @@ Applied migrations: }, ); -test( +it( cliListTests, "unapplied migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -448,10 +461,11 @@ Unapplied migrations: }, ); -test( +it( cliListTests, "moved migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -479,10 +493,11 @@ Moved migrations: }, ); -test( +it( cliListTests, "deleted migrations", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -509,10 +524,11 @@ Deleted migrations: }, ); -const cliApplyTests = new TestSuite({ +const cliApplyTests = describe({ name: "apply", suite: cliTests, - async beforeEach({ migrate }: InitializedMigrateTest) { + async beforeEach() { + const { migrate } = this; await migrate.connect(); await migrate.init(); await migrate.load(); @@ -520,10 +536,11 @@ const cliApplyTests = new TestSuite({ }, }); -test( +it( cliApplyTests, "all unapplied", - async ({ migrate }) => { + async function () { + const { migrate } = this; const process = Deno.run({ cmd: [ resolve(migrate.migrationsDir, "../migrate.ts"), @@ -556,10 +573,11 @@ Done }, ); -test( +it( cliApplyTests, "no unapplied", - async ({ migrate }) => { + async function () { + const { migrate } = this; await migrate.connect(); const migrations = await migrate.getUnapplied(); for (const migration of migrations) { diff --git a/deps.ts b/deps.ts index 980df7f..3d0e049 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1,8 @@ -export { parse } from "https://deno.land/std@0.117.0/flags/mod.ts"; -export type { Args } from "https://deno.land/std@0.117.0/flags/mod.ts"; +export { parse } from "https://deno.land/std@0.142.0/flags/mod.ts"; +export type { Args } from "https://deno.land/std@0.142.0/flags/mod.ts"; -export { walk } from "https://deno.land/std@0.117.0/fs/walk.ts"; -export type { WalkEntry } from "https://deno.land/std@0.117.0/fs/walk.ts"; +export { walk } from "https://deno.land/std@0.142.0/fs/walk.ts"; +export type { WalkEntry } from "https://deno.land/std@0.142.0/fs/walk.ts"; export { dirname, @@ -11,9 +11,9 @@ export { relative, resolve, toFileUrl, -} from "https://deno.land/std@0.117.0/path/mod.ts"; +} from "https://deno.land/std@0.142.0/path/mod.ts"; -export { readLines } from "https://deno.land/std@0.117.0/io/buffer.ts"; -export { StringReader } from "https://deno.land/std@0.117.0/io/readers.ts"; +export { readLines } from "https://deno.land/std@0.142.0/io/buffer.ts"; +export { StringReader } from "https://deno.land/std@0.142.0/io/readers.ts"; -export { delay } from "https://deno.land/std@0.117.0/async/delay.ts"; +export { delay } from "https://deno.land/std@0.142.0/async/delay.ts"; diff --git a/examples/postgres/deps.ts b/examples/postgres/deps.ts index f9f0ec6..c361c09 100644 --- a/examples/postgres/deps.ts +++ b/examples/postgres/deps.ts @@ -2,7 +2,7 @@ export { dirname, fromFileUrl, resolve, -} from "https://deno.land/std@0.114.0/path/mod.ts"; +} from "https://deno.land/std@0.142.0/path/mod.ts"; export { PostgresMigrate } from "../../postgres.ts"; export { apply } from "../../basic.ts"; diff --git a/migrate_test.ts b/migrate_test.ts index 7a00301..def9715 100644 --- a/migrate_test.ts +++ b/migrate_test.ts @@ -10,9 +10,9 @@ import { assertEquals, assertRejects, assertThrows, + describe, ensureDir, - test, - TestSuite, + it, } from "./test_deps.ts"; function migrationFromFile( @@ -70,19 +70,20 @@ interface MigrateTest { migrate: Migrate; } -const migrateTests = new TestSuite({ +const migrateTests = describe({ name: "Migrate", - async beforeEach(context) { - context.migrate = new FakeMigrate({ + async beforeEach() { + this.migrate = new FakeMigrate({ migrationsDir: await Deno.makeTempDir(), }); }, - async afterEach({ migrate }) { - await Deno.remove(migrate.migrationsDir, { recursive: true }); + async afterEach() { + await Deno.remove(this.migrate.migrationsDir, { recursive: true }); }, }); -test(migrateTests, "resolve works", ({ migrate }) => { +it(migrateTests, "resolve works", function () { + const { migrate } = this; let migration = migrationFromFile({ id: 1, path: "0_user_create.sql", @@ -107,7 +108,7 @@ test(migrateTests, "resolve works", ({ migrate }) => { ); }); -const migrateGetFilesTests = new TestSuite({ +const migrateGetFilesTests = describe({ name: "getFiles", suite: migrateTests, }); @@ -125,10 +126,11 @@ async function assertMigrationFiles( assertEquals(actual, expected); } -test( +it( migrateGetFilesTests, "finds migrations in directory", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -147,10 +149,11 @@ test( }, ); -test( +it( migrateGetFilesTests, "finds migrations in child directories", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -171,10 +174,11 @@ test( }, ); -test( +it( migrateGetFilesTests, "recognizes migrations without name", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -185,10 +189,11 @@ test( }, ); -test( +it( migrateGetFilesTests, "recognizes migrations with json, js, or ts extensions", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -211,10 +216,11 @@ test( }, ); -test( +it( migrateGetFilesTests, "ignores non migration files", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -231,10 +237,11 @@ test( }, ); -test( +it( migrateGetFilesTests, "rejects on migration file index collision", - async ({ migrate }) => { + async function () { + const { migrate } = this; const expected: MigrationFile[] = []; await assertMigrationFiles(migrate, expected); @@ -252,12 +259,12 @@ test( }, ); -const migrateGetPlanTests = new TestSuite({ +const migrateGetPlanTests = describe({ name: "getPlan", suite: migrateTests, - beforeEach(context: MigrateTest) { - context.migrate = new FakeMigrate({ - migrationsDir: context.migrate.migrationsDir, + beforeEach() { + this.migrate = new FakeMigrate({ + migrationsDir: this.migrate.migrationsDir, }); }, }); @@ -283,10 +290,11 @@ async function assertPlan(migrate: Migrate, migration: Migration, expect: { assertEquals(actualQueries, queries); } -test( +it( migrateGetPlanTests, "from sql migration file", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.sql", @@ -302,10 +310,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from sql migration file with disableTransaction", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.sql", @@ -323,10 +332,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from json migration file", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.json", @@ -344,10 +354,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from json migration file with disableTransaction", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.json", @@ -365,10 +376,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from js migration file", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.js", @@ -392,10 +404,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from js migration file with disableTransaction", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.js", @@ -425,10 +438,11 @@ const migrateImportPath = resolve( "migrate.ts", ); -test( +it( migrateGetPlanTests, "from ts migration file", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", @@ -453,10 +467,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from ts migration file with disableTransaction", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", @@ -482,10 +497,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "from script migration file with iterator returned", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", @@ -521,10 +537,11 @@ const depsImportPath = resolve( "deps.ts", ); -test( +it( migrateGetPlanTests, "from script migration file with async iterator returned", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", @@ -558,10 +575,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "error if migration script missing generateQueries export", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", @@ -587,10 +605,11 @@ test( }, ); -test( +it( migrateGetPlanTests, "error if migration script generateQueries export is not a function", - async ({ migrate }) => { + async function () { + const { migrate } = this; const migration = migrationFromFile({ id: 0, path: "0_user_create.ts", diff --git a/postgres.ts b/postgres.ts index 0111c32..8aff004 100644 --- a/postgres.ts +++ b/postgres.ts @@ -119,7 +119,7 @@ export class PostgresMigrate `; for (const id of deletedMigrationIds) { if (appliedMigrationIds.has(id)) { - await this.client.queryArray(UPSERT_MIGRATION_FILE_SQL, id, null); + await this.client.queryArray(UPSERT_MIGRATION_FILE_SQL, [id, null]); } else { await this.client.queryArray`DELETE FROM migration WHERE id = ${id}`; } @@ -127,7 +127,7 @@ export class PostgresMigrate for (const { id, path } of migrationFiles) { if (path !== migrationPaths.get(id)) { - await this.client.queryArray(UPSERT_MIGRATION_FILE_SQL, id, path); + await this.client.queryArray(UPSERT_MIGRATION_FILE_SQL, [id, path]); } } } @@ -161,7 +161,7 @@ export class PostgresMigrate await clientOrTransaction.queryArray(query); } else { const { text, args } = query; - await clientOrTransaction.queryArray(text, ...(args ?? [])); + await clientOrTransaction.queryArray(text, args); } } clientOrTransaction.queryArray diff --git a/postgres_deps.ts b/postgres_deps.ts index e3a990f..103a9e0 100644 --- a/postgres_deps.ts +++ b/postgres_deps.ts @@ -2,5 +2,5 @@ export { Client, Transaction, TransactionError, -} from "https://deno.land/x/postgres@v0.14.2/mod.ts"; -export type { ClientOptions } from "https://deno.land/x/postgres@v0.14.2/mod.ts"; +} from "https://deno.land/x/postgres@v0.16.0/mod.ts"; +export type { ClientOptions } from "https://deno.land/x/postgres@v0.16.0/mod.ts"; diff --git a/postgres_test.ts b/postgres_test.ts index 902513f..18f78b1 100644 --- a/postgres_test.ts +++ b/postgres_test.ts @@ -6,14 +6,13 @@ import { assertRejects, assertSpyCall, assertSpyCalls, + describe, FakeTime, + it, spy, stub, - test, - TestSuite, } from "./test_deps.ts"; import { PostgresMigrate } from "./postgres.ts"; -import { Migrate } from "./migrate.ts"; import { Client, Transaction } from "./postgres_deps.ts"; import { cleanupInit, @@ -23,16 +22,17 @@ import { options, } from "./test_postgres.ts"; -const migrateTests = new TestSuite({ +const migrateTests = describe({ name: "PostgresMigrate", - async afterEach({ migrate }: MigrateTest) { + async afterEach() { + const { migrate } = this; if (migrate) { await migrate.end(); } }, }); -test(migrateTests, "client works", async () => { +it(migrateTests, "client works", async () => { const migrate = new PostgresMigrate(options); const { client } = migrate; await migrate.connect(); @@ -41,7 +41,7 @@ test(migrateTests, "client works", async () => { await assertRejects(() => client.queryArray("SELECT NOW()")); }); -test( +it( migrateTests, "duplicate calls to connect or end are ignored", async () => { @@ -56,7 +56,7 @@ test( }, ); -test(migrateTests, "now gets current date from client", async () => { +it(migrateTests, "now gets current date from client", async () => { const migrate = new PostgresMigrate(options); const { client } = migrate; await migrate.connect(); @@ -77,20 +77,20 @@ test(migrateTests, "now gets current date from client", async () => { await assertRejects(() => client.queryArray("SELECT NOW()")); }); -test(migrateTests, "init", async (context: MigrateTest) => { - context.migrate = new PostgresMigrate(options); - const { migrate } = context; +it(migrateTests, "init", async function (this: MigrateTest) { + this.migrate = new PostgresMigrate(options); + const { migrate } = this; await cleanupInit(migrate); await migrate.init(); await assertRejects(() => migrate.init()); }); -test( +it( migrateTests, "get returns array of all migrations sorted by id", - async (context: MigrateTest) => { - context.migrate = new PostgresMigrate(options); - const { migrate } = context; + async function () { + this.migrate = new PostgresMigrate(options); + const { migrate } = this; await cleanupInit(migrate); await migrate.init(); const before = await migrate.now(); @@ -154,12 +154,12 @@ test( }, ); -test( +it( migrateTests, "getUnapplied returns array of unapplied migrations sorted by id", - async (context: MigrateTest) => { - context.migrate = new PostgresMigrate(options); - const { migrate } = context; + async function () { + this.migrate = new PostgresMigrate(options); + const { migrate } = this; await cleanupInit(migrate); await migrate.init(); const before = await migrate.now(); @@ -201,21 +201,26 @@ test( }, ); -const migrateLoadTests = new TestSuite({ +const migrateLoadTests = describe({ name: "load", suite: migrateTests, - async beforeEach(context: InitializedMigrateTest) { - context.migrate = new PostgresMigrate(options); - const { migrate } = context; + async beforeEach() { + this.migrate = new PostgresMigrate(options); + const { migrate } = this; await cleanupInit(migrate); await migrate.init(); }, + // Remove after https://github.com/denoland/deno_std/pull/2308 is fixed + async afterEach() { + await this.migrate.end(); + }, }); -test( +it( migrateLoadTests, "no migrations found", - async ({ migrate }) => { + async function () { + const { migrate } = this; const getFiles = stub( migrate, "getFiles", @@ -231,10 +236,11 @@ test( }, ); -test( +it( migrateLoadTests, "add migrations for new files", - async ({ migrate }) => { + async function () { + const { migrate } = this; const getFiles = stub( migrate, "getFiles", @@ -279,10 +285,11 @@ test( }, ); -test( +it( migrateLoadTests, "delete migrations if unapplied migration file is deleted", - async ({ migrate }) => { + async function () { + const { migrate } = this; const getFiles = stub( migrate, "getFiles", @@ -316,10 +323,11 @@ test( }, ); -test( +it( migrateLoadTests, "update migrations if migration file is moved", - async ({ migrate }) => { + async function () { + const { migrate } = this; const getFiles = stub( migrate, "getFiles", @@ -367,15 +375,15 @@ test( }, ); -const migrateApplyTests = new TestSuite({ +const migrateApplyTests = describe({ name: "apply", suite: migrateTests, - async beforeEach(context: InitializedMigrateTest) { - context.migrate = new PostgresMigrate({ + async beforeEach() { + this.migrate = new PostgresMigrate({ ...options, migrationsDir: await Deno.makeTempDir(), }); - const { migrate } = context; + const { migrate } = this; await cleanupInit(migrate); await migrate.init(); try { @@ -384,9 +392,10 @@ const migrateApplyTests = new TestSuite({ await migrate.connect(); } }, - async afterEach({ migrate }: InitializedMigrateTest) { - await migrate.end(); - await Deno.remove(migrate.migrationsDir, { recursive: true }); + async afterEach() { + await Deno.remove(this.migrate.migrationsDir, { recursive: true }); + // Remove after https://github.com/denoland/deno_std/pull/2308 is fixed + await this.migrate.end(); }, }); @@ -405,7 +414,7 @@ const exampleMigrationFiles = [ }, ]; -async function assertApplyFirst(migrate: Migrate, expect: { +async function assertApplyFirst(migrate: PostgresMigrate, expect: { names: string[]; useTransaction: boolean; }): Promise { @@ -423,9 +432,10 @@ async function assertApplyFirst(migrate: Migrate, expect: { } const afterApply = await migrate.now(); - const call = assertSpyCall(applyQueries, 0); + assertSpyCall(applyQueries, 0); assert( - call.args[1] instanceof (useTransaction ? Transaction : Client), + applyQueries.calls[0].args[1] instanceof + (useTransaction ? Transaction : Client), `expected ${useTransaction ? "transaction" : "client"} but used ${ useTransaction ? "client" : "transaction" }`, @@ -468,10 +478,11 @@ const migrateImportPath = resolve( "migrate.ts", ); -test( +it( migrateApplyTests, "apply migration queries", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -504,10 +515,11 @@ test( }, ); -test( +it( migrateApplyTests, "apply migration queries with disableTransaction", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -541,10 +553,11 @@ test( }, ); -test( +it( migrateApplyTests, "apply migration queries from iterable", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -575,10 +588,11 @@ const depsImportPath = resolve( "deps.ts", ); -test( +it( migrateApplyTests, "apply migration queries from async iterable", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -607,7 +621,7 @@ test( }, ); -async function assertApplyError(migrate: Migrate, expect: { +async function assertApplyError(migrate: PostgresMigrate, expect: { names: string[]; useTransaction: boolean; errorMsg: string; @@ -625,9 +639,10 @@ async function assertApplyError(migrate: Migrate, expect: { applyQueries.restore(); } - const call = assertSpyCall(applyQueries, 0); + assertSpyCall(applyQueries, 0); assert( - call.args[1] instanceof (useTransaction ? Transaction : Client), + applyQueries.calls[0].args[1] instanceof + (useTransaction ? Transaction : Client), `expected ${useTransaction ? "transaction" : "client"} but used ${ useTransaction ? "client" : "transaction" }`, @@ -662,10 +677,11 @@ async function assertApplyError(migrate: Migrate, expect: { assertEquals(migrations.slice(2), []); } -test( +it( migrateApplyTests, "rollback on transaction error", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -684,15 +700,16 @@ test( await assertApplyError(migrate, { names: ["0_user_create.ts", "1_user_add_column_email.ts"], useTransaction: true, - errorMsg: "violates not-null constraint", + errorMsg: 'The transaction "migrate_apply_0" has been aborted', }); }, ); -test( +it( migrateApplyTests, "rollback on runtime error", - async ({ migrate }) => { + async function () { + const { migrate } = this; for (const [index, migrationFile] of exampleMigrationFiles.entries()) { await Deno.writeTextFile( resolve(migrate.migrationsDir, `${index}_${migrationFile.name}.ts`), @@ -722,23 +739,27 @@ interface LockTest extends InitializedMigrateTest { time: FakeTime; } -const migrateLockTests = new TestSuite({ +const migrateLockTests = describe({ name: "lock", suite: migrateTests, - async beforeEach(context: LockTest) { - context.migrate = new PostgresMigrate(options); - await context.migrate.connect(); - context.otherMigrate = new PostgresMigrate(options); - await context.otherMigrate.connect(); - context.time = new FakeTime(); + async beforeEach(this: LockTest) { + this.migrate = new PostgresMigrate(options); + await this.migrate.connect(); + this.otherMigrate = new PostgresMigrate(options); + await this.otherMigrate.connect(); + this.time = new FakeTime(); }, - async afterEach({ otherMigrate, time }: LockTest) { + async afterEach() { + const { otherMigrate, time } = this; await otherMigrate.end(); time.restore(); + // Remove after https://github.com/denoland/deno_std/pull/2308 is fixed + this.migrate.end(); }, }); -test(migrateLockTests, "works", async ({ migrate, otherMigrate, time }) => { +it(migrateLockTests, "works", async function () { + const { migrate, otherMigrate, time } = this; const seq: number[] = []; const main = delay(0) .then(async () => { @@ -763,7 +784,8 @@ test(migrateLockTests, "works", async ({ migrate, otherMigrate, time }) => { assertEquals(seq, [1, 2, 3, 4, 5]); }); -test(migrateLockTests, "abortable", async ({ migrate, otherMigrate, time }) => { +it(migrateLockTests, "abortable", async function () { + const { migrate, otherMigrate, time } = this; const seq: number[] = []; const controller = new AbortController(); const main = delay(0) @@ -775,6 +797,7 @@ test(migrateLockTests, "abortable", async ({ migrate, otherMigrate, time }) => { await time.tickAsync(1000); controller.abort(); await time.tickAsync(1000); + await time.tickAsync(1000); seq.push(5); await lock.release(); }); diff --git a/test.Dockerfile b/test.Dockerfile index 7d713cf..01885f8 100644 --- a/test.Dockerfile +++ b/test.Dockerfile @@ -1,4 +1,4 @@ -FROM denoland/deno:1.16.1 +FROM denoland/deno:1.22.2 WORKDIR /app # Install wait utility diff --git a/test_deps.ts b/test_deps.ts index 4a234ac..8f47378 100644 --- a/test_deps.ts +++ b/test_deps.ts @@ -5,16 +5,17 @@ export { assertRejects, assertStrictEquals, assertThrows, -} from "https://deno.land/std@0.114.0/testing/asserts.ts"; +} from "https://deno.land/std@0.142.0/testing/asserts.ts"; -export { ensureDir } from "https://deno.land/std@0.114.0/fs/ensure_dir.ts"; +export { ensureDir } from "https://deno.land/std@0.142.0/fs/ensure_dir.ts"; -export { test, TestSuite } from "https://deno.land/x/test_suite@0.9.1/mod.ts"; +export { describe, it } from "https://deno.land/std@0.142.0/testing/bdd.ts"; export { assertSpyCall, assertSpyCalls, - FakeTime, spy, stub, -} from "https://deno.land/x/mock@0.12.0/mod.ts"; +} from "https://deno.land/std@0.142.0/testing/mock.ts"; + +export { FakeTime } from "https://deno.land/std@0.142.0/testing/time.ts";