File tree 10 files changed +83
-4
lines changed
10 files changed +83
-4
lines changed Original file line number Diff line number Diff line change 44
44
PGUSER : postgres
45
45
PGPASSWORD : postgres
46
46
47
- - name : Run tests
48
- run : npm run test -- --coverage
47
+ - name : Run unit tests
48
+ run : npm run test:unit -- --coverage
49
+
50
+ - name : Run integration tests
51
+ run : npm run test:integration -- --coverage
52
+ env :
53
+ PGHOST : localhost
54
+ PGPORT : 5432
55
+ PGUSER : postgres
56
+ PGPASSWORD : postgres
49
57
50
58
- name : Upload coverage
51
59
uses : codecov/codecov-action@v2
Original file line number Diff line number Diff line change @@ -24,7 +24,14 @@ In order to run this software you need to set up a [Postgres 14](https://www.pos
24
24
edit .env
25
25
```
26
26
27
- 3 . Run migrations
27
+ 3 . Set up test environment variables
28
+
29
+ ``` bash
30
+ cp .env.example .env.test
31
+ edit .env.test
32
+ ```
33
+
34
+ 4 . Run migrations
28
35
29
36
``` bash
30
37
npm run migrate:dev
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ module.exports = {
6
6
} ,
7
7
collectCoverageFrom : [ "src/**/*.ts" ] ,
8
8
preset : 'ts-jest' ,
9
+ setupFiles : [ '<rootDir>/src/test/setupEnv.ts' ] ,
9
10
testEnvironment : 'node' ,
10
11
testPathIgnorePatterns : [ "<rootDir>/dist/" ] ,
11
12
silent : true ,
Original file line number Diff line number Diff line change
1
+ var config = require ( './jest.config.base.js' ) ;
2
+ config . testMatch = [ '**/?(*.)+(int).(spec|test).[jt]s?(x)' ] ;
3
+ config . setupFilesAfterEnv = [ "<rootDir>/src/test/integrationSuiteSetup.ts" ] ;
4
+ module . exports = config ;
Original file line number Diff line number Diff line change
1
+ var config = require ( './jest.config.base.js' ) ;
2
+ config . testMatch = [ '**/?(*.)+(unit).(spec|test).[jt]s?(x)' ] ;
3
+ module . exports = config ;
Original file line number Diff line number Diff line change 11
11
"lint" : " eslint ./src --ext .ts" ,
12
12
"migrate" : " node -r dotenv/config dist/scripts/migrate.js" ,
13
13
"migrate:dev" : " ts-node -r dotenv/config src/scripts/migrate.ts | pino-pretty" ,
14
- "test" : " jest" ,
14
+ "test" : " npm run test:unit && npm run test:integration" ,
15
+ "test:unit" : " jest --config=jest.config.unit.js" ,
16
+ "test:integration" : " jest --config=jest.config.int.js --runInBand" ,
15
17
"start" : " node dist/index.js" ,
16
18
"start:dev" : " ts-node src/index.ts | pino-pretty"
17
19
},
Original file line number Diff line number Diff line change 1
1
import request from 'supertest' ;
2
2
import { app } from '../app' ;
3
+ import {
4
+ prepareDatabaseForCurrentWorker ,
5
+ cleanupDatabaseForCurrentWorker ,
6
+ } from '../test/utils' ;
3
7
4
8
const agent = request . agent ( app ) ;
5
9
6
10
describe ( '/canonicalFields' , ( ) => {
7
11
describe ( '/' , ( ) => {
8
12
it ( 'should return HTTP Status Code 200 OK' , async ( ) => {
13
+ await prepareDatabaseForCurrentWorker ( ) ;
9
14
await agent
10
15
. get ( '/canonicalFields' )
11
16
. expect ( 200 ) ;
17
+ await cleanupDatabaseForCurrentWorker ( ) ;
12
18
} ) ;
13
19
} ) ;
14
20
} ) ;
Original file line number Diff line number Diff line change
1
+ import { db } from '../database' ;
2
+
3
+ afterAll ( async ( ) => {
4
+ await db . close ( ) ;
5
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import dotenv from 'dotenv' ;
2
+
3
+ dotenv . config ( { path : '.env.test' } ) ;
Original file line number Diff line number Diff line change
1
+ import {
2
+ db ,
3
+ migrate ,
4
+ } from '../database' ;
5
+
6
+ const generateSchemaName = ( workerId : string ) : string => `test_${ workerId } ` ;
7
+
8
+ const getSchemaNameForCurrentTestWorker = ( ) : string => {
9
+ if ( process . env . NODE_ENV !== 'test' ) {
10
+ throw new Error ( 'You cannot get a test schema name outside of a test environment.' ) ;
11
+ }
12
+ if ( process . env . JEST_WORKER_ID === undefined ) {
13
+ throw new Error ( 'You cannot get a test schema name if jest has not specified a worker ID.' ) ;
14
+ }
15
+ return generateSchemaName ( process . env . JEST_WORKER_ID ) ;
16
+ } ;
17
+
18
+ const createSchema = async ( schemaName : string ) : Promise < void > => {
19
+ await db . query ( `CREATE SCHEMA IF NOT EXISTS ${ schemaName } ;` ) ;
20
+ } ;
21
+
22
+ const setSchema = async ( schemaName : string ) : Promise < void > => {
23
+ await db . query ( `SET search_path TO ${ schemaName } ;` ) ;
24
+ } ;
25
+
26
+ const dropSchema = async ( schemaName : string ) : Promise < void > => {
27
+ await db . query ( `DROP SCHEMA ${ schemaName } CASCADE;` ) ;
28
+ } ;
29
+
30
+ export const prepareDatabaseForCurrentWorker = async ( ) : Promise < void > => {
31
+ const schemaName = getSchemaNameForCurrentTestWorker ( ) ;
32
+ await createSchema ( schemaName ) ;
33
+ await setSchema ( schemaName ) ;
34
+ await migrate ( ) ;
35
+ } ;
36
+
37
+ export const cleanupDatabaseForCurrentWorker = async ( ) : Promise < void > => {
38
+ const schemaName = getSchemaNameForCurrentTestWorker ( ) ;
39
+ await dropSchema ( schemaName ) ;
40
+ } ;
You can’t perform that action at this time.
0 commit comments