UNPKG

3.88 kBMarkdownView Raw
1# gulp-mocha-tdd
2easy test driven development with gulp and mocha
3
4* it('should use mocha for unit tests')
5* it('should auto-generate the boilerplate module test describe statement')
6* it('should be able to work with your test naming conventions')
7* it('should make test driven development easy and convienant')
8* it('should support mocha options like breakpoints and grep')
9* it('should use conventions to associate modules with unit tests')
10* it('should support additional gulp stream handlers (like JSX)')
11* it('should re-run module tests when either the module or unit test code changes')
12
13
14Usage
15-----------
16
17To execute all tests
18```
19> gulp test
20```
21And watch for module or unit test changes
22```
23> gulp test -w
24```
25Stop on any debugger statements
26```
27> gulp test -d
28> node-inspector {in another window}
29browse to http://127.0.0.1:8080/debug?port=5858
30```
31Any other mocha params can be used as well: see ```mocha -h```
32
33
34Test Modules
35---------------
36Test modules do not need the top level ```describe``` function (it will be created automatically based on file structure). You can either just have your tests directly in the file (no usage of ```module.exports```) or you can export a function callback that contains your tests. This callback accepts 2 parameters ```(targetModule, targetModuleDirectoryPath)```. For example:
37
38```
39var targetModule = require('path/to/target/module');
40it('should ...', function() {
41 expect(targetModule...).to...
42});
43```
44or
45```
46module.exports = function(targetModule, targetBase) {
47 it('should ...', function() {
48 expect(targetModule...).to...
49 });
50 it('should ...', function() {
51 expect(require(targetBase + '/targetModuleName')...).to...
52 });
53}
54```
55
56
57All modules must be within a root directory ("js" by default) and tests can either be in a separate root directory ("tests" by default) or tests can be in a directory relative ("_tests" by default) to the module. Tests can use any naming pattern ("{module name}-test.js" by default).
58
59For example
60```
61|-- js
62 |-- _tests
63 |-- foo-test.js
64 |-- foo.js
65```
66or
67```
68|-- lib
69 |-- foo.js
70|-- tests
71 |-- foo.spec.js
72```
73
74See Options below for available configuration
75
76[see examples for details](https://github.com/jhudson8/gulp-mocha-tdd/tree/master/examples)
77
78Any files within the test directories prefixed with ```_``` will be ignored for testing allowing for utility modules.
79
80
81Additional Transformations
82------------
83The ```pipe``` option (array of gulp stream handlers) can be used to perform additional transformations. The
84following gulpfile can be used to support React JSX
85```
86var gulp = require('gulp');
87var react = require('gulp-react');
88var gulpMochaTDD = require('gulp-mocha-tdd');
89
90gulpMochaTDD(gulp, {
91 pipe: [react({ harmony: true })]
92});
93```
94
95Options
96------------
97* ***taskName***: the gulp task name ("test" if undefined)
98* ***pipe***: array of gulp stream handlers (See above for example)
99* ***init***: init function executed once before the tests
100* ***scriptsDirName***: top level directory ("js" if undefined)
101* ***testFilePattern***: unit test file name (without ext) pattern (use "{name}" to reference the module name; "{name}-test" if undefined)
102* ***testsDirName***: name of directory which contains the unit test files ("_tests" if undefined)
103* ***rootTestsDir***: true if using a root tests directory and undefined/false if tests are in a directory relative to the module ([see examples](https://github.com/jhudson8/gulp-mocha-tdd/tree/master/examples))
104
105
106Installation
107------------
108Install dependencies
109```
110npm install --save-dev mocha
111npm install --save-dev gulp
112npm install --save-dev gulp-mocha-tdd
113```
114
115Inject the ```test``` task in ```gulpfile.js```
116```
117var gulp = require('gulp');
118var gulpMochaTDD = require('gulp-mocha-tdd', {
119 // add options here
120});
121
122gulpMochaTDD(gulp);
123```
124
125Add ```.test``` to your ```.gitignore``` file
126
\No newline at end of file