-
Notifications
You must be signed in to change notification settings - Fork 10
/
mocha.js
215 lines (188 loc) · 5.42 KB
/
mocha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
var mocha = require('../lib/mocha'),
path = require('path'),
fs = require('fs'),
chai = require('chai'),
should = chai.should();
describe('Unit Tests', function () {
it('should pass a sanity chack', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true
}, function (error, output) {
should.not.exist(error);
output.should.include('1 passing');
done();
});
});
it('should test when mocha passes', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true
}, function (error) {
should.not.exist(error);
done();
});
});
it('should test when mocha fails', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/fail.js')],
quiet: true
}, function (error) {
should.exist(error);
done();
});
});
it('should test setting string options', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true,
reporter: 'json'
}, function (error, output) {
should.not.exist(error);
(function () {
JSON.parse(output);
}).should.not.throw();
done();
});
});
it('should test setting boolean options', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/fail.js')],
quiet: true,
reporter: 'json',
bail: true
}, function (error, output) {
should.exist(error);
output = JSON.parse(output);
output.stats.tests.should.equal(1);
done();
});
});
it('should test setting array options', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/coffeescript.coffee')],
quiet: true,
reporter: 'json',
compilers: ['coffee:coffee-script/register']
}, function (error) {
should.not.exist(error);
done();
});
});
it('should test requiring modules', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/require.js')],
quiet: true,
reporter: 'json',
require: ['should']
}, function (error, output) {
should.not.exist(error);
output = JSON.parse(output);
output.stats.passes.should.equal(1);
done();
});
});
it('should test file globbing', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass*.js')],
quiet: true,
reporter: 'json'
}, function (error, output) {
should.not.exist(error);
output = JSON.parse(output);
output.stats.suites.should.equal(2);
done();
});
});
it('should test coverage', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true,
reporter: 'json-cov',
coverage: true
}, function (error, output) {
should.not.exist(error);
output.should.include('"coverage": 100');
done();
});
});
it('should test coverage output', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true,
reporter: 'json-cov',
output: 'test/out.json'
}, function (error) {
should.not.exist(error);
var filename = path.resolve('test/out.json');
var jsonOutput = JSON.parse(fs.readFileSync(filename));
fs.unlinkSync(filename);
jsonOutput.coverage.should.equals(100);
done();
});
});
it('should test coverage output when directory does not exist', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
quiet: true,
reporter: 'json-cov',
output: 'test/new/out.json'
}, function (error) {
should.not.exist(error);
var filename = path.resolve('test/new/out.json'),
dir = path.dirname(filename);
var jsonOutput = JSON.parse(fs.readFileSync(filename));
fs.unlinkSync(filename);
fs.rmdirSync(dir);
jsonOutput.coverage.should.equals(100);
done();
});
});
it('should test coveralls integration', function (done) {
mocha({
files: [path.join(__dirname, '/fixture/pass.js')],
coveralls: {
repoToken: 'bad-token',
serviceName: 'bad-name',
serviceJobId: 'bad-job-id'
}
}, function (error) {
// expect a bad response as we intentionaly give coveralls a bad token
should.exist(error);
done();
});
});
it('should accept a boolean for coveralls option', function () {
var opts = mocha.getBaseOptions({
coveralls: true
});
opts.coveralls.should.be.a('object');
});
it('should accept a string for require and make it an array', function () {
var opts = mocha.getBaseOptions({
require: 'module'
});
opts.require.should.eql([
'module'
]);
});
// since travis provides the Token, this is tough to test...
//
// it('should require that either a supported ci is in use, a repoToken is specified, or a serviceJobId and serviceName are specified', function (done) {
// var origTravisVal = process.env.TRAVIS;
// delete process.env.TRAVIS;
// mocha({
// files: [path.join(__dirname, '/fixture/pass.js')],
// coveralls: {
// repoToken: null,
// serviceName: null,
// serviceJobId: null
// }
// }, function (err) {
// process.env.TRAVIS = origTravisVal;
// err.message.should.match(/(.*(repoToken|serviceName|serviceJobId).*){3}/);
// done();
// });
// });
});