[test] add sourceMap test against relative sources

This commit is contained in:
Swaagie 2016-11-07 10:56:51 +01:00
parent c898a7997d
commit fcb4f2f215

View File

@ -3,12 +3,10 @@ var assert = require("assert");
var SourceMapConsumer = require("source-map").SourceMapConsumer; var SourceMapConsumer = require("source-map").SourceMapConsumer;
describe("input sourcemaps", function() { describe("input sourcemaps", function() {
var transpiled = '"use strict";\n\n' + var transpilemap, map;
'var foo = function foo(x) {\n return "foo " + x;\n};\n' +
'console.log(foo("bar"));\n\n' +
'//# sourceMappingURL=bundle.js.map';
var transpilemap = { function getMap() {
return {
"version": 3, "version": 3,
"sources": ["index.js"], "sources": ["index.js"],
"names": [], "names": [],
@ -16,18 +14,43 @@ describe("input sourcemaps", function() {
"file": "bundle.js", "file": "bundle.js",
"sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"] "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
}; };
}
function prepareMap(sourceMap) {
var transpiled = '"use strict";\n\n' +
'var foo = function foo(x) {\n return "foo " + x;\n};\n' +
'console.log(foo("bar"));\n\n' +
'//# sourceMappingURL=bundle.js.map';
transpilemap = sourceMap || getMap();
var result = Uglify.minify(transpiled, { var result = Uglify.minify(transpiled, {
fromString: true, fromString: true,
inSourceMap: transpilemap, inSourceMap: transpilemap,
outSourceMap: true outSourceMap: true
}); });
var map = new SourceMapConsumer(result.map);
map = new SourceMapConsumer(result.map);
}
beforeEach(function () {
prepareMap();
});
it("Should copy over original sourcesContent", function() { it("Should copy over original sourcesContent", function() {
assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]); assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
}); });
it("Should copy sourcesContent if sources are relative", function () {
var relativeMap = getMap();
relativeMap.sources = ['./index.js'];
prepareMap(relativeMap);
assert.notEqual(map.sourcesContent, null);
assert.equal(map.sourcesContent.length, 1);
assert.equal(map.sourceContentFor("index.js"), transpilemap.sourcesContent[0]);
});
it("Final sourcemap should not have invalid mappings from inputSourceMap (issue #882)", function() { it("Final sourcemap should not have invalid mappings from inputSourceMap (issue #882)", function() {
// The original source has only 2 lines, check that mappings don't have more lines // The original source has only 2 lines, check that mappings don't have more lines