Skip to content

Commit b3a7468

Browse files
committed
fix: fix db tests
1 parent 096ba47 commit b3a7468

File tree

4 files changed

+47
-33
lines changed

4 files changed

+47
-33
lines changed

jest.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ module.exports = {
1010
collectCoverageFrom: [
1111
'<rootDir>/src/**/*.{ts,js}',
1212
'!<rootDir>/src/index.{ts,js}',
13-
'!<rootDir>/src/database/*.{ts,js}',
1413
],
1514
coverageThreshold: {
1615
global: {

src/database/__mocks__/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DataSource } from 'typeorm';
2+
3+
const AppDataSource = new DataSource({
4+
type: 'sqlite',
5+
database: ':memory:',
6+
synchronize: true,
7+
logging: false,
8+
entities: ['src/entities/*.*'],
9+
migrations: [],
10+
subscribers: [],
11+
});
12+
13+
async function initialiseDataSource(): Promise<void> {
14+
try {
15+
await AppDataSource.initialize();
16+
} catch (err) {
17+
console.error('Error during Data Source initialization', err);
18+
}
19+
}
20+
21+
async function destroyDataSource(): Promise<void> {
22+
await AppDataSource.destroy();
23+
}
24+
25+
export { AppDataSource, initialiseDataSource, destroyDataSource };

src/database/index.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
/* istanbul ignore file */
12
import { DataSource } from 'typeorm';
23

3-
// @TODO: Use jest mocks instead of this crap
4-
const AppDataSource = process.env.JEST_WORKER_ID ? getTestDataSource() : getAppDataSource();
4+
const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE } = process.env;
5+
const AppDataSource = new DataSource({
6+
type: 'mysql',
7+
host: DB_HOST,
8+
port: Number(DB_PORT),
9+
username: DB_USERNAME,
10+
password: DB_PASSWORD,
11+
database: DB_DATABASE,
12+
synchronize: true,
13+
logging: false,
14+
entities: ['src/entities/*.*'],
15+
migrations: [],
16+
subscribers: [],
17+
});
518

619
async function initialiseDataSource(): Promise<void> {
720
try {
@@ -11,33 +24,8 @@ async function initialiseDataSource(): Promise<void> {
1124
}
1225
}
1326

14-
function getAppDataSource(): DataSource {
15-
const { DB_HOST, DB_PORT, DB_USERNAME, DB_PASSWORD, DB_DATABASE } = process.env;
16-
return new DataSource({
17-
type: 'mysql',
18-
host: DB_HOST,
19-
port: Number(DB_PORT),
20-
username: DB_USERNAME,
21-
password: DB_PASSWORD,
22-
database: DB_DATABASE,
23-
synchronize: true,
24-
logging: false,
25-
entities: ['src/entities/*.*'],
26-
migrations: [],
27-
subscribers: [],
28-
});
27+
async function destroyDataSource(): Promise<void> {
28+
await AppDataSource.destroy();
2929
}
3030

31-
function getTestDataSource(): DataSource {
32-
return new DataSource({
33-
type: 'sqlite',
34-
database: ':memory:',
35-
synchronize: true,
36-
logging: false,
37-
entities: ['src/entities/*.*'],
38-
migrations: [],
39-
subscribers: [],
40-
});
41-
}
42-
43-
export { AppDataSource, initialiseDataSource };
31+
export { AppDataSource, initialiseDataSource, destroyDataSource };

tests/resolvers/RecipeResolver.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import 'reflect-metadata';
22
import { createApolloServer } from '../../src/services/ApolloServices/ApolloServerService';
3+
import { AppDataSource, initialiseDataSource, destroyDataSource } from '../../src/database';
34
import { Recipe } from '../../src/entities/Recipe';
4-
import { AppDataSource, initialiseDataSource } from '../../src/database';
5+
6+
jest.mock('../../src/database');
57

68
describe('RecipeResolver', () => {
79
beforeAll(async () => {
@@ -16,7 +18,7 @@ describe('RecipeResolver', () => {
1618
});
1719

1820
afterAll(async () => {
19-
await AppDataSource.destroy();
21+
await destroyDataSource();
2022
});
2123

2224
it('gets recipes', async () => {

0 commit comments

Comments
 (0)