Skip to content

Commit

Permalink
Add npm test, test:watch, test:check-coverage npm tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalkapadia committed Oct 16, 2016
1 parent 0b46aae commit dc9094d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
25 changes: 25 additions & 0 deletions .istanbul.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
verbose: false
instrumentation:
excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js']
include-all-sources: true
reporting:
print: summary
reports:
- lcov
dir: ./coverage
watermarks:
statements: [50, 80]
lines: [50, 80]
functions: [50, 80]
branches: [50, 80]
check:
global:
statements: 50
lines: 50
branches: 50
functions: 50
each:
statements: 50
lines: 50
branches: 50
functions: 20
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Heavily inspired from [Egghead.io - How to Write an Open Source JavaScript Libra
| Authentication via JsonWebToken | Supports authentication using [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken). |
| Code Linting | JavaScript code linting is done using [ESLint](http://eslint.org) - a pluggable linter tool for identifying and reporting on patterns in JavaScript. Uses ESLint with [eslint-config-airbnb](https://github.com/airbnb/javascript/tree/master/packages/eslint-config-airbnb), which tries to follow the Airbnb JavaScript style guide. |
| Auto server restart | Restart the server using [nodemon](https://github.com/remy/nodemon) in real-time anytime an edit is made, with babel compilation and eslint. |
| ES6 Code Coverage via [istanbul](https://www.npmjs.com/package/istanbul) | Supports code coverage of ES6 code using istanbul and mocha. Code coverage reports are saved in `coverage/` directory post `npm test` execution. Open `lcov-report/index.html` to view coverage report. `npm test` also displays code coverage summary on console. |
| ES6 Code Coverage via [istanbul](https://www.npmjs.com/package/istanbul) | Supports code coverage of ES6 code using istanbul and mocha. Code coverage reports are saved in `coverage/` directory post `npm test` execution. Open `coverage/lcov-report/index.html` to view coverage report. `npm test` also displays code coverage summary on console. Code coverage can also be enforced overall and per file as well, configured via .istanbul.yml |
| Debugging via [debug](https://www.npmjs.com/package/debug) | Instead of inserting and deleting console.log you can replace it with the debug function and just leave it there. You can then selectively debug portions of your code by setting DEBUG env variable. If DEBUG env variable is not set, nothing is displayed to the console. |
| Promisified Code via [bluebird](https://github.com/petkaantonov/bluebird) | We love promise, don't we ? All our code is promisified and even so our tests via [supertest-as-promised](https://www.npmjs.com/package/supertest-as-promised). |
| API parameter validation via [express-validation](https://www.npmjs.com/package/express-validation) | Validate body, params, query, headers and cookies of a request (via middleware) and return a response with errors; if any of the configured validation rules fail. You won't anymore need to make your route handler dirty with such validations. |
Expand Down Expand Up @@ -60,11 +60,14 @@ gulp serve

Execute tests:
```sh
# compile with babel and run tests
npm test (or gulp mocha)
# Run tests written in ES6 along with code coverage
npm test

# use --code-coverage-reporter text to get code coverage for each file
gulp mocha --code-coverage-reporter text
# Run tests on file change
npm run test:watch

# Same as above but enforcing code coverage (configured via .istanbul.yml)
npm run test:check-coverage
```

Other gulp tasks:
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ if (config.MONGOOSE_DEBUG) {
});
}

// listen on port config.port
app.listen(config.port, () => {
debug(`server started on port ${config.port} (${config.env})`);
});
// module.parent check is required to support mocha watch
// src: https://github.com/mochajs/mocha/issues/1912
if (!module.parent) {
// listen on port config.port
app.listen(config.port, () => {
debug(`server started on port ${config.port} (${config.env})`);
});
}

export default app;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"build": "gulp",
"lint": "esw *.js server/**/*.js config/**/*.js --color",
"lint:watch": "npm run lint -- --watch",
"test": "gulp mocha",
"test": "NODE_ENV=test babel-node node_modules/.bin/isparta cover _mocha -- --ui bdd --reporter spec --colors --compilers js:babel-core/register ./server/**/*.test.js",
"test:watch": "npm run test -- --watch",
"test:check-coverage": "npm run test && istanbul check-coverage",
"commit": "git-cz",
"report-coverage": "coveralls < ./coverage/lcov.info"
},
Expand Down
12 changes: 12 additions & 0 deletions server/tests/user.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import mongoose from 'mongoose';
import request from 'supertest-as-promised';
import httpStatus from 'http-status';
import chai, { expect } from 'chai';
import app from '../../index';

chai.config.includeStack = true;

/**
* root level hooks
*/
after((done) => {
// required because https://github.com/Automattic/mongoose/issues/1251#issuecomment-65793092
mongoose.models = {};
mongoose.modelSchemas = {};
mongoose.connection.close();
done();
});

describe('## User APIs', () => {
let user = {
username: 'KK123',
Expand Down

0 comments on commit dc9094d

Please sign in to comment.