Skip to content

Commit

Permalink
Run database setup and teardown via suite
Browse files Browse the repository at this point in the history
Manually calling the DB setup / teardown within a test was verbose, but
more importantly it meant teardown wouldn't happen if a test failed.
That's no good!

By using Jest's suite functionality we ensure that setup and teardown
both run for each test regardless of test outcome.

Issue #43 Support integration tests
  • Loading branch information
slifty committed Jul 21, 2022
1 parent 35b82d7 commit 2b44e55
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"migrate": "node -r dotenv/config dist/scripts/migrate.js",
"migrate:dev": "ts-node -r dotenv/config src/scripts/migrate.ts | pino-pretty",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "jest --config=jest.config.unit.js | pino-pretty",
"test:integration": "jest --config=jest.config.int.js --runInBand | pino-pretty",
"test:unit": "jest --config=jest.config.unit.js",
"test:integration": "jest --config=jest.config.int.js --runInBan",
"start": "node dist/index.js",
"start:dev": "ts-node src/index.ts | pino-pretty"
},
Expand Down
6 changes: 0 additions & 6 deletions src/__tests__/canonicalFields.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import request from 'supertest';
import { app } from '../app';
import {
prepareDatabaseForCurrentWorker,
cleanupDatabaseForCurrentWorker,
} from '../test/harnessFunctions';

const agent = request.agent(app);

describe('/canonicalFields', () => {
describe('/', () => {
it('should return HTTP Status Code 200 OK', async () => {
await prepareDatabaseForCurrentWorker();
await agent
.get('/canonicalFields')
.expect(200);
await cleanupDatabaseForCurrentWorker();
});
});
});
12 changes: 12 additions & 0 deletions src/test/integrationSuiteSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
* via `setupFilesAfterEnv`.
*/
import { db } from '../database';
import {
prepareDatabaseForCurrentWorker,
cleanupDatabaseForCurrentWorker,
} from './harnessFunctions'

afterAll(async () => {
await db.close();
});

beforeEach(async () => {
await prepareDatabaseForCurrentWorker();
})

afterEach(async () => {
await cleanupDatabaseForCurrentWorker();
})

0 comments on commit 2b44e55

Please sign in to comment.