-
-
Notifications
You must be signed in to change notification settings - Fork 350
/
nyc-test.js
274 lines (232 loc) · 7.18 KB
/
nyc-test.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* global describe, it */
var _ = require('lodash')
var fs = require('fs')
var NYC = require('../')
var path = require('path')
var rimraf = require('rimraf')
var sinon = require('sinon')
var spawn = require('child_process').spawn
require('chai').should()
require('tap').mochaGlobals()
describe('nyc', function () {
var fixtures = path.resolve(__dirname, './fixtures')
describe('cwd', function () {
function afterEach () {
delete process.env.NYC_CWD
rimraf.sync(path.resolve(fixtures, './nyc_output'))
}
it('sets cwd to process.cwd() if no environment variable is set', function () {
var nyc = new NYC()
nyc.cwd.should.eql(process.cwd())
afterEach()
})
it('uses NYC_CWD environment variable for cwd if it is set', function () {
process.env.NYC_CWD = path.resolve(__dirname, './fixtures')
var nyc = new NYC()
nyc.cwd.should.equal(path.resolve(__dirname, './fixtures'))
afterEach()
})
})
describe('config', function () {
it("loads 'exclude' patterns from package.json", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, './fixtures')
})
nyc.exclude.length.should.eql(3)
})
})
describe('wrap', function () {
var nyc
it('wraps modules with coverage counters when they are required', function () {
nyc = (new NYC({
cwd: process.cwd()
})).wrap()
// clear the module cache so that
// we pull index.js in again and wrap it.
var name = require.resolve('../')
delete require.cache[name]
// when we require index.js it should be wrapped.
var index = require('../')
index.should.match(/__cov_/)
})
function testSignal (signal, done) {
var proc = spawn(process.execPath, ['./test/fixtures/' + signal + '.js'], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
proc.on('close', function () {
var reports = _.filter(nyc._loadReports(), function (report) {
return report['./test/fixtures/' + signal + '.js']
})
reports.length.should.equal(1)
return done()
})
}
it('writes coverage report when process is killed with SIGTERM', function (done) {
testSignal('sigterm', done)
})
it('writes coverage report when process is killed with SIGINT', function (done) {
testSignal('sigint', done)
})
})
describe('report', function () {
it('runs reports for all JSON in output directory', function (done) {
var nyc = new NYC({
cwd: process.cwd()
})
var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
var start = fs.readdirSync(nyc.tmpDirectory()).length
proc.on('close', function () {
nyc.report(
null,
{
add: function (report) {
// the subprocess we ran should output reports
// for files in the fixtures directory.
Object.keys(report).should.match(/.\/test\/fixtures\//)
}
},
{
add: function (reporter) {
// reporter defaults to 'text'/
reporter.should.equal('text')
},
write: function () {
// we should have output a report for the new subprocess.
var stop = fs.readdirSync(nyc.tmpDirectory()).length
stop.should.be.gt(start)
return done()
}
}
)
})
})
it('handles corrupt JSON files', function (done) {
var nyc = new NYC({
cwd: process.cwd()
})
var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
fs.writeFileSync('./.nyc_output/bad.json', '}', 'utf-8')
proc.on('close', function () {
nyc.report(
null,
{
add: function (report) {}
},
{
add: function (reporter) {},
write: function () {
// we should get here without exception.
fs.unlinkSync('./.nyc_output/bad.json')
return done()
}
}
)
})
})
it('handles multiple reporters', function (done) {
var reporters = ['text-summary', 'text-lcov']
var incr = 0
var nyc = new NYC({
cwd: process.cwd(),
reporter: reporters
})
var proc = spawn(process.execPath, ['./test/fixtures/sigint.js'], {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
proc.on('close', function () {
nyc.report(
null,
{
add: function (report) {}
},
{
add: function (reporter) {
incr += !!~reporters.indexOf(reporter)
},
write: function () {
incr.should.eql(reporters.length)
return done()
}
}
)
})
})
})
describe('.istanbul.yml configuration', function () {
var istanbul = require('istanbul')
var configSpy = sinon.spy(istanbul.config, 'loadFile')
var instrumenterSpy = sinon.spy(istanbul, 'Instrumenter')
function writeConfig () {
fs.writeFileSync('./.istanbul.yml', 'instrumentation:\n\tpreserve-comments: true', 'utf-8')
}
function afterEach () {
configSpy.reset()
instrumenterSpy.reset()
rimraf.sync('./.istanbul.yml')
}
it('it handles having no .istanbul.yml in the root directory', function (done) {
afterEach()
var nyc = new NYC()
nyc.wrap()
return done()
})
it('uses the values in .istanbul.yml to instantiate the instrumenter', function (done) {
writeConfig()
var nyc = new NYC({
istanbul: istanbul
})
nyc.wrap()
istanbul.config.loadFile.calledWithMatch('.istanbul.yml').should.equal(true)
istanbul.Instrumenter.calledWith({
coverageVariable: '__coverage__',
embedSource: false,
noCompact: false,
preserveComments: false
}).should.equal(true)
afterEach()
return done()
})
it('loads the .istanbul.yml configuration from NYC_CWD', function (done) {
var nyc = new NYC({
istanbul: istanbul,
cwd: './test/fixtures'
})
nyc.wrap()
istanbul.config.loadFile.calledWithMatch(path.join('test', 'fixtures', '.istanbul.yml')).should.equal(true)
istanbul.Instrumenter.calledWith({
coverageVariable: '__coverage__',
embedSource: false,
noCompact: false,
preserveComments: true
}).should.equal(true)
afterEach()
return done()
})
})
describe('mungeArgs', function () {
it('removes dashed options that proceed bin', function () {
process.argv = ['/Users/benjamincoe/bin/iojs',
'/Users/benjamincoe/bin/nyc.js',
'--reporter',
'lcov',
'node',
'test/nyc-test.js'
]
var yargv = require('yargs').argv
var munged = (new NYC()).mungeArgs(yargv)
munged.should.eql(['node', 'test/nyc-test.js'])
})
})
})