diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-06-22 01:04:15 +0800 |
---|---|---|
committer | alexlamsl <alexlamsl@gmail.com> | 2018-06-24 04:00:36 +0800 |
commit | 28330913d871dd4bdb4e59464c7230f9ae4174fa (patch) | |
tree | 32fba882ce0d546d14f461677c56e771e10f02e1 /test/mocha/sourcemaps.js | |
parent | 766a4147d4891a093b550e958268c48104330b52 (diff) | |
download | tracifyjs-28330913d871dd4bdb4e59464c7230f9ae4174fa.tar.gz tracifyjs-28330913d871dd4bdb4e59464c7230f9ae4174fa.zip |
improve `mocha` tests (#3195)
Diffstat (limited to 'test/mocha/sourcemaps.js')
-rw-r--r-- | test/mocha/sourcemaps.js | 110 |
1 files changed, 88 insertions, 22 deletions
diff --git a/test/mocha/sourcemaps.js b/test/mocha/sourcemaps.js index 8590b308..db79a64a 100644 --- a/test/mocha/sourcemaps.js +++ b/test/mocha/sourcemaps.js @@ -1,19 +1,52 @@ var assert = require("assert"); var readFileSync = require("fs").readFileSync; -var Uglify = require("../../"); +var SourceMapConsumer = require("source-map").SourceMapConsumer; +var UglifyJS = require("../.."); function read(path) { return readFileSync(path, "utf8"); } function source_map(code) { - return JSON.parse(Uglify.minify(code, { + return JSON.parse(UglifyJS.minify(code, { compress: false, mangle: false, sourceMap: true, }).map); } +function get_map() { + return { + "version": 3, + "sources": ["index.js"], + "names": [], + "mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ", + "file": "bundle.js", + "sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"] + }; +} + +function prepare_map(sourceMap) { + var code = [ + '"use strict";', + "", + "var foo = function foo(x) {", + ' return "foo " + x;', + "};", + 'console.log(foo("bar"));', + "", + "//# sourceMappingURL=bundle.js.map", + ].join("\n"); + var result = UglifyJS.minify(code, { + sourceMap: { + content: sourceMap, + includeSources: true, + } + }); + if (result.error) throw result.error; + return new SourceMapConsumer(result.map); +} + describe("sourcemaps", function() { it("Should give correct version", function() { var map = source_map("var x = 1 + 1;"); @@ -36,7 +69,7 @@ describe("sourcemaps", function() { }); it("Should mark array/object literals", function() { - var result = Uglify.minify([ + var result = UglifyJS.minify([ "var obj = {};", "obj.wat([]);", ].join("\n"), { @@ -50,7 +83,7 @@ describe("sourcemaps", function() { it("Should give correct sourceRoot", function() { var code = "console.log(42);"; - var result = Uglify.minify(code, { + var result = UglifyJS.minify(code, { sourceMap: { root: "//foo.bar/", }, @@ -61,8 +94,8 @@ describe("sourcemaps", function() { }); describe("inSourceMap", function() { - it("Should read the given string filename correctly when sourceMapIncludeSources is enabled (#1236)", function() { - var result = Uglify.minify(read("./test/input/issue-1236/simple.js"), { + it("Should read the given string filename correctly when sourceMapIncludeSources is enabled", function() { + var result = UglifyJS.minify(read("./test/input/issue-1236/simple.js"), { sourceMap: { content: read("./test/input/issue-1236/simple.js.map"), filename: "simple.min.js", @@ -76,7 +109,7 @@ describe("sourcemaps", function() { assert.equal(map.sourcesContent[0], 'let foo = x => "foo " + x;\nconsole.log(foo("bar"));'); }); it("Should process inline source map", function() { - var result = Uglify.minify(read("./test/input/issue-520/input.js"), { + var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), { compress: { toplevel: true }, sourceMap: { content: "inline", @@ -88,13 +121,13 @@ describe("sourcemaps", function() { assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-520/output.js", "utf8")); }); it("Should warn for missing inline source map", function() { - var warn_function = Uglify.AST_Node.warn_function; + var warn_function = UglifyJS.AST_Node.warn_function; var warnings = []; - Uglify.AST_Node.warn_function = function(txt) { + UglifyJS.AST_Node.warn_function = function(txt) { warnings.push(txt); }; try { - var result = Uglify.minify(read("./test/input/issue-1323/sample.js"), { + var result = UglifyJS.minify(read("./test/input/issue-1323/sample.js"), { mangle: false, sourceMap: { content: "inline" @@ -104,17 +137,17 @@ describe("sourcemaps", function() { assert.strictEqual(warnings.length, 1); assert.strictEqual(warnings[0], "inline source map not found: 0"); } finally { - Uglify.AST_Node.warn_function = warn_function; + UglifyJS.AST_Node.warn_function = warn_function; } }); it("Should handle multiple input and inline source map", function() { - var warn_function = Uglify.AST_Node.warn_function; + var warn_function = UglifyJS.AST_Node.warn_function; var warnings = []; - Uglify.AST_Node.warn_function = function(txt) { + UglifyJS.AST_Node.warn_function = function(txt) { warnings.push(txt); }; try { - var result = Uglify.minify([ + var result = UglifyJS.minify([ read("./test/input/issue-520/input.js"), read("./test/input/issue-1323/sample.js"), ], { @@ -131,11 +164,11 @@ describe("sourcemaps", function() { assert.strictEqual(warnings.length, 1); assert.strictEqual(warnings[0], "inline source map not found: 1"); } finally { - Uglify.AST_Node.warn_function = warn_function; + UglifyJS.AST_Node.warn_function = warn_function; } }); it("Should drop source contents for includeSources=false", function() { - var result = Uglify.minify(read("./test/input/issue-520/input.js"), { + var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), { compress: false, mangle: false, sourceMap: { @@ -146,7 +179,7 @@ describe("sourcemaps", function() { if (result.error) throw result.error; var map = JSON.parse(result.map); assert.strictEqual(map.sourcesContent.length, 1); - result = Uglify.minify(result.code, { + result = UglifyJS.minify(result.code, { compress: false, mangle: false, sourceMap: { @@ -161,7 +194,7 @@ describe("sourcemaps", function() { describe("sourceMapInline", function() { it("Should append source map to output js when sourceMapInline is enabled", function() { - var result = Uglify.minify('var a = function(foo) { return foo; };', { + var result = UglifyJS.minify('var a = function(foo) { return foo; };', { sourceMap: { url: "inline" } @@ -172,13 +205,13 @@ describe("sourcemaps", function() { "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0="); }); it("Should not append source map to output js when sourceMapInline is not enabled", function() { - var result = Uglify.minify('var a = function(foo) { return foo; };'); + var result = UglifyJS.minify('var a = function(foo) { return foo; };'); if (result.error) throw result.error; var code = result.code; assert.strictEqual(code, "var a=function(n){return n};"); }); it("Should work with max_line_len", function() { - var result = Uglify.minify(read("./test/input/issue-505/input.js"), { + var result = UglifyJS.minify(read("./test/input/issue-505/input.js"), { output: { max_line_len: 20 }, @@ -194,7 +227,7 @@ describe("sourcemaps", function() { "var tëst = '→unicøde←';", "alert(tëst);", ].join("\n"); - var result = Uglify.minify(code, { + var result = UglifyJS.minify(code, { sourceMap: { includeSources: true, url: "inline", @@ -208,7 +241,7 @@ describe("sourcemaps", function() { map = JSON.parse(new Buffer(encoded, "base64").toString()); assert.strictEqual(map.sourcesContent.length, 1); assert.strictEqual(map.sourcesContent[0], code); - result = Uglify.minify(result.code, { + result = UglifyJS.minify(result.code, { sourceMap: { content: "inline", includeSources: true, @@ -221,4 +254,37 @@ describe("sourcemaps", function() { assert.strictEqual(map.names[1], "alert"); }); }); + + describe("input sourcemaps", function() { + it("Should copy over original sourcesContent", function() { + var orig = get_map(); + var map = prepare_map(orig); + assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]); + }); + + it("Should copy sourcesContent if sources are relative", function() { + var relativeMap = get_map(); + relativeMap.sources = ['./index.js']; + var map = prepare_map(relativeMap); + assert.notEqual(map.sourcesContent, null); + assert.equal(map.sourcesContent.length, 1); + assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]); + }); + + it("Should not have invalid mappings from inputSourceMap", function() { + var map = prepare_map(get_map()); + // The original source has only 2 lines, check that mappings don't have more lines + var msg = "Mapping should not have higher line number than the original file had"; + map.eachMapping(function(mapping) { + assert.ok(mapping.originalLine <= 2, msg); + }); + map.allGeneratedPositionsFor({ + source: "index.js", + line: 1, + column: 1 + }).forEach(function(pos) { + assert.ok(pos.line <= 2, msg); + }); + }); + }); }); |