Skip to content

Commit

Permalink
rewrite to use async expect
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 5, 2024
1 parent acd26dc commit a267968
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 69 deletions.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions test/db.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down
111 changes: 52 additions & 59 deletions test/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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', () => {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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;');
});
});
});

0 comments on commit a267968

Please sign in to comment.