Skip to content

Commit

Permalink
Fix heroku error (#57)
Browse files Browse the repository at this point in the history
* Fix heroku error

* fix import
  • Loading branch information
krygacz authored Mar 20, 2022
1 parent d66a988 commit 050bc61
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 33 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
"coverageReporters": [
"json",
"html"
],
"setupFilesAfterEnv": [
"<rootDir>/src/setupTests.js"
]
},
"babel": {
Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth.test.js
Original file line number Diff line number Diff line change
@@ -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: `[email protected]`,
Expand Down Expand Up @@ -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 () => {
Expand Down
File renamed without changes.
19 changes: 14 additions & 5 deletions src/helpers/dbConnection.js
Original file line number Diff line number Diff line change
@@ -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();
};
16 changes: 0 additions & 16 deletions src/helpers/productionDbConnection.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/offer/offer.test.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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 () => {
Expand Down
5 changes: 2 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.mock('./helpers/dbConnection');
6 changes: 3 additions & 3 deletions src/user/user.test.js
Original file line number Diff line number Diff line change
@@ -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: `[email protected]`,
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 050bc61

Please sign in to comment.