Skip to content

Commit 277a1ad

Browse files
committed
feat: do not preprocess files if coverage reporter is not used
Closes #7
1 parent bc20465 commit 277a1ad

File tree

3 files changed

+61
-21
lines changed

3 files changed

+61
-21
lines changed

lib/preprocessor.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
var istanbul = require('istanbul');
22

3-
var createCoveragePreprocessor = function(logger, basePath) {
3+
var createCoveragePreprocessor = function(logger, basePath, reporters) {
44
var log = logger.create('preprocessor.coverage');
55
var instrumenter = new istanbul.Instrumenter();
66

7+
// if coverage reporter is not used, do not preprocess the files
8+
if (reporters.indexOf('coverage') === -1) {
9+
return function(content, _, done) {
10+
done(content);
11+
};
12+
}
13+
714
return function(content, file, done) {
815
log.debug('Processing "%s".', file.originalPath);
916

@@ -17,6 +24,6 @@ var createCoveragePreprocessor = function(logger, basePath) {
1724
};
1825
};
1926

20-
createCoveragePreprocessor.$inject = ['logger', 'config.basePath'];
27+
createCoveragePreprocessor.$inject = ['logger', 'config.basePath', 'config.reporters'];
2128

2229
module.exports = createCoveragePreprocessor;

test/preprocessor.spec.coffee

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
vm = require 'vm'
2+
util = require 'util'
3+
4+
describe 'preprocessor', ->
5+
createPreprocessor = require '../lib/preprocessor'
6+
7+
ORIGINAL_CODE = '''
8+
if (a) {
9+
something();
10+
} else {
11+
other();
12+
}
13+
'''
14+
15+
16+
mockLogger = create: ->
17+
error: -> throw new Error(util.format.apply util, arguments)
18+
warn: -> null
19+
info: -> null
20+
debug: -> null
21+
22+
# TODO(vojta): refactor this somehow ;-) it's copy pasted from lib/file-list.js
23+
File = (path, mtime) ->
24+
@path = path
25+
@originalPath = path
26+
@contentPath = path
27+
@mtime = mtime
28+
@isUrl = false
29+
30+
31+
it 'should not do anything if coverage reporter is not used', (done) ->
32+
process = createPreprocessor mockLogger, null, ['dots', 'progress']
33+
file = new File '/base/path/file.js'
34+
35+
process ORIGINAL_CODE, file, (preprocessedCode) ->
36+
expect(preprocessedCode).to.equal ORIGINAL_CODE
37+
expect(file.path).to.equal '/base/path/file.js'
38+
done()
39+
40+
41+
it 'should preprocess the code', (done) ->
42+
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress']
43+
file = new File '/base/path/file.js'
44+
45+
process ORIGINAL_CODE, file, (preprocessedCode) ->
46+
sandbox =
47+
a: true
48+
something: ->
49+
50+
vm.runInNewContext preprocessedCode, sandbox
51+
expect(sandbox.__coverage__).to.have.ownProperty './file.js'
52+
done()

test/reporter.spec.coffee

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,3 @@
1-
# TODO(vojta): extract this into a separate file
2-
sinon = require 'sinon'
3-
chai = require 'chai'
4-
5-
# publish globals that all specs can use
6-
global.expect = chai.expect
7-
global.should = chai.should()
8-
global.sinon = sinon
9-
10-
# chai plugins
11-
chai.use(require 'sinon-chai')
12-
13-
beforeEach ->
14-
global.sinon = sinon.sandbox.create()
15-
16-
afterEach ->
17-
global.sinon.restore()
18-
19-
201
#==============================================================================
212
# lib/reporters/Coverage.js module
223
#==============================================================================

0 commit comments

Comments
 (0)