Skip to content

Commit a231fe0

Browse files
authored
Merge pull request #94 from KunalKapadia/develop
Add npm test, test:watch, test:check-coverage npm tasks
2 parents a00c28b + e400dc4 commit a231fe0

File tree

6 files changed

+56
-94
lines changed

6 files changed

+56
-94
lines changed

.istanbul.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
verbose: false
2+
instrumentation:
3+
excludes: ['dist/**', 'coverage/**', 'gulpfile.babel.js']
4+
include-all-sources: true
5+
reporting:
6+
print: summary
7+
reports:
8+
- lcov
9+
dir: ./coverage
10+
watermarks:
11+
statements: [50, 80]
12+
lines: [50, 80]
13+
functions: [50, 80]
14+
branches: [50, 80]
15+
check:
16+
global:
17+
statements: 50
18+
lines: 50
19+
branches: 50
20+
functions: 50
21+
each:
22+
statements: 50
23+
lines: 50
24+
branches: 50
25+
functions: 20

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Heavily inspired from [Egghead.io - How to Write an Open Source JavaScript Libra
2525
| Authentication via JsonWebToken | Supports authentication using [jsonwebtoken](https://www.npmjs.com/package/jsonwebtoken). |
2626
| 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. |
2727
| 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. |
28-
| 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. |
28+
| 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 |
2929
| 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. |
3030
| 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). |
3131
| 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. |
@@ -60,11 +60,14 @@ gulp serve
6060

6161
Execute tests:
6262
```sh
63-
# compile with babel and run tests
64-
npm test (or gulp mocha)
63+
# Run tests written in ES6 along with code coverage
64+
npm test
6565

66-
# use --code-coverage-reporter text to get code coverage for each file
67-
gulp mocha --code-coverage-reporter text
66+
# Run tests on file change
67+
npm run test:watch
68+
69+
# Run tests enforcing code coverage (configured via .istanbul.yml)
70+
npm run test:check-coverage
6871
```
6972

7073
Other gulp tasks:

gulpfile.babel.js

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import gulpLoadPlugins from 'gulp-load-plugins';
33
import path from 'path';
44
import del from 'del';
55
import runSequence from 'run-sequence';
6-
import babelCompiler from 'babel-core/register';
7-
import * as isparta from 'isparta';
86

97
const plugins = gulpLoadPlugins();
108

@@ -14,29 +12,11 @@ const paths = {
1412
tests: './server/tests/*.js'
1513
};
1614

17-
const options = {
18-
codeCoverage: {
19-
reporters: ['lcov', 'text-summary'],
20-
thresholds: {
21-
global: { statements: 80, branches: 80, functions: 80, lines: 80 }
22-
}
23-
}
24-
};
25-
2615
// Clean up dist and coverage directory
2716
gulp.task('clean', () =>
2817
del(['dist/**', 'coverage/**', '!dist', '!coverage'])
2918
);
3019

31-
// Set env variables
32-
gulp.task('set-env', () => {
33-
plugins.env({
34-
vars: {
35-
NODE_ENV: 'test'
36-
}
37-
});
38-
});
39-
4020
// Copy non-js files to dist
4121
gulp.task('copy', () =>
4222
gulp.src(paths.nonJs)
@@ -69,66 +49,6 @@ gulp.task('nodemon', ['copy', 'babel'], () =>
6949
})
7050
);
7151

