From a267968735da2dfc0e42a08f1bc83e468bbb81b6 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 5 Mar 2024 12:38:06 +0100 Subject: [PATCH] rewrite to use async expect --- pnpm-lock.yaml | 8 +-- test/db.spec.ts | 12 ++--- test/migration.spec.ts | 111 +++++++++++++++++++---------------------- 3 files changed, 62 insertions(+), 69 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e7679e4b..301a61a8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4327,7 +4327,7 @@ packages: debug: 4.3.4 pathe: 1.1.2 picocolors: 1.0.0 - vite: 5.1.4(@types/node@18.19.21) + vite: 5.1.5(@types/node@18.19.21) transitivePeerDependencies: - '@types/node' - less @@ -4339,8 +4339,8 @@ packages: - terser dev: true - /vite@5.1.4(@types/node@18.19.21): - resolution: {integrity: sha512-n+MPqzq+d9nMVTKyewqw6kSt+R3CkvF9QAKY8obiQn8g1fwTscKxyfaYnC632HtBXAQGc1Yjomphwn1dtwGAHg==} + /vite@5.1.5(@types/node@18.19.21): + resolution: {integrity: sha512-BdN1xh0Of/oQafhU+FvopafUp6WaYenLU/NFoL5WyJL++GxkNfieKzBhM24H3HVsPQrlAqB7iJYTHabzaRed5Q==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true peerDependencies: @@ -4419,7 +4419,7 @@ packages: strip-literal: 2.0.0 tinybench: 2.6.0 tinypool: 0.8.2 - vite: 5.1.4(@types/node@18.19.21) + vite: 5.1.5(@types/node@18.19.21) vite-node: 1.3.1(@types/node@18.19.21) why-is-node-running: 2.2.2 transitivePeerDependencies: diff --git a/test/db.spec.ts b/test/db.spec.ts index 0cd0c1ea..54472e17 100644 --- a/test/db.spec.ts +++ b/test/db.spec.ts @@ -91,32 +91,32 @@ describe('lib/db', () => { expect(hoisted.client.query).toHaveBeenCalledWith('query', undefined); }); - it('should not call client.query if client.connect fails', async () => { + it('should not call client.query if client.connect fails', () => { const error = 'error'; vi.spyOn(hoisted.client, 'connect').mockImplementation((fn) => fn(new Error(error)) ); - await expect(() => db.query('query')).rejects.toThrow(error); + expect(() => db.query('query')).rejects.toThrow(error); expect(hoisted.client.query).not.toHaveBeenCalled(); }); - it('should resolve promise if query throws no error', async () => { + it('should resolve promise if query throws no error', () => { const result = 'result'; vi.spyOn(hoisted.client, 'connect').mockImplementation((fn) => fn()); vi.spyOn(hoisted.client, 'query').mockResolvedValue(result); - await expect(db.query('query')).resolves.toBe(result); + expect(db.query('query')).resolves.toBe(result); }); - it('should reject promise if query throws error', async () => { + it('should reject promise if query throws error', () => { const error = 'error'; vi.spyOn(hoisted.client, 'query').mockRejectedValue(new Error(error)); - await expect(() => db.query('query')).rejects.toThrow(error); + expect(() => db.query('query')).rejects.toThrow(error); expect(hoisted.client.connect).toHaveBeenCalledOnce(); }); }); diff --git a/test/migration.spec.ts b/test/migration.spec.ts index 798ca9f1..259a7567 100644 --- a/test/migration.spec.ts +++ b/test/migration.spec.ts @@ -46,7 +46,7 @@ describe('lib/migration', () => { }); describe('self.applyUp', () => { - it('normal operations: db.query should be called', () => { + it('normal operations: db.query should be called', async () => { const migration = new Migration( dbMock, callbackMigration, @@ -56,12 +56,12 @@ describe('lib/migration', () => { logger ); - return migration.apply('up').then(() => { - expect(queryMock).toHaveBeenCalled(); - }); + await migration.apply('up'); + + expect(queryMock).toHaveBeenCalled(); }); - it('normal operations: db.query should be called when returning promise', () => { + it('normal operations: db.query should be called when returning promise', async () => { const migration = new Migration( dbMock, promiseMigration, @@ -71,12 +71,12 @@ describe('lib/migration', () => { logger ); - return migration.apply('up').then(() => { - expect(queryMock).toHaveBeenCalled(); - }); + await migration.apply('up'); + + expect(queryMock).toHaveBeenCalled(); }); - it('--dry-run option: db.query should not be called', () => { + it('--dry-run option: db.query should not be called', async () => { const migration = new Migration( dbMock, callbackMigration, @@ -86,12 +86,12 @@ describe('lib/migration', () => { logger ); - return migration.apply('up').then(() => { - expect(queryMock).not.toHaveBeenCalled(); - }); + await migration.apply('up'); + + expect(queryMock).not.toHaveBeenCalled(); }); - it('should make proper SQL calls', () => { + it('should make proper SQL calls', async () => { const migration = new Migration( dbMock, promiseMigration, @@ -101,19 +101,19 @@ describe('lib/migration', () => { logger ); - return migration.apply('up').then(() => { - expect(queryMock).toHaveBeenCalledTimes(4); - expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;'); - expect(queryMock).toHaveBeenNthCalledWith( - 2, - expect.stringMatching('CREATE TABLE') - ); - expect(queryMock).toHaveBeenNthCalledWith( - 3, - expect.stringMatching(`INSERT INTO "public"."${migrationsTable}"`) - ); - expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;'); - }); + await migration.apply('up'); + + expect(queryMock).toHaveBeenCalledTimes(4); + expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;'); + expect(queryMock).toHaveBeenNthCalledWith( + 2, + expect.stringMatching('CREATE TABLE') + ); + expect(queryMock).toHaveBeenNthCalledWith( + 3, + expect.stringMatching(`INSERT INTO "public"."${migrationsTable}"`) + ); + expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;'); }); it('should fail with an error message if the migration is invalid', () => { @@ -130,23 +130,16 @@ describe('lib/migration', () => { const direction = 'up'; - let error; - try { - migration.apply(direction); - } catch (err) { - error = err; - } - - // expecting outside the catch block ensures that the test will fail if the - // an exception is not caught - expect(error.toString()).to.include( - `${invalidMigrationName} exporting a '${direction}' function` + expect(() => migration.apply(direction)).toThrow( + new Error( + `Unknown value for direction: ${direction}. Is the migration ${invalidMigrationName} exporting a '${direction}' function?` + ) ); }); }); describe('self.applyDown', () => { - it('normal operations: db.query should be called', () => { + it('normal operations: db.query should be called', async () => { const migration = new Migration( dbMock, callbackMigration, @@ -156,12 +149,12 @@ describe('lib/migration', () => { logger ); - return migration.apply('down').then(() => { - expect(queryMock).toHaveBeenCalled(); - }); + await migration.apply('down'); + + expect(queryMock).toHaveBeenCalled(); }); - it('--dry-run option: db.query should not be called', () => { + it('--dry-run option: db.query should not be called', async () => { const migration = new Migration( dbMock, callbackMigration, @@ -171,12 +164,12 @@ describe('lib/migration', () => { logger ); - return migration.apply('down').then(() => { - expect(queryMock).not.toHaveBeenCalled(); - }); + await migration.apply('down'); + + expect(queryMock).not.toHaveBeenCalled(); }); - it('should make proper SQL calls', () => { + it('should make proper SQL calls', async () => { const migration = new Migration( dbMock, promiseMigration, @@ -186,19 +179,19 @@ describe('lib/migration', () => { logger ); - return migration.apply('down').then(() => { - expect(queryMock).toHaveBeenCalledTimes(4); - expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;'); - expect(queryMock).toHaveBeenNthCalledWith( - 2, - expect.stringMatching('DROP TABLE') - ); - expect(queryMock).toHaveBeenNthCalledWith( - 3, - expect.stringMatching(`DELETE FROM "public"."${migrationsTable}"`) - ); - expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;'); - }); + await migration.apply('down'); + + expect(queryMock).toHaveBeenCalledTimes(4); + expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;'); + expect(queryMock).toHaveBeenNthCalledWith( + 2, + expect.stringMatching('DROP TABLE') + ); + expect(queryMock).toHaveBeenNthCalledWith( + 3, + expect.stringMatching(`DELETE FROM "public"."${migrationsTable}"`) + ); + expect(queryMock).toHaveBeenNthCalledWith(4, 'COMMIT;'); }); }); });