1 | |
---|
2 | /** |
---|
3 | * Module dependencies. |
---|
4 | */ |
---|
5 | |
---|
6 | var Suite = require('../suite') |
---|
7 | , Test = require('../test') |
---|
8 | , utils = require('../utils'); |
---|
9 | |
---|
10 | /** |
---|
11 | * BDD-style interface: |
---|
12 | * |
---|
13 | * describe('Array', function(){ |
---|
14 | * describe('#indexOf()', function(){ |
---|
15 | * it('should return -1 when not present', function(){ |
---|
16 | * |
---|
17 | * }); |
---|
18 | * |
---|
19 | * it('should return the index when present', function(){ |
---|
20 | * |
---|
21 | * }); |
---|
22 | * }); |
---|
23 | * }); |
---|
24 | * |
---|
25 | */ |
---|
26 | |
---|
27 | module.exports = function(suite){ |
---|
28 | var suites = [suite]; |
---|
29 | |
---|
30 | suite.on('pre-require', function(context, file, mocha){ |
---|
31 | |
---|
32 | /** |
---|
33 | * Execute before running tests. |
---|
34 | */ |
---|
35 | |
---|
36 | context.before = function(fn){ |
---|
37 | suites[0].beforeAll(fn); |
---|
38 | }; |
---|
39 | |
---|
40 | /** |
---|
41 | * Execute after running tests. |
---|
42 | */ |
---|
43 | |
---|
44 | context.after = function(fn){ |
---|
45 | suites[0].afterAll(fn); |
---|
46 | }; |
---|
47 | |
---|
48 | /** |
---|
49 | * Execute before each test case. |
---|
50 | */ |
---|
51 | |
---|
52 | context.beforeEach = function(fn){ |
---|
53 | suites[0].beforeEach(fn); |
---|
54 | }; |
---|
55 | |
---|
56 | /** |
---|
57 | * Execute after each test case. |
---|
58 | */ |
---|
59 | |
---|
60 | context.afterEach = function(fn){ |
---|
61 | suites[0].afterEach(fn); |
---|
62 | }; |
---|
63 | |
---|
64 | /** |
---|
65 | * Describe a "suite" with the given `title` |
---|
66 | * and callback `fn` containing nested suites |
---|
67 | * and/or tests. |
---|
68 | */ |
---|
69 | |
---|
70 | context.describe = context.context = function(title, fn){ |
---|
71 | var suite = Suite.create(suites[0], title); |
---|
72 | suites.unshift(suite); |
---|
73 | fn.call(suite); |
---|
74 | suites.shift(); |
---|
75 | return suite; |
---|
76 | }; |
---|
77 | |
---|
78 | /** |
---|
79 | * Pending describe. |
---|
80 | */ |
---|
81 | |
---|
82 | context.xdescribe = |
---|
83 | context.xcontext = |
---|
84 | context.describe.skip = function(title, fn){ |
---|
85 | var suite = Suite.create(suites[0], title); |
---|
86 | suite.pending = true; |
---|
87 | suites.unshift(suite); |
---|
88 | fn.call(suite); |
---|
89 | suites.shift(); |
---|
90 | }; |
---|
91 | |
---|
92 | /** |
---|
93 | * Exclusive suite. |
---|
94 | */ |
---|
95 | |
---|
96 | context.describe.only = function(title, fn){ |
---|
97 | var suite = context.describe(title, fn); |
---|
98 | mocha.grep(suite.fullTitle()); |
---|
99 | return suite; |
---|
100 | }; |
---|
101 | |
---|
102 | /** |
---|
103 | * Describe a specification or test-case |
---|
104 | * with the given `title` and callback `fn` |
---|
105 | * acting as a thunk. |
---|
106 | */ |
---|
107 | |
---|
108 | context.it = context.specify = function(title, fn){ |
---|
109 | var suite = suites[0]; |
---|
110 | if (suite.pending) var fn = null; |
---|
111 | var test = new Test(title, fn); |
---|
112 | suite.addTest(test); |
---|
113 | return test; |
---|
114 | }; |
---|
115 | |
---|
116 | /** |
---|
117 | * Exclusive test-case. |
---|
118 | */ |
---|
119 | |
---|
120 | context.it.only = function(title, fn){ |
---|
121 | var test = context.it(title, fn); |
---|
122 | var reString = '^' + utils.escapeRegexp(test.fullTitle()) + '$'; |
---|
123 | mocha.grep(new RegExp(reString)); |
---|
124 | return test; |
---|
125 | }; |
---|
126 | |
---|
127 | /** |
---|
128 | * Pending test case. |
---|
129 | */ |
---|
130 | |
---|
131 | context.xit = |
---|
132 | context.xspecify = |
---|
133 | context.it.skip = function(title){ |
---|
134 | context.it(title); |
---|
135 | }; |
---|
136 | }); |
---|
137 | }; |
---|