Skip to content

Commit

Permalink
feat: support coverage for coffee script
Browse files Browse the repository at this point in the history
  • Loading branch information
can3p authored and vojtajina committed Nov 29, 2013
1 parent 89c9817 commit 9f802c1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var istanbul = require('istanbul');
var istanbul = require('istanbul'),
ibrik = require('ibrik');

var createCoveragePreprocessor = function(logger, basePath, reporters) {
var log = logger.create('preprocessor.coverage');
var instrumenter = new istanbul.Instrumenter();
var jsInstrumenter = new istanbul.Instrumenter();
var coffeeInstrumenter = new ibrik.Instrumenter();

// if coverage reporter is not used, do not preprocess the files
if (reporters.indexOf('coverage') === -1) {
Expand All @@ -15,10 +17,14 @@ var createCoveragePreprocessor = function(logger, basePath, reporters) {
log.debug('Processing "%s".', file.originalPath);

var jsPath = file.originalPath.replace(basePath + '/', './');
var instrumenter = jsPath.match(/\.coffee$/) ? coffeeInstrumenter : jsInstrumenter;

instrumenter.instrument(content, jsPath, function(err, instrumentedCode) {
if(err) {
log.error('%s\n at %s', err.message, file.originalPath);
}

file.path = file.path.replace(/\.coffee$/, '.js');
done(instrumentedCode);
});
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"author": "SATO taichi <ryushi@gmail.com>",
"dependencies": {
"istanbul": "~0.1.41",
"ibrik": "~0.0.4",
"dateformat": "~1.0.6"
},
"peerDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions test/preprocessor.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ describe 'preprocessor', ->
}
'''

ORIGINAL_COFFEE_CODE = '''
if a
something()
else
other()
'''

mockLogger = create: ->
error: -> throw new Error(util.format.apply util, arguments)
Expand Down Expand Up @@ -50,3 +56,17 @@ describe 'preprocessor', ->
vm.runInNewContext preprocessedCode, sandbox
expect(sandbox.__coverage__).to.have.ownProperty './file.js'
done()

it 'should preprocess the coffee code', (done) ->
process = createPreprocessor mockLogger, '/base/path', ['coverage', 'progress']
file = new File '/base/path/file.coffee'

process ORIGINAL_COFFEE_CODE, file, (preprocessedCode) ->
sandbox =
a: true
something: ->

vm.runInNewContext preprocessedCode, sandbox
expect(file.path).to.equal '/base/path/file.js'
expect(sandbox.__coverage__).to.have.ownProperty './file.coffee'
done()

0 comments on commit 9f802c1

Please sign in to comment.