From 1794e1359eb5a8b3620f83b55e1827bc0c4612d9 Mon Sep 17 00:00:00 2001 From: Evgeny Shpilevsky Date: Wed, 20 Dec 2017 11:47:24 +0300 Subject: [PATCH] fix(coverage): support webpack sourcemaps format --- lib/coverage.js | 6 +++--- test/unit/coverage.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/coverage.js b/lib/coverage.js index d92411dc5..e220353c5 100644 --- a/lib/coverage.js +++ b/lib/coverage.js @@ -133,9 +133,9 @@ const Coverage = module.exports = inherit({ return; } - var base64Prefix = 'data:application/json;base64,'; - if (_.includes(sourceMapUrl, base64Prefix)) { - var base64Str = sourceMapUrl.split(base64Prefix)[1], + var base64Prefix = /^data:application\/json;(?:charset=utf-8;)?base64,/; + if (base64Prefix.test(sourceMapUrl)) { + var base64Str = sourceMapUrl.replace(base64Prefix, ''), sourceMapStr = new Buffer(base64Str, 'base64').toString('utf8'); return new SourceMapConsumer(JSON.parse(sourceMapStr)); } diff --git a/test/unit/coverage.js b/test/unit/coverage.js index 7ebd30fea..597991038 100644 --- a/test/unit/coverage.js +++ b/test/unit/coverage.js @@ -49,4 +49,42 @@ describe('coverage', () => { assert.deepEqual(coverage.byURL['http://some/url'].coverage, {'.some-selector': 'full'}); }); }); + + describe('getSourceMap', () => { + it('parse source map', () => { + const config = createConfig(); + const coverage = new Coverage(config); + + const ast = { + stylesheet: { + rules: [{ + type: 'comment', + comment: '# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdHlsZXMuY3NzIiwic291cmNlUm9vdCI6IiJ9' + }] + } + }; + + const map = coverage.getSourceMap(ast, '', ''); + + assert.equal(map.file, 'styles.css'); + }); + + it('parse webpack source map', () => { + const config = createConfig(); + const coverage = new Coverage(config); + + const ast = { + stylesheet: { + rules: [{ + type: 'comment', + comment: '# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJzdHlsZXMuY3NzIiwic291cmNlUm9vdCI6IiJ9' + }] + } + }; + + const map = coverage.getSourceMap(ast, '', ''); + + assert.equal(map.file, 'styles.css'); + }); + }); });