mirror of
https://github.com/usatiuk/ustk-todolist.git
synced 2025-10-28 15:47:48 +01:00
refactor app.js
test not found routes
This commit is contained in:
13
.env.example
13
.env.example
@@ -1,3 +1,12 @@
|
|||||||
PORT=4000
|
DEV_APP_PORT =
|
||||||
|
|
||||||
MONGODB_URI="mongodb://localhost/todolist"
|
DEV_DB_HOST =
|
||||||
|
DEV_DB_PORT =
|
||||||
|
DEV_DB_NAME =
|
||||||
|
|
||||||
|
|
||||||
|
TEST_APP_PORT =
|
||||||
|
|
||||||
|
TEST_DB_HOST =
|
||||||
|
TEST_DB_PORT =
|
||||||
|
TEST_DB_NAME =
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"extends": ["airbnb-base"],
|
"extends": ["airbnb-base", "plugin:jest/recommended"],
|
||||||
|
"plugins": ["jest"],
|
||||||
"rules": {
|
"rules": {
|
||||||
"linebreak-style": "off",
|
"linebreak-style": "off",
|
||||||
"no-unused-expressions": [
|
"no-unused-expressions": [
|
||||||
|
|||||||
32
app.js
32
app.js
@@ -1,30 +1,14 @@
|
|||||||
const express = require('express');
|
|
||||||
const bodyParser = require('body-parser');
|
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const methodOverride = require('method-override');
|
|
||||||
const morgan = require('morgan');
|
|
||||||
|
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
|
|
||||||
const app = express();
|
const config = require('./config');
|
||||||
|
require('./config/db');
|
||||||
app.use(bodyParser.urlencoded({ extended: false }));
|
const app = require('./config/app');
|
||||||
app.use(bodyParser.json());
|
|
||||||
|
|
||||||
app.use(morgan('tiny'));
|
|
||||||
|
|
||||||
app.use(methodOverride('_method'));
|
|
||||||
|
|
||||||
mongoose.connect(process.env.MONGODB_URI);
|
|
||||||
|
|
||||||
require('./models/Todo');
|
require('./models/Todo');
|
||||||
require('./models/TodoList');
|
require('./models/TodoList');
|
||||||
|
|
||||||
const todos = require('./routes/todos');
|
app.use('/todos', require('./routes/todos'));
|
||||||
const lists = require('./routes/lists');
|
app.use('/lists', require('./routes/lists'));
|
||||||
|
|
||||||
app.use('/todos', todos);
|
|
||||||
app.use('/lists', lists);
|
|
||||||
|
|
||||||
// 404 route
|
// 404 route
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
@@ -36,7 +20,7 @@ app.use((req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (req.accepts('json')) {
|
if (req.accepts('json')) {
|
||||||
req.send({ error: 'Not found' });
|
res.send({ error: 'Not found' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +45,8 @@ app.use((error, req, res, next) => {
|
|||||||
next(error);
|
next(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.listen(process.env.PORT, () => {
|
const server = app.listen(config.app.port, () => {
|
||||||
console.log('Started!');
|
console.log('Started!');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module.exports = server;
|
||||||
|
|||||||
15
config/app.js
Normal file
15
config/app.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
const methodOverride = require('method-override');
|
||||||
|
const morgan = require('morgan');
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(bodyParser.urlencoded({ extended: false }));
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
|
app.use(morgan('tiny'));
|
||||||
|
|
||||||
|
app.use(methodOverride('_method'));
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
7
config/db.js
Normal file
7
config/db.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const mongoose = require('mongoose');
|
||||||
|
const config = require('./');
|
||||||
|
|
||||||
|
const { host, port, name } = config.db;
|
||||||
|
const connectionString = `mongodb://${host}:${port}/${name}`;
|
||||||
|
|
||||||
|
mongoose.connect(connectionString);
|
||||||
29
config/index.js
Normal file
29
config/index.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
const env = process.env.NODE_ENV;
|
||||||
|
|
||||||
|
const dev = {
|
||||||
|
app: {
|
||||||
|
port: process.env.DEV_APP_PORT || 4000,
|
||||||
|
},
|
||||||
|
db: {
|
||||||
|
host: process.env.DEV_DB_HOST || 'localhost',
|
||||||
|
port: process.env.DEV_DB_PORT || 27017,
|
||||||
|
name: process.env.DEV_DB_NAME || 'todolist',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const test = {
|
||||||
|
app: {
|
||||||
|
port: process.env.TEST_APP_PORT || 4001,
|
||||||
|
},
|
||||||
|
db: {
|
||||||
|
host: process.env.TEST_DB_HOST || 'localhost',
|
||||||
|
port: process.env.TEST_DB_PORT || 27017,
|
||||||
|
name: process.env.TEST_DB_NAME || 'todolistTest',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
dev,
|
||||||
|
test,
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = config[env] || config.dev;
|
||||||
3015
package-lock.json
generated
3015
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -7,7 +7,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./app.js",
|
"start": "node ./app.js",
|
||||||
"debug": "npx nodemon --inspect ./app.js",
|
"debug": "npx nodemon --inspect ./app.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "jest"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@@ -25,7 +25,13 @@
|
|||||||
"eslint": "^4.19.1",
|
"eslint": "^4.19.1",
|
||||||
"eslint-config-airbnb-base": "^12.1.0",
|
"eslint-config-airbnb-base": "^12.1.0",
|
||||||
"eslint-config-node": "^2.0.0",
|
"eslint-config-node": "^2.0.0",
|
||||||
"eslint-plugin-import": "^2.11.0",
|
"eslint-plugin-import": "^2.12.0",
|
||||||
"nodemon": "^1.17.4"
|
"eslint-plugin-jest": "^21.15.1",
|
||||||
|
"jest": "^22.4.4",
|
||||||
|
"nodemon": "^1.17.4",
|
||||||
|
"supertest": "^3.1.0"
|
||||||
|
},
|
||||||
|
"jest": {
|
||||||
|
"testEnvironment": "node"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
37
tests/integration/root.test.js
Normal file
37
tests/integration/root.test.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
const server = require('../../app.js');
|
||||||
|
|
||||||
|
const request = require('supertest');
|
||||||
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await mongoose.connection.dropDatabase();
|
||||||
|
await mongoose.disconnect();
|
||||||
|
await server.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Test not found', () => {
|
||||||
|
test('respond not found with json', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get('/')
|
||||||
|
.set('Accept', 'application/json')
|
||||||
|
.expect(404)
|
||||||
|
.expect('Content-Type', 'application/json; charset=utf-8');
|
||||||
|
expect(response.body).toEqual({ error: 'Not found' });
|
||||||
|
});
|
||||||
|
test('respond not found with html', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get('/')
|
||||||
|
.set('Accept', 'text/html')
|
||||||
|
.expect(404)
|
||||||
|
.expect('Content-Type', 'text/html; charset=utf-8');
|
||||||
|
expect(response.text).toEqual('404');
|
||||||
|
});
|
||||||
|
test('respond not found with plain text', async () => {
|
||||||
|
const response = await request(server)
|
||||||
|
.get('/')
|
||||||
|
.set('Accept', 'text/plain')
|
||||||
|
.expect(404)
|
||||||
|
.expect('Content-Type', 'text/plain; charset=utf-8');
|
||||||
|
expect(response.text).toEqual('not found');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user