Skip to content

Commit dc91458

Browse files
author
hirsch88
committed
Add integration tests
1 parent 3c9b9da commit dc91458

File tree

8 files changed

+84
-9
lines changed

8 files changed

+84
-9
lines changed

.env.test

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@ AUTH_ROUTE="http://localhost:3333/tokeninfo"
2121
#
2222
# DATABASE
2323
#
24-
DB_TYPE="mysql"
25-
DB_HOST="localhost"
26-
DB_PORT=3306
27-
DB_USERNAME="root"
28-
DB_PASSWORD=""
29-
DB_DATABASE="my_database"
30-
DB_SYNCHRONIZE=false
24+
DB_TYPE="sqlite"
25+
DB_DATABASE="./mydb.sql"
3126
DB_LOGGING=false
3227

3328
#

mydb.sql

28 KB
Binary file not shown.

package-scripts.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ module.exports = {
104104
script: 'nps "test --coverage"'
105105
}
106106
},
107+
integration: {
108+
default: {
109+
script: series(
110+
'nps banner.test',
111+
'nps test.integration.pretest',
112+
'nps test.integration.run'
113+
)
114+
},
115+
pretest: {
116+
script: 'tslint -c ./tslint.json -t stylish ./test/integration/**/*.ts'
117+
},
118+
verbose: {
119+
script: 'nps "test.integration --verbose"'
120+
},
121+
run: {
122+
script: 'cross-env NODE_ENV=test jest --testPathPattern=integration -i'
123+
},
124+
},
107125
e2e: {
108126
default: {
109127
script: series(

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"mock-express-request": "^0.2.0",
117117
"mock-express-response": "^0.2.1",
118118
"nock": "^9.1.0",
119+
"sqlite3": "^3.1.13",
119120
"ts-jest": "^21.1.4"
120121
}
121122
}

src/api/models/Pet.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export class Pet {
1717
@Column()
1818
public age: number;
1919

20-
@Column()
20+
@Column({
21+
nullable: true,
22+
})
2123
public userId: number;
2224

2325
@ManyToOne(type => User, user => user.pets)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Container } from 'typedi';
2+
import { createConnection, useContainer, Connection } from 'typeorm';
3+
4+
import { Pet } from '../../src/api/models/Pet';
5+
import { PetService } from './../../src/api/services/PetService';
6+
import { createDatabaseConnection, prepareDatabase, closeDatabase } from './utils/database';
7+
8+
describe('PetService', () => {
9+
10+
let connection: Connection;
11+
beforeAll(async () => connection = await createDatabaseConnection());
12+
beforeEach(() => prepareDatabase(connection));
13+
afterAll(() => closeDatabase(connection));
14+
15+
test('should create a new pet in the database', async (done) => {
16+
const pet = new Pet();
17+
pet.name = 'test';
18+
pet.age = 1;
19+
const service = Container.get<PetService>(PetService);
20+
const resultCreate = await service.create(pet);
21+
expect(resultCreate.name).toBe(pet.name);
22+
expect(resultCreate.age).toBe(pet.age);
23+
24+
const resultFind = await service.findOne(resultCreate.id);
25+
expect(resultFind.name).toBe(pet.name);
26+
expect(resultFind.age).toBe(pet.age);
27+
done();
28+
});
29+
30+
});

test/integration/utils/database.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Container } from 'typedi';
2+
import { createConnection, useContainer, Connection } from 'typeorm';
3+
import { env } from '../../../src/core/env';
4+
5+
export const createDatabaseConnection = async (): Promise<Connection> => {
6+
useContainer(Container);
7+
const connection = await createConnection({
8+
type: env.db.type as any, // See createConnection options for valid types
9+
database: env.db.database,
10+
logging: env.db.logging,
11+
entities: env.app.dirs.entities,
12+
});
13+
return connection;
14+
};
15+
16+
export const prepareDatabase = (connection: Connection) => {
17+
return connection.synchronize(true);
18+
};

yarn.lock

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2923,6 +2923,10 @@ nan@^2.3.0:
29232923
version "2.8.0"
29242924
resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a"
29252925

2926+
nan@~2.7.0:
2927+
version "2.7.0"
2928+
resolved "https://registry.yarnpkg.com/nan/-/nan-2.7.0.tgz#d95bf721ec877e08db276ed3fc6eb78f9083ad46"
2929+
29262930
natural-compare@^1.4.0:
29272931
version "1.4.0"
29282932
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -2972,7 +2976,7 @@ node-notifier@^5.0.2:
29722976
shellwords "^0.1.0"
29732977
which "^1.2.12"
29742978

2975-
node-pre-gyp@^0.6.39:
2979+
node-pre-gyp@^0.6.39, node-pre-gyp@~0.6.38:
29762980
version "0.6.39"
29772981
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.6.39.tgz#c00e96860b23c0e1420ac7befc5044e1d78d8649"
29782982
dependencies:
@@ -3916,6 +3920,13 @@ sprintf-js@~1.0.2:
39163920
version "1.0.3"
39173921
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
39183922

3923+
sqlite3@^3.1.13:
3924+
version "3.1.13"
3925+
resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-3.1.13.tgz#d990a05627392768de6278bafd1a31fdfe907dd9"
3926+
dependencies:
3927+
nan "~2.7.0"
3928+
node-pre-gyp "~0.6.38"
3929+
39193930
sqlstring@2.3.0:
39203931
version "2.3.0"
39213932
resolved "https://registry.yarnpkg.com/sqlstring/-/sqlstring-2.3.0.tgz#525b8a4fd26d6f71aa61e822a6caf976d31ad2a8"

0 commit comments

Comments
 (0)