diff --git a/package.json b/package.json index 8e34bab..313070a 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,9 @@ "coverageReporters": [ "json", "html" + ], + "setupFilesAfterEnv": [ + "/src/setupTests.js" ] }, "babel": { diff --git a/src/auth/auth.test.js b/src/auth/auth.test.js index e2fba0f..02c08c7 100644 --- a/src/auth/auth.test.js +++ b/src/auth/auth.test.js @@ -1,6 +1,6 @@ import request from 'supertest'; import { app } from '../app.js'; -import dbConnection from '../helpers/dbConnection.js'; +import { connect, disconnect } from '../helpers/dbConnection.js'; const userBody = { email: `example@test.pl`, @@ -28,10 +28,10 @@ jest.mock('nodemailer', () => ({ describe('auth endpoints', () => { beforeAll(async () => { - await dbConnection.connect(); + await connect(); }); afterAll(async () => { - await dbConnection.disconnect(); + await disconnect(); }); it('should allow a POST to /auth/register with correct data', async () => { diff --git a/src/helpers/testDbConnection.js b/src/helpers/__mocks__/dbConnection.js similarity index 100% rename from src/helpers/testDbConnection.js rename to src/helpers/__mocks__/dbConnection.js diff --git a/src/helpers/dbConnection.js b/src/helpers/dbConnection.js index 5589f92..cd5891b 100644 --- a/src/helpers/dbConnection.js +++ b/src/helpers/dbConnection.js @@ -1,7 +1,16 @@ -import * as productionDbConnection from './productionDbConnection.js'; -import * as testDbConnection from './testDbConnection.js'; +import mongoose from 'mongoose'; -const dbConnection = - process.env.NODE_ENV === 'test' ? testDbConnection : productionDbConnection; +export const connect = async () => { + try { + await mongoose.connect(process.env.MONGO_URL, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); + } catch (error) { + console.log(error.message); + } +}; -export default dbConnection; +export const disconnect = () => { + return mongoose.disconnect(); +}; diff --git a/src/helpers/productionDbConnection.js b/src/helpers/productionDbConnection.js deleted file mode 100644 index cd5891b..0000000 --- a/src/helpers/productionDbConnection.js +++ /dev/null @@ -1,16 +0,0 @@ -import mongoose from 'mongoose'; - -export const connect = async () => { - try { - await mongoose.connect(process.env.MONGO_URL, { - useNewUrlParser: true, - useUnifiedTopology: true, - }); - } catch (error) { - console.log(error.message); - } -}; - -export const disconnect = () => { - return mongoose.disconnect(); -}; diff --git a/src/offer/offer.test.js b/src/offer/offer.test.js index f12dab5..fe4b926 100644 --- a/src/offer/offer.test.js +++ b/src/offer/offer.test.js @@ -1,7 +1,7 @@ import request from 'supertest'; import { app } from '../app.js'; import { USER_ROLE } from '../constants.js'; -import dbConnection from '../helpers/dbConnection.js'; +import { connect, disconnect } from '../helpers/dbConnection.js'; const offerBody = { title: 'Super oferta', @@ -88,12 +88,12 @@ const registerUsers = async () => { describe('offer endpoints', () => { beforeAll(async () => { - await dbConnection.connect(); + await connect(); await registerUsers(); }); afterAll(async () => { - await dbConnection.disconnect(); + await disconnect(); }); it('should allow a POST to /offer when authorized', async () => { diff --git a/src/server.js b/src/server.js index f822795..ae457f9 100644 --- a/src/server.js +++ b/src/server.js @@ -1,8 +1,7 @@ import { app } from './app.js'; -import dbConnection from './helpers/dbConnection.js'; +import { connect } from './helpers/dbConnection.js'; -dbConnection - .connect() +connect() .then(() => app.listen(process.env.PORT, () => { console.log('Server is listing on port', process.env.PORT); diff --git a/src/setupTests.js b/src/setupTests.js new file mode 100644 index 0000000..e09d90f --- /dev/null +++ b/src/setupTests.js @@ -0,0 +1 @@ +jest.mock('./helpers/dbConnection'); diff --git a/src/user/user.test.js b/src/user/user.test.js index 74d6f44..3ee2726 100644 --- a/src/user/user.test.js +++ b/src/user/user.test.js @@ -1,7 +1,7 @@ import request from 'supertest'; import { app } from '../app.js'; import { USER_ROLE } from '../constants.js'; -import dbConnection from '../helpers/dbConnection.js'; +import { connect, disconnect } from '../helpers/dbConnection.js'; const userBody = { email: `user@test.pl`, @@ -60,13 +60,13 @@ jest.mock('nodemailer', () => ({ describe('user endpoints', () => { beforeAll(async () => { - await dbConnection.connect(); + await connect(); await registerUsers(); await getUsersData(adminToken); }); afterAll(async () => { - await dbConnection.disconnect(); + await disconnect(); }); describe('admin user', () => {