Skip to content

Commit 1eb0fcd

Browse files
committed
add database and cache mock
1 parent abb5184 commit 1eb0fcd

File tree

12 files changed

+23
-1
lines changed

12 files changed

+23
-1
lines changed

src/database/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ process.on('SIGINT', () => {
6363
process.exit(0);
6464
});
6565
});
66+
67+
export const connection = mongoose.connection;

tests/auth/apikey/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '../../database/mock';
2+
import '../../cache/mock';
13
import { API_KEY, mockFindApiKey } from './mock'; // mock should be imported on the top
24
import app from '../../../src/app';
35
import supertest from 'supertest';

tests/auth/authentication/unit.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import '../../database/mock';
2+
import '../../cache/mock';
3+
14
import {
25
ACCESS_TOKEN,
36
addHeaders,

tests/auth/authorization/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '../../database/mock';
2+
import '../../cache/mock';
13
import { addAuthHeaders } from '../authentication/mock';
24

35
// import the mock for the current test after all other mock imports

tests/cache/mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.mock('../../src/cache', () => ({}));

tests/database/mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.mock('../../src/database', () => ({}));

tests/routes/v1/blog/index/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '../../../../database/mock';
2+
import '../../../../cache/mock';
13
import { addHeaders } from '../../../../auth/authentication/mock';
24

35
import {

tests/routes/v1/blog/writer/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '../../../../database/mock';
2+
import '../../../../cache/mock';
13
import { addAuthHeaders } from '../../../../auth/authentication/mock';
24

35
// this import should be below authentication/mock to override for role validation to work

tests/routes/v1/login/integration.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import * as authUtils from '../../../../src/auth/authUtils';
1010
import Role, { RoleCode } from '../../../../src/database/model/Role';
1111
import { Types } from 'mongoose';
1212
import ApiKey, { ApiKeyModel } from '../../../../src/database/model/ApiKey';
13+
import { connection } from '../../../../src/database';
14+
import cache from '../../../../src/cache';
1315

1416
export const createTokensSpy = jest.spyOn(authUtils, 'createTokens');
1517
export const bcryptCompareSpy = jest.spyOn(bcrypt, 'compare');
@@ -41,6 +43,8 @@ describe('Login basic route', () => {
4143

4244
afterAll(async () => {
4345
await UserModel.remove({}); // delete all data from user table
46+
connection.close();
47+
cache.disconnect();
4448
});
4549

4650
beforeEach(() => {

tests/routes/v1/login/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '../../../../database/mock';
2+
import '../../../../cache/mock';
13
import { addHeaders } from '../../../auth/authentication/mock';
24

35
// the mock for this class should be below all other mock imports

tests/routes/v1/signup/mock.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-unused-vars */
21
import { mockUserFindByEmail } from '../login/mock';
32
import User from '../../../../src/database/model/User';
43
import Keystore from '../../../../src/database/model/Keystore';

tests/routes/v1/signup/unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// importing any mock file let the jest load all the mocks defined in that file
2+
import '../../../../database/mock';
3+
import '../../../../cache/mock';
24
import { addHeaders } from '../../../auth/authentication/mock';
35
import { mockUserFindByEmail, createTokensSpy, USER_EMAIL, USER_PASSWORD } from '../login/mock';
46

0 commit comments

Comments
 (0)