72-
// covers files for code coverage
73-
gulp.task('pre-test', () =>
74-
gulp.src([...paths.js, '!gulpfile.babel.js'])
75-
// Covering files
76-
.pipe(plugins.istanbul({
77-
instrumenter: isparta.Instrumenter,
78-
includeUntested: true
79-
}))
80-
// Force `require` to return covered files
81-
.pipe(plugins.istanbul.hookRequire())
82-
);
83-
84-
// triggers mocha test with code coverage
85-
gulp.task('test', ['pre-test', 'set-env'], () => {
86-
let reporters;
87-
let exitCode = 0;
88-
89-
if (plugins.util.env['code-coverage-reporter']) {
90-
reporters = [...options.codeCoverage.reporters, plugins.util.env['code-coverage-reporter']];
91-
} else {
92-
reporters = options.codeCoverage.reporters;
93-
}
94-
95-
return gulp.src([paths.tests], { read: false })
96-
.pipe(plugins.plumber())
97-
.pipe(plugins.mocha({
98-
reporter: plugins.util.env['mocha-reporter'] || 'spec',
99-
ui: 'bdd',
100-
timeout: 6000,
101-
compilers: {
102-
js: babelCompiler
103-
}
104-
}))
105-
.once('error', (err) => {
106-
plugins.util.log(err);
107-
exitCode = 1;
108-
})
109-
// Creating the reports after execution of test cases
110-
.pipe(plugins.istanbul.writeReports({
111-
dir: './coverage',
112-
reporters
113-
}))
114-
// Enforce test coverage
115-
.pipe(plugins.istanbul.enforceThresholds({
116-
thresholds: options.codeCoverage.thresholds
117-
}))
118-
.once('end', () => {
119-
plugins.util.log('completed !!');
120-
process.exit(exitCode);
121-
});
122-
});
123-
124-
// clean dist, compile js files, copy non-js files and execute tests
125-
gulp.task('mocha', ['clean'], () => {
126-
runSequence(
127-
['copy', 'babel'],
128-
'test'
129-
);
130-
});
131-
13252
// gulp serve for development
13353
gulp.task('serve', ['clean'], () => runSequence('nodemon'));
13454

index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ if (config.MONGOOSE_DEBUG) {
2222
});
2323
}
2424

25-
// listen on port config.port
26-
app.listen(config.port, () => {
27-
debug(`server started on port ${config.port} (${config.env})`);
28-
});
25+
// module.parent check is required to support mocha watch
26+
// src: https://github.com/mochajs/mocha/issues/1912
27+
if (!module.parent) {
28+
// listen on port config.port
29+
app.listen(config.port, () => {
30+
debug(`server started on port ${config.port} (${config.env})`);
31+
});
32+
}
2933

3034
export default app;

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"build": "gulp",
1515
"lint": "esw *.js server/**/*.js config/**/*.js --color",
1616
"lint:watch": "npm run lint -- --watch",
17-
"test": "gulp mocha",
17+
"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",
18+
"test:watch": "npm run test -- --watch",
19+
"test:check-coverage": "npm run test && istanbul check-coverage",
1820
"commit": "git-cz",
1921
"report-coverage": "coveralls < ./coverage/lcov.info"
2022
},
@@ -73,13 +75,9 @@
7375
"ghooks": "^1.2.4",
7476
"gulp": "3.9.1",
7577
"gulp-babel": "6.1.2",
76-
"gulp-env": "^0.4.0",
77-
"gulp-istanbul": "1.1.1",
7878
"gulp-load-plugins": "^1.2.0",
79-
"gulp-mocha": "3.0.1",
8079
"gulp-newer": "^1.1.0",
8180
"gulp-nodemon": "^2.0.6",
82-
"gulp-plumber": "^1.0.1",
8381
"gulp-sourcemaps": "^1.6.0",
8482
"gulp-util": "^3.0.7",
8583
"isparta": "4.0.0",

server/tests/user.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
import mongoose from 'mongoose';
12
import request from 'supertest-as-promised';
23
import httpStatus from 'http-status';
34
import chai, { expect } from 'chai';
45
import app from '../../index';
56

67
chai.config.includeStack = true;
78

9+
/**
10+
* root level hooks
11+
*/
12+
after((done) => {
13+
// required because https://github.com/Automattic/mongoose/issues/1251#issuecomment-65793092
14+
mongoose.models = {};
15+
mongoose.modelSchemas = {};
16+
mongoose.connection.close();
17+
done();
18+
});
19+
820
describe('## User APIs', () => {
921
let user = {
1022
username: 'KK123',

0 commit comments

Comments
 (0)