diff --git a/.eslintrc b/.eslintrc index f35c29ad..25adcd63 100644 --- a/.eslintrc +++ b/.eslintrc @@ -21,14 +21,27 @@ // TODO: turn on later "comma-dangle": [ 0 + ], + "import/no-extraneous-dependencies": [ + "error", + { + "devDependencies": true + } + ], + "no-underscore-dangle": [ + 0 ] }, "env": { "node": true, "mocha": true }, + "parserOptions": { + "ecmaVersion": 6, + "sourceType": "module" + }, "extends": [ "eslint:recommended", - "airbnb/base" + "airbnb-base" ] } diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 92b6c992..76f1c2c1 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -37,20 +37,6 @@ gulp.task('set-env', () => { }); }); -// Lint Javascript -gulp.task('lint', () => - gulp.src(paths.js) - // eslint() attaches the lint output to the "eslint" property - // of the file object so it can be used by other modules. - .pipe(plugins.eslint()) - // eslint.format() outputs the lint results to the console. - // Alternatively use eslint.formatEach() (see Docs). - .pipe(plugins.eslint.format()) - // To have the process exit with an error code (1) on - // lint error, return the stream and pipe to failAfterError last. - .pipe(plugins.eslint.failAfterError()) -); - // Copy non-js files to dist gulp.task('copy', () => gulp.src(paths.nonJs) @@ -74,12 +60,12 @@ gulp.task('babel', () => ); // Start server with restart on file changes -gulp.task('nodemon', ['lint', 'copy', 'babel'], () => +gulp.task('nodemon', ['copy', 'babel'], () => plugins.nodemon({ script: path.join('dist', 'index.js'), ext: 'js', ignore: ['node_modules/**/*.js', 'dist/**/*.js'], - tasks: ['lint', 'copy', 'babel'] + tasks: ['copy', 'babel'] }) ); @@ -98,7 +84,7 @@ gulp.task('pre-test', () => // triggers mocha test with code coverage gulp.task('test', ['pre-test', 'set-env'], () => { let reporters; - let exitCode = 0; + let exitCode = 0; if (plugins.util.env['code-coverage-reporter']) { reporters = [...options.codeCoverage.reporters, plugins.util.env['code-coverage-reporter']]; diff --git a/package.json b/package.json index 92762642..82fb9adc 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "scripts": { "start": "gulp serve", "build": "gulp", - "lint": "gulp lint", + "lint": "eslint *.js server/**/*.js config/**/*.js && echo Lint Passed ❤", "test": "gulp mocha", "commit": "git-cz", "report-coverage": "coveralls < ./coverage/lcov.info" @@ -65,13 +65,13 @@ "coveralls": "^2.11.6", "cz-conventional-changelog": "1.1.5", "del": "^2.2.0", - "eslint": "^1.10.3", - "eslint-config-airbnb": "5.0.1", + "eslint": "3.5.0", + "eslint-config-airbnb-base": "7.1.0", + "eslint-plugin-import": "1.16.0", "ghooks": "^1.2.4", "gulp": "3.9.1", "gulp-babel": "6.1.2", "gulp-env": "^0.4.0", - "gulp-eslint": "^1.1.1", "gulp-istanbul": "1.0.0", "gulp-load-plugins": "^1.2.0", "gulp-mocha": "^2.2.0", diff --git a/server/controllers/user.js b/server/controllers/user.js index d80c39b5..6cbab5e8 100644 --- a/server/controllers/user.js +++ b/server/controllers/user.js @@ -6,10 +6,10 @@ import User from '../models/user'; function load(req, res, next, id) { User.get(id) .then((user) => { - req.user = user; // eslint-disable-line no-param-reassign + req.user = user; // eslint-disable-line no-param-reassign return next(); }) - .catch((e) => next(e)); + .catch(e => next(e)); } /** @@ -33,8 +33,8 @@ function create(req, res, next) { }); user.save() - .then((savedUser) => res.json(savedUser)) - .catch((e) => next(e)); + .then(savedUser => res.json(savedUser)) + .catch(e => next(e)); } /** @@ -49,8 +49,8 @@ function update(req, res, next) { user.mobileNumber = req.body.mobileNumber; user.save() - .then((savedUser) => res.json(savedUser)) - .catch((e) => next(e)); + .then(savedUser => res.json(savedUser)) + .catch(e => next(e)); } /** @@ -62,8 +62,8 @@ function update(req, res, next) { function list(req, res, next) { const { limit = 50, skip = 0 } = req.query; User.list({ limit, skip }) - .then((users) => res.json(users)) - .catch((e) => next(e)); + .then(users => res.json(users)) + .catch(e => next(e)); } /** @@ -73,8 +73,8 @@ function list(req, res, next) { function remove(req, res, next) { const user = req.user; user.remove() - .then((deletedUser) => res.json(deletedUser)) - .catch((e) => next(e)); + .then(deletedUser => res.json(deletedUser)) + .catch(e => next(e)); } export default { load, get, create, update, list, remove }; diff --git a/server/helpers/APIError.js b/server/helpers/APIError.js index f6c39a75..13bb3a6d 100644 --- a/server/helpers/APIError.js +++ b/server/helpers/APIError.js @@ -10,7 +10,7 @@ class ExtendableError extends Error { this.message = message; this.status = status; this.isPublic = isPublic; - this.isOperational = true; // This is required since bluebird 4 doesn't append it anymore. + this.isOperational = true; // This is required since bluebird 4 doesn't append it anymore. Error.captureStackTrace(this, this.constructor.name); } } diff --git a/server/routes/auth.js b/server/routes/auth.js index 8ecb596b..dbc10a39 100644 --- a/server/routes/auth.js +++ b/server/routes/auth.js @@ -5,7 +5,7 @@ import paramValidation from '../../config/param-validation'; import authCtrl from '../controllers/auth'; import config from '../../config/env'; -const router = express.Router(); // eslint-disable-line new-cap +const router = express.Router(); // eslint-disable-line new-cap /** POST /api/auth/login - Returns token if correct username and password is provided */ router.route('/login') diff --git a/server/routes/index.js b/server/routes/index.js index 58c9435c..9e1df6e3 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -2,7 +2,7 @@ import express from 'express'; import userRoutes from './user'; import authRoutes from './auth'; -const router = express.Router(); // eslint-disable-line new-cap +const router = express.Router(); // eslint-disable-line new-cap /** GET /health-check - Check service health */ router.get('/health-check', (req, res) => diff --git a/server/routes/user.js b/server/routes/user.js index c987ee2b..68b778ee 100644 --- a/server/routes/user.js +++ b/server/routes/user.js @@ -3,7 +3,7 @@ import validate from 'express-validation'; import paramValidation from '../../config/param-validation'; import userCtrl from '../controllers/user'; -const router = express.Router(); // eslint-disable-line new-cap +const router = express.Router(); // eslint-disable-line new-cap router.route('/') /** GET /api/users - Get list of users */ diff --git a/server/tests/misc.test.js b/server/tests/misc.test.js index c8538988..2d02ef51 100644 --- a/server/tests/misc.test.js +++ b/server/tests/misc.test.js @@ -1,7 +1,6 @@ import request from 'supertest-as-promised'; import httpStatus from 'http-status'; -import chai from 'chai'; -import { expect } from 'chai'; +import chai, { expect } from 'chai'; import app from '../../index'; chai.config.includeStack = true; @@ -12,10 +11,11 @@ describe('## Misc', () => { request(app) .get('/api/health-check') .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.text).to.equal('OK'); done(); - }); + }) + .catch(done); }); }); @@ -24,10 +24,11 @@ describe('## Misc', () => { request(app) .get('/api/404') .expect(httpStatus.NOT_FOUND) - .then(res => { + .then((res) => { expect(res.body.message).to.equal('Not Found'); done(); - }); + }) + .catch(done); }); }); @@ -36,10 +37,11 @@ describe('## Misc', () => { request(app) .get('/api/users/56z787zzz67fc') .expect(httpStatus.INTERNAL_SERVER_ERROR) - .then(res => { + .then((res) => { expect(res.body.message).to.equal('Internal Server Error'); done(); - }); + }) + .catch(done); }); it('should handle express validation error - username is required', (done) => { @@ -49,10 +51,11 @@ describe('## Misc', () => { mobileNumber: '1234567890' }) .expect(httpStatus.BAD_REQUEST) - .then(res => { - expect(res.body.message).to.equal(`"username" is required`); + .then((res) => { + expect(res.body.message).to.equal('"username" is required'); done(); - }); + }) + .catch(done); }); }); }); diff --git a/server/tests/user.test.js b/server/tests/user.test.js index e3e6b15b..2347bb0f 100644 --- a/server/tests/user.test.js +++ b/server/tests/user.test.js @@ -1,7 +1,6 @@ import request from 'supertest-as-promised'; import httpStatus from 'http-status'; -import chai from 'chai'; -import { expect } from 'chai'; +import chai, { expect } from 'chai'; import app from '../../index'; chai.config.includeStack = true; @@ -18,12 +17,13 @@ describe('## User APIs', () => { .post('/api/users') .send(user) .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.body.username).to.equal(user.username); expect(res.body.mobileNumber).to.equal(user.mobileNumber); user = res.body; done(); - }); + }) + .catch(done); }); }); @@ -32,21 +32,23 @@ describe('## User APIs', () => { request(app) .get(`/api/users/${user._id}`) .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.body.username).to.equal(user.username); expect(res.body.mobileNumber).to.equal(user.mobileNumber); done(); - }); + }) + .catch(done); }); it('should report error with message - Not found, when user does not exists', (done) => { request(app) .get('/api/users/56c787ccc67fc16ccc1a5e92') .expect(httpStatus.NOT_FOUND) - .then(res => { + .then((res) => { expect(res.body.message).to.equal('Not Found'); done(); - }); + }) + .catch(done); }); }); @@ -57,11 +59,12 @@ describe('## User APIs', () => { .put(`/api/users/${user._id}`) .send(user) .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.body.username).to.equal('KK'); expect(res.body.mobileNumber).to.equal(user.mobileNumber); done(); - }); + }) + .catch(done); }); }); @@ -70,10 +73,11 @@ describe('## User APIs', () => { request(app) .get('/api/users') .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.body).to.be.an('array'); done(); - }); + }) + .catch(done); }); }); @@ -82,11 +86,12 @@ describe('## User APIs', () => { request(app) .delete(`/api/users/${user._id}`) .expect(httpStatus.OK) - .then(res => { + .then((res) => { expect(res.body.username).to.equal('KK'); expect(res.body.mobileNumber).to.equal(user.mobileNumber); done(); - }); + }) + .catch(done); }); }); });