diff --git a/index.js b/index.js index 698661f1..d14ffa6d 100644 --- a/index.js +++ b/index.js @@ -19,13 +19,13 @@ module.exports = { if (this._isCoverageEnabled() && this.parent.isEmberCLIAddon()) { var coveredAddon = this.project.findAddonByName(this.project.pkg.name); var coverageAddonContext = this; - var original = coveredAddon.addonJsFiles; - coveredAddon.addonJsFiles = function(tree) { - tree = coverageAddonContext.preprocessTree('addon-js', tree); - - return original.call(this, tree); - }; + coveredAddon.processedAddonJsFiles = function (tree){ + var instrumentedTree = coverageAddonContext.preprocessTree('addon-js', this.addonJsFiles(tree)); + return this.preprocessJs(instrumentedTree, '/', this.name, { + registry: this.registry + }); + } } }, @@ -50,8 +50,10 @@ module.exports = { * @returns {[type]} processed tree */ preprocessTree: function(type, tree) { + var useBabelInstrumenter = this._getConfig().useBabelInstrumenter === true; + // If coverage isn't enabled or tree is not JavaScript tree then we don't need to alter the tree - if (!this._isCoverageEnabled() || (type !== 'js' && type !== 'addon-js')) { + if (!this._isCoverageEnabled() || (type !== 'js' && type !=='addon-js')) { return tree; } @@ -60,8 +62,6 @@ module.exports = { exclude: this._getExcludes() }); - var useBabelInstrumenter = this._getConfig().useBabelInstrumenter === true; - // Instrument JavaScript for code coverage var instrumentedNode = new CoverageInstrumenter(appFiles, { annotation: 'Instrumenting for code coverage', @@ -124,7 +124,20 @@ module.exports = { return this._doesTemplateFileExist(relativePath); }, + /** + * Check if a file exists within the current addon directory. Removing `module/` from the path. + * @param {String} relativePath - path to file within current app + * @returns {Boolean} whether or not the file exists within the current app + */ + _doesFileExistIsCurrentProjectAddonModule: function(relativePath) { + relativePath = path.join('addon', relativePath.replace(path.join('modules', this.project.pkg.name),'')); + if (this._existsSync(relativePath)) { + return true; + } + + return this._doesTemplateFileExist(relativePath); + }, /** * Check if a file exists within the dummy app * @param {String} relativePath - path to file within dummy app @@ -189,7 +202,8 @@ module.exports = { var fileExists = ( this._doesFileExistInDummyApp(relativePath) || this._doesFileExistInCurrentProjectApp(relativePath) || - this._doesFileExistInCurrentProjectAddon(relativePath) + this._doesFileExistInCurrentProjectAddon(relativePath) || + this._doesFileExistIsCurrentProjectAddonModule(relativePath) ); console.log(!fileExists, name, relativePath); diff --git a/lib/coverage-instrumenter.js b/lib/coverage-instrumenter.js index 873c26a3..1a6b4f17 100644 --- a/lib/coverage-instrumenter.js +++ b/lib/coverage-instrumenter.js @@ -97,7 +97,11 @@ CoverageInstrumenter.prototype.processString = function(content, relativePath) { relativePath = fixPath(relativePath, this._appName, this._appRoot, this._templateExtensions); - return instrumenter.instrumentSync(content, relativePath); + try { + return instrumenter.instrumentSync(content, relativePath); + } catch (e) { + console.error('Unable to cover:', relativePath, '. Try setting useBabelInstrumenter to true. \n', e.stack); + } }; module.exports = CoverageInstrumenter; diff --git a/test/unit/index-test.js b/test/unit/index-test.js index dcdbb0f2..e81319fa 100644 --- a/test/unit/index-test.js +++ b/test/unit/index-test.js @@ -3,7 +3,6 @@ var expect = require('chai').expect; var sinon = require('sinon'); var Index = require('../../index.js'); - describe('index.js', function() { var sandbox @@ -96,13 +95,13 @@ describe('index.js', function() { }); }); - describe('_doesFileExistInCurrentProject', function() { + describe('_doesFileExistInCurrentProjectApp', function() { describe('when file exists', function() { var result; beforeEach(function() { sandbox.stub(Index, '_existsSync').returns(true); - result = Index._doesFileExistInCurrentProject('adapters/application.js'); + result = Index._doesFileExistInCurrentProjectApp('adapters/application.js'); }); it('uses path to file in app', function() { @@ -124,7 +123,7 @@ describe('index.js', function() { beforeEach(function() { sandbox.stub(Index, '_doesTemplateFileExist').returns(true); - result = Index._doesFileExistInCurrentProject('templates/application.js'); + result = Index._doesFileExistInCurrentProjectApp('templates/application.js'); }); it('uses path to file in app', function() { @@ -141,7 +140,7 @@ describe('index.js', function() { beforeEach(function() { sandbox.stub(Index, '_doesTemplateFileExist').returns(false); - result = Index._doesFileExistInCurrentProject('templates/application.js'); + result = Index._doesFileExistInCurrentProjectApp('templates/application.js'); }); it('uses path to file in app', function() {