mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 07:37:49 +01:00
smarter db connection
tests
This commit is contained in:
17
app.js
17
app.js
@@ -1,7 +1,7 @@
|
||||
require('dotenv').config();
|
||||
|
||||
const config = require('./config');
|
||||
require('./config/db');
|
||||
const db = require('./config/db');
|
||||
const app = require('./config/app');
|
||||
|
||||
require('./models/TodoList');
|
||||
@@ -48,11 +48,14 @@ app.use((error, req, res, next) => {
|
||||
next(error);
|
||||
});
|
||||
|
||||
const server =
|
||||
process.env.NODE_ENV !== 'TEST' || process.env.NODE_ENV !== 'test'
|
||||
? app.listen(config.app.port, () => {
|
||||
console.log('Started!');
|
||||
})
|
||||
: app;
|
||||
let server;
|
||||
if (process.env.NODE_ENV !== 'test') {
|
||||
db.connect();
|
||||
server = app.listen(config.app.port, () => {
|
||||
console.log('Started!');
|
||||
});
|
||||
} else {
|
||||
server = app;
|
||||
}
|
||||
|
||||
module.exports = server;
|
||||
|
||||
13
config/db.js
13
config/db.js
@@ -4,4 +4,15 @@ const config = require('./');
|
||||
const { host, port, name } = config.db;
|
||||
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
4689
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -14,7 +14,6 @@
|
||||
"body-parser": "^1.18.3",
|
||||
"dotenv": "^5.0.1",
|
||||
"express": "^4.16.3",
|
||||
"https-proxy-agent": "^2.2.1",
|
||||
"method-override": "^2.3.10",
|
||||
"mongoose": "^5.1.1",
|
||||
"morgan": "^1.9.0",
|
||||
@@ -28,7 +27,6 @@
|
||||
"eslint-plugin-import": "^2.12.0",
|
||||
"eslint-plugin-jest": "^21.15.1",
|
||||
"jest": "^22.4.4",
|
||||
"mockgoose": "^7.3.5",
|
||||
"nodemon": "^1.17.4",
|
||||
"supertest": "^3.1.0"
|
||||
},
|
||||
|
||||
@@ -3,6 +3,8 @@ const server = require('../../app.js');
|
||||
const request = require('supertest');
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const db = require('../../config/db');
|
||||
|
||||
const TodoList = mongoose.model('TodoList');
|
||||
const Todo = mongoose.model('Todo');
|
||||
|
||||
@@ -11,6 +13,8 @@ let listsPopulated;
|
||||
let todos;
|
||||
|
||||
beforeEach(async () => {
|
||||
await db.connect();
|
||||
|
||||
// seed lists and todos
|
||||
const list1 = new TodoList({ name: 'List1' });
|
||||
const todo1 = new Todo({ text: 'Todo1', list: list1._id });
|
||||
@@ -27,12 +31,12 @@ beforeEach(async () => {
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await mongoose.connection.dropDatabase();
|
||||
await TodoList.remove({}).exec();
|
||||
await Todo.remove({}).exec();
|
||||
await db.disconnect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mongoose.connection.dropDatabase();
|
||||
await mongoose.disconnect();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
|
||||
@@ -3,13 +3,22 @@ const server = require('../../app.js');
|
||||
const request = require('supertest');
|
||||
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 () => {
|
||||
await mongoose.connection.dropDatabase();
|
||||
await db.disconnect();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await mongoose.connection.dropDatabase();
|
||||
await mongoose.disconnect();
|
||||
await TodoList.remove({}).exec();
|
||||
await Todo.remove({}).exec();
|
||||
await server.close();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user