smarter db connection

tests
This commit is contained in:
2018-05-20 23:35:21 +03:00
parent a9b318cb4d
commit 76b6f76758
6 changed files with 1954 additions and 2792 deletions

17
app.js
View File

@@ -1,7 +1,7 @@
require('dotenv').config(); require('dotenv').config();
const config = require('./config'); const config = require('./config');
require('./config/db'); const db = require('./config/db');
const app = require('./config/app'); const app = require('./config/app');
require('./models/TodoList'); require('./models/TodoList');
@@ -48,11 +48,14 @@ app.use((error, req, res, next) => {
next(error); next(error);
}); });
const server = let server;
process.env.NODE_ENV !== 'TEST' || process.env.NODE_ENV !== 'test' if (process.env.NODE_ENV !== 'test') {
? app.listen(config.app.port, () => { db.connect();
console.log('Started!'); server = app.listen(config.app.port, () => {
}) console.log('Started!');
: app; });
} else {
server = app;
}
module.exports = server; module.exports = server;

View File

@@ -4,4 +4,15 @@ const config = require('./');
const { host, port, name } = config.db; const { host, port, name } = config.db;
const connectionString = `mongodb://${host}:${port}/${name}`; const connectionString = `mongodb://${host}:${port}/${name}`;
mongoose.connect(connectionString); async function connect() {
await mongoose.connect(connectionString);
}
async function disconnect() {
await mongoose.disconnect();
}
module.exports = {
connect,
disconnect,
};

4689
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -14,7 +14,6 @@
"body-parser": "^1.18.3", "body-parser": "^1.18.3",
"dotenv": "^5.0.1", "dotenv": "^5.0.1",
"express": "^4.16.3", "express": "^4.16.3",
"https-proxy-agent": "^2.2.1",
"method-override": "^2.3.10", "method-override": "^2.3.10",
"mongoose": "^5.1.1", "mongoose": "^5.1.1",
"morgan": "^1.9.0", "morgan": "^1.9.0",
@@ -28,7 +27,6 @@
"eslint-plugin-import": "^2.12.0", "eslint-plugin-import": "^2.12.0",
"eslint-plugin-jest": "^21.15.1", "eslint-plugin-jest": "^21.15.1",
"jest": "^22.4.4", "jest": "^22.4.4",
"mockgoose": "^7.3.5",
"nodemon": "^1.17.4", "nodemon": "^1.17.4",
"supertest": "^3.1.0" "supertest": "^3.1.0"
}, },

View File

@@ -3,6 +3,8 @@ const server = require('../../app.js');
const request = require('supertest'); const request = require('supertest');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const db = require('../../config/db');
const TodoList = mongoose.model('TodoList'); const TodoList = mongoose.model('TodoList');
const Todo = mongoose.model('Todo'); const Todo = mongoose.model('Todo');
@@ -11,6 +13,8 @@ let listsPopulated;
let todos; let todos;
beforeEach(async () => { beforeEach(async () => {
await db.connect();
// seed lists and todos // seed lists and todos
const list1 = new TodoList({ name: 'List1' }); const list1 = new TodoList({ name: 'List1' });
const todo1 = new Todo({ text: 'Todo1', list: list1._id }); const todo1 = new Todo({ text: 'Todo1', list: list1._id });
@@ -27,12 +31,12 @@ beforeEach(async () => {
}); });
afterEach(async () => { afterEach(async () => {
await mongoose.connection.dropDatabase(); await TodoList.remove({}).exec();
await Todo.remove({}).exec();
await db.disconnect();
}); });
afterAll(async () => { afterAll(async () => {
await mongoose.connection.dropDatabase();
await mongoose.disconnect();
await server.close(); await server.close();
}); });

View File

@@ -3,13 +3,22 @@ const server = require('../../app.js');
const request = require('supertest'); const request = require('supertest');
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const Todo = require('../../models/Todo');
const TodoList = require('../../models/TodoList');
const db = require('../../config/db');
beforeEach(async () => {
await db.connect();
});
afterEach(async () => { afterEach(async () => {
await mongoose.connection.dropDatabase(); await db.disconnect();
}); });
afterAll(async () => { afterAll(async () => {
await mongoose.connection.dropDatabase(); await TodoList.remove({}).exec();
await mongoose.disconnect(); await Todo.remove({}).exec();
await server.close(); await server.close();
}); });