-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration tests are slower than unit tests because they rely on a database connection, so we want the ability to run them separately depending on our environment. Jest allows us to define different config files. By creating a `base` config we can define all of the common settings between unit and integration tests, and then customize for each type in the respective extended configs. In addition to configuration this begins to define define some hooks so that we can eventually run these tests in parallel. Unfortunately there is a bug in the migration library we're using which prevents that kind of parallel migration within a single database / across multiple schemas. We have an open PR with a patch for that bug[1]. I took out the explicit running of migrations because our integration tests should now cover that (as they do run migrations). [1] ThomWright/postgres-migrations#93 Issue #43 Support integration tests
- Loading branch information
Showing
10 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var config = require('./jest.config.base.js'); | ||
config.testMatch = ['**/?(*.)+(int).(spec|test).[jt]s?(x)']; | ||
config.setupFilesAfterEnv = ["<rootDir>/src/test/integrationSuiteSetup.ts"]; | ||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var config = require('./jest.config.base.js'); | ||
config.testMatch = ['**/?(*.)+(unit).(spec|test).[jt]s?(x)']; | ||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/__tests__/canonicalFields.test.ts → src/__tests__/canonicalFields.int.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import request from 'supertest'; | ||
import { app } from '../app'; | ||
import { | ||
prepareDatabaseForCurrentWorker, | ||
cleanupDatabaseForCurrentWorker, | ||
} from '../test/utils'; | ||
|
||
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { db } from '../database'; | ||
|
||
afterAll(async () => { | ||
await db.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config({ path: '.env.test' }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
db, | ||
migrate, | ||
} from '../database'; | ||
|
||
const generateSchemaName = (workerId: string): string => `test_${workerId}`; | ||
|
||
const getSchemaNameForCurrentTestWorker = (): string => { | ||
if (process.env.NODE_ENV !== 'test') { | ||
throw new Error('You cannot get a test schema name outside of a test environment.'); | ||
} | ||
if (process.env.JEST_WORKER_ID === undefined) { | ||
throw new Error('You cannot get a test schema name if jest has not specified a worker ID.'); | ||
} | ||
return generateSchemaName(process.env.JEST_WORKER_ID); | ||
}; | ||
|
||
const createSchema = async (schemaName: string): Promise<void> => { | ||
await db.query(`CREATE SCHEMA IF NOT EXISTS ${schemaName};`); | ||
}; | ||
|
||
const setSchema = async (schemaName: string): Promise<void> => { | ||
await db.query(`SET search_path TO ${schemaName};`); | ||
}; | ||
|
||
const dropSchema = async (schemaName: string): Promise<void> => { | ||
await db.query(`DROP SCHEMA ${schemaName} CASCADE;`); | ||
}; | ||
|
||
export const prepareDatabaseForCurrentWorker = async (): Promise<void> => { | ||
const schemaName = getSchemaNameForCurrentTestWorker(); | ||
await createSchema(schemaName); | ||
await setSchema(schemaName); | ||
await migrate(); | ||
}; | ||
|
||
export const cleanupDatabaseForCurrentWorker = async (): Promise<void> => { | ||
const schemaName = getSchemaNameForCurrentTestWorker(); | ||
await dropSchema(schemaName); | ||
}; |