2013-07-23 00:21:10 +08:00
|
|
|
require('./source-map-support').install({
|
|
|
|
emptyCacheBetweenOperations: true // Needed to be able to test for failure
|
|
|
|
});
|
2013-01-18 15:56:20 +08:00
|
|
|
|
|
|
|
var SourceMapGenerator = require('source-map').SourceMapGenerator;
|
2013-03-06 03:36:36 +08:00
|
|
|
var child_process = require('child_process');
|
2013-01-18 15:56:20 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
var fs = require('fs');
|
|
|
|
|
2013-04-25 08:56:13 +08:00
|
|
|
function compareLines(actual, expected) {
|
|
|
|
assert(actual.length >= expected.length, 'got ' + actual.length + ' lines but expected at least ' + expected.length + ' lines');
|
|
|
|
for (var i = 0; i < expected.length; i++) {
|
|
|
|
// Some tests are regular expressions because the output format changed slightly between node v0.9.2 and v0.9.3
|
|
|
|
if (expected[i] instanceof RegExp) {
|
|
|
|
assert(expected[i].test(actual[i]), JSON.stringify(actual[i]) + ' does not match ' + expected[i]);
|
|
|
|
} else {
|
|
|
|
assert.equal(actual[i], expected[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:20:34 +08:00
|
|
|
function createEmptySourceMap() {
|
|
|
|
return new SourceMapGenerator({
|
2013-03-06 03:36:36 +08:00
|
|
|
file: '.generated.js',
|
|
|
|
sourceRoot: '.'
|
|
|
|
});
|
2013-06-27 14:20:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createSourceMapWithGap() {
|
|
|
|
var sourceMap = createEmptySourceMap();
|
|
|
|
sourceMap.addMapping({
|
2014-04-27 02:43:54 +08:00
|
|
|
generated: { line: 100, column: 0 },
|
|
|
|
original: { line: 100, column: 0 },
|
2013-06-27 14:20:34 +08:00
|
|
|
source: '.original.js'
|
|
|
|
});
|
|
|
|
return sourceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createSingleLineSourceMap() {
|
|
|
|
var sourceMap = createEmptySourceMap();
|
|
|
|
sourceMap.addMapping({
|
2014-04-27 02:43:54 +08:00
|
|
|
generated: { line: 1, column: 0 },
|
|
|
|
original: { line: 1, column: 0 },
|
2013-06-27 14:20:34 +08:00
|
|
|
source: '.original.js'
|
|
|
|
});
|
|
|
|
return sourceMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMultiLineSourceMap() {
|
|
|
|
var sourceMap = createEmptySourceMap();
|
2013-03-06 03:36:36 +08:00
|
|
|
for (var i = 1; i <= 100; i++) {
|
|
|
|
sourceMap.addMapping({
|
2014-04-27 02:43:54 +08:00
|
|
|
generated: { line: i, column: 0 },
|
|
|
|
original: { line: 1000 + i, column: 99 + i },
|
2013-03-06 03:36:36 +08:00
|
|
|
source: 'line' + i + '.js'
|
|
|
|
});
|
|
|
|
}
|
2013-06-27 14:20:34 +08:00
|
|
|
return sourceMap;
|
|
|
|
}
|
2013-06-27 13:51:13 +08:00
|
|
|
|
2014-04-27 03:15:02 +08:00
|
|
|
function createMultiLineSourceMapWithSourcesContent() {
|
|
|
|
var sourceMap = createEmptySourceMap();
|
|
|
|
var original = new Array(1001).join('\n');
|
|
|
|
for (var i = 1; i <= 100; i++) {
|
|
|
|
sourceMap.addMapping({
|
|
|
|
generated: { line: i, column: 0 },
|
|
|
|
original: { line: 1000 + i, column: 4 },
|
|
|
|
source: 'original.js'
|
|
|
|
});
|
|
|
|
original += ' line ' + i + '\n';
|
|
|
|
}
|
|
|
|
sourceMap.setSourceContent('original.js', original);
|
|
|
|
return sourceMap;
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:20:34 +08:00
|
|
|
function compareStackTrace(sourceMap, source, expected) {
|
2013-06-27 13:51:13 +08:00
|
|
|
// Check once with a separate source map
|
2013-01-18 15:56:20 +08:00
|
|
|
fs.writeFileSync('.generated.js.map', sourceMap);
|
|
|
|
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
|
|
|
|
source.join('\n') + '};//@ sourceMappingURL=.generated.js.map');
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve('./.generated')];
|
|
|
|
require('./.generated').test();
|
|
|
|
} catch (e) {
|
2013-04-25 08:56:13 +08:00
|
|
|
compareLines(e.stack.split('\n'), expected);
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
fs.unlinkSync('.generated.js.map');
|
2013-06-27 13:51:13 +08:00
|
|
|
|
|
|
|
// Check again with an inline source map (in a data URL)
|
|
|
|
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
|
|
|
|
source.join('\n') + '};//@ sourceMappingURL=data:application/json;base64,' +
|
|
|
|
new Buffer(sourceMap.toString()).toString('base64'));
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve('./.generated')];
|
|
|
|
require('./.generated').test();
|
|
|
|
} catch (e) {
|
|
|
|
compareLines(e.stack.split('\n'), expected);
|
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
2013-01-18 15:56:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-27 14:20:34 +08:00
|
|
|
function compareStdout(done, sourceMap, source, expected) {
|
2013-03-06 03:36:36 +08:00
|
|
|
fs.writeFileSync('.original.js', 'this is the original code');
|
|
|
|
fs.writeFileSync('.generated.js.map', sourceMap);
|
|
|
|
fs.writeFileSync('.generated.js', source.join('\n') +
|
|
|
|
'//@ sourceMappingURL=.generated.js.map');
|
|
|
|
child_process.exec('node ./.generated', function(error, stdout, stderr) {
|
|
|
|
try {
|
2015-10-21 15:18:06 +08:00
|
|
|
compareLines(
|
|
|
|
(stdout + stderr)
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.filter(function (line) { return line !== '' }), // Empty lines are not relevant.
|
|
|
|
expected
|
|
|
|
);
|
2013-03-06 03:36:36 +08:00
|
|
|
} catch (e) {
|
|
|
|
return done(e);
|
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
fs.unlinkSync('.generated.js.map');
|
|
|
|
fs.unlinkSync('.original.js');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-18 15:56:20 +08:00
|
|
|
it('normal throw', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throw inside function', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'function foo() {',
|
|
|
|
' throw new Error("test");',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/line2\.js:1002:102\)$/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line4\.js:1004:104\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throw inside function inside function', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'function foo() {',
|
|
|
|
' function bar() {',
|
|
|
|
' throw new Error("test");',
|
|
|
|
' }',
|
|
|
|
' bar();',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at bar \(.*\/line3\.js:1003:103\)$/,
|
|
|
|
/^ at foo \(.*\/line5\.js:1005:105\)$/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line7\.js:1007:107\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'eval("throw new Error(\'test\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-10-21 15:18:06 +08:00
|
|
|
|
|
|
|
// Before Node 4, `Object.eval`, after just `eval`.
|
|
|
|
/^ at (?:Object\.)?eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval inside eval', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'eval("eval(\'throw new Error(\\"test\\")\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-10-21 15:18:06 +08:00
|
|
|
/^ at (?:Object\.)?eval \(eval at <anonymous> \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
|
|
|
/^ at (?:Object\.)?eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval inside function', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'function foo() {',
|
|
|
|
' eval("throw new Error(\'test\')");',
|
|
|
|
'}',
|
|
|
|
'foo();'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at eval \(eval at foo \(.*\/line2\.js:1002:102\)/,
|
|
|
|
/^ at foo \(.*\/line2\.js:1002:102\)/,
|
|
|
|
/^ at Object\.exports\.test \(.*\/line4\.js:1004:104\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval with sourceURL', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'eval("throw new Error(\'test\')//@ sourceURL=sourceURL.js");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-10-21 15:18:06 +08:00
|
|
|
/^ at (?:Object\.)?eval \(sourceURL\.js:1:7\)$/,
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('eval with sourceURL inside eval', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-01-18 15:56:20 +08:00
|
|
|
'eval("eval(\'throw new Error(\\"test\\")//@ sourceURL=sourceURL.js\')");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-10-21 15:18:06 +08:00
|
|
|
/^ at (?:Object\.)?eval \(sourceURL\.js:1:7\)$/,
|
|
|
|
/^ at (?:Object\.)?eval \(eval at <anonymous> \(.*\/line1\.js:1001:101\)/,
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
2013-01-18 15:56:20 +08:00
|
|
|
]);
|
|
|
|
});
|
2013-03-06 03:36:36 +08:00
|
|
|
|
2016-03-27 21:12:52 +08:00
|
|
|
it('native function', function() {
|
|
|
|
compareStackTrace(createSingleLineSourceMap(), [
|
|
|
|
'[1].map(function(x) { throw new Error(x); });'
|
|
|
|
], [
|
|
|
|
'Error: 1',
|
|
|
|
/\/.original\.js/,
|
|
|
|
/at Array\.map \(native\)/
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2013-04-29 05:30:55 +08:00
|
|
|
it('function constructor', function() {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStackTrace(createMultiLineSourceMap(), [
|
2013-04-29 05:30:55 +08:00
|
|
|
'throw new Function(")");'
|
|
|
|
], [
|
|
|
|
'SyntaxError: Unexpected token )',
|
2015-05-26 16:41:04 +08:00
|
|
|
/^ at (?:Object\.)?Function \((?:unknown source|<anonymous>|native)\)$/,
|
2013-04-29 05:30:55 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/,
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2013-06-27 14:20:34 +08:00
|
|
|
it('throw with empty source map', function() {
|
|
|
|
compareStackTrace(createEmptySourceMap(), [
|
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-06-02 21:43:39 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/.generated.js:1:34\)$/
|
2013-06-27 14:20:34 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throw with source map with gap', function() {
|
|
|
|
compareStackTrace(createSourceMapWithGap(), [
|
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
2015-06-02 21:43:39 +08:00
|
|
|
/^ at Object\.exports\.test \(.*\/.generated.js:1:34\)$/
|
2013-06-27 14:20:34 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2014-04-27 03:24:31 +08:00
|
|
|
it('sourcesContent with data URL', function() {
|
|
|
|
compareStackTrace(createMultiLineSourceMapWithSourcesContent(), [
|
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
|
|
|
/^ at Object\.exports\.test \(.*\/original.js:1001:5\)$/
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-04-10 02:50:03 +08:00
|
|
|
it('finds the last sourceMappingURL', function() {
|
|
|
|
compareStackTrace(createMultiLineSourceMapWithSourcesContent(), [
|
|
|
|
'//# sourceMappingURL=missing.map.js', // NB: compareStackTrace adds another source mapping.
|
|
|
|
'throw new Error("test");'
|
|
|
|
], [
|
|
|
|
'Error: test',
|
|
|
|
/^ at Object\.exports\.test \(.*\/original.js:1002:5\)$/
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2013-03-06 03:36:36 +08:00
|
|
|
it('default options', function(done) {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
2013-03-06 03:36:36 +08:00
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(function() { process.exit(1); });'
|
|
|
|
], [
|
2013-04-25 10:16:25 +08:00
|
|
|
/\/.original\.js:1$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'this is the original code',
|
|
|
|
'^',
|
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleUncaughtExceptions is true', function(done) {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
2013-03-06 03:36:36 +08:00
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install({ handleUncaughtExceptions: true });',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
2013-04-25 10:16:25 +08:00
|
|
|
/\/.original\.js:1$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'this is the original code',
|
|
|
|
'^',
|
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handleUncaughtExceptions is false', function(done) {
|
2013-06-27 14:20:34 +08:00
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
2013-03-06 03:36:36 +08:00
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install({ handleUncaughtExceptions: false });',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
2013-04-25 08:56:13 +08:00
|
|
|
/\/.generated.js:2$/,
|
2013-03-06 03:36:36 +08:00
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
2015-10-21 15:18:06 +08:00
|
|
|
|
|
|
|
// Before Node 4, the arrow points on the `new`, after on the
|
|
|
|
// `throw`.
|
|
|
|
/^ (?: )?\^$/,
|
|
|
|
|
2013-03-06 03:36:36 +08:00
|
|
|
'Error: this is the error',
|
2013-04-25 10:16:25 +08:00
|
|
|
/^ at foo \(.*\/.original\.js:1:1\)$/
|
2013-03-06 03:36:36 +08:00
|
|
|
]);
|
|
|
|
});
|
2013-06-27 14:20:34 +08:00
|
|
|
|
|
|
|
it('default options with empty source map', function(done) {
|
|
|
|
compareStdout(done, createEmptySourceMap(), [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
|
|
|
/\/.generated.js:2$/,
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
2015-10-21 15:18:06 +08:00
|
|
|
/^ (?: )?\^$/,
|
2013-06-27 14:20:34 +08:00
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/.generated.js:2:24\)$/
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('default options with source map with gap', function(done) {
|
|
|
|
compareStdout(done, createSourceMapWithGap(), [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
|
|
|
/\/.generated.js:2$/,
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
2015-10-21 15:18:06 +08:00
|
|
|
/^ (?: )?\^$/,
|
2013-06-27 14:20:34 +08:00
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/.generated.js:2:24\)$/
|
|
|
|
]);
|
|
|
|
});
|
2013-12-23 06:28:04 +08:00
|
|
|
|
|
|
|
it('specifically requested error source', function(done) {
|
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'var sms = require("./source-map-support");',
|
|
|
|
'sms.install({ handleUncaughtExceptions: false });',
|
2014-04-27 02:43:54 +08:00
|
|
|
'process.on("uncaughtException", function (e) { console.log("SRC:" + sms.getErrorSource(e)); });',
|
2013-12-23 06:28:04 +08:00
|
|
|
'process.nextTick(foo);'
|
|
|
|
], [
|
2015-05-26 16:37:14 +08:00
|
|
|
/^SRC:.*\/.original.js:1$/,
|
2013-12-23 06:28:04 +08:00
|
|
|
'this is the original code',
|
|
|
|
'^'
|
|
|
|
]);
|
|
|
|
});
|
2014-04-27 03:15:02 +08:00
|
|
|
|
|
|
|
it('sourcesContent', function(done) {
|
|
|
|
compareStdout(done, createMultiLineSourceMapWithSourcesContent(), [
|
|
|
|
'',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(function() { process.exit(1); });'
|
|
|
|
], [
|
|
|
|
/\/original\.js:1002$/,
|
|
|
|
' line 2',
|
|
|
|
' ^',
|
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/original\.js:1002:5\)$/
|
|
|
|
]);
|
|
|
|
});
|
2015-03-01 01:54:29 +08:00
|
|
|
|
|
|
|
it('missing source maps should also be cached', function(done) {
|
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
|
|
|
'',
|
|
|
|
'var count = 0;',
|
|
|
|
'function foo() {',
|
|
|
|
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
|
|
|
|
'}',
|
|
|
|
'require("./source-map-support").install({',
|
2015-08-27 05:25:38 +08:00
|
|
|
' overrideRetrieveSourceMap: true,',
|
2015-03-01 01:54:29 +08:00
|
|
|
' retrieveSourceMap: function(name) {',
|
|
|
|
' if (/\\.generated.js$/.test(name)) count++;',
|
|
|
|
' return null;',
|
|
|
|
' }',
|
|
|
|
'});',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(function() { console.log(count); });',
|
|
|
|
], [
|
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/.generated.js:4:15\)$/,
|
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/.generated.js:4:15\)$/,
|
|
|
|
'1', // The retrieval should only be attempted once
|
|
|
|
]);
|
|
|
|
});
|
2015-06-16 15:10:41 +08:00
|
|
|
|
2015-08-27 05:25:38 +08:00
|
|
|
it('should consult all retrieve source map providers', function(done) {
|
|
|
|
compareStdout(done, createSingleLineSourceMap(), [
|
|
|
|
'',
|
|
|
|
'var count = 0;',
|
|
|
|
'function foo() {',
|
|
|
|
' console.log(new Error("this is the error").stack.split("\\n").slice(0, 2).join("\\n"));',
|
|
|
|
'}',
|
|
|
|
'require("./source-map-support").install({',
|
|
|
|
' retrieveSourceMap: function(name) {',
|
|
|
|
' if (/\\.generated.js$/.test(name)) count++;',
|
|
|
|
' return undefined;',
|
|
|
|
' }',
|
|
|
|
'});',
|
|
|
|
'require("./source-map-support").install({',
|
|
|
|
' retrieveSourceMap: function(name) {',
|
|
|
|
' if (/\\.generated.js$/.test(name)) {',
|
|
|
|
' count++;',
|
|
|
|
' return ' + JSON.stringify({url: '.original.js', map: createMultiLineSourceMapWithSourcesContent().toJSON()}) + ';',
|
|
|
|
' }',
|
|
|
|
' }',
|
|
|
|
'});',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'process.nextTick(function() { console.log(count); });',
|
|
|
|
], [
|
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/original.js:1004:5\)$/,
|
|
|
|
'Error: this is the error',
|
|
|
|
/^ at foo \(.*\/original.js:1004:5\)$/,
|
|
|
|
'1', // The retrieval should only be attempted once
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2015-06-16 15:10:41 +08:00
|
|
|
/* The following test duplicates some of the code in
|
|
|
|
* `compareStackTrace` but appends a charset to the
|
|
|
|
* source mapping url.
|
|
|
|
*/
|
|
|
|
it('finds source maps with charset specified', function() {
|
|
|
|
var sourceMap = createMultiLineSourceMap()
|
|
|
|
var source = [ 'throw new Error("test");' ];
|
|
|
|
var expected = [
|
|
|
|
'Error: test',
|
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
|
|
|
];
|
|
|
|
|
|
|
|
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
|
|
|
|
source.join('\n') + '};//@ sourceMappingURL=data:application/json;charset=utf8;base64,' +
|
|
|
|
new Buffer(sourceMap.toString()).toString('base64'));
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve('./.generated')];
|
|
|
|
require('./.generated').test();
|
|
|
|
} catch (e) {
|
|
|
|
compareLines(e.stack.split('\n'), expected);
|
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
});
|
2015-06-16 16:00:49 +08:00
|
|
|
|
2015-07-27 21:34:21 +08:00
|
|
|
/* The following test duplicates some of the code in
|
|
|
|
* `compareStackTrace` but appends some code and a
|
|
|
|
* comment to the source mapping url.
|
|
|
|
*/
|
|
|
|
it('allows code/comments after sourceMappingURL', function() {
|
|
|
|
var sourceMap = createMultiLineSourceMap()
|
|
|
|
var source = [ 'throw new Error("test");' ];
|
|
|
|
var expected = [
|
|
|
|
'Error: test',
|
|
|
|
/^ at Object\.exports\.test \(.*\/line1\.js:1001:101\)$/
|
|
|
|
];
|
|
|
|
|
|
|
|
fs.writeFileSync('.generated.js', 'exports.test = function() {' +
|
|
|
|
source.join('\n') + '};//# sourceMappingURL=data:application/json;base64,' +
|
|
|
|
new Buffer(sourceMap.toString()).toString('base64') +
|
|
|
|
'\n// Some comment below the sourceMappingURL\nvar foo = 0;');
|
|
|
|
try {
|
|
|
|
delete require.cache[require.resolve('./.generated')];
|
|
|
|
require('./.generated').test();
|
|
|
|
} catch (e) {
|
|
|
|
compareLines(e.stack.split('\n'), expected);
|
|
|
|
}
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
});
|
|
|
|
|
2015-06-16 16:00:49 +08:00
|
|
|
it('handleUncaughtExceptions is true with existing listener', function(done) {
|
|
|
|
var source = [
|
|
|
|
'process.on("uncaughtException", function() { /* Silent */ });',
|
|
|
|
'function foo() { throw new Error("this is the error"); }',
|
|
|
|
'require("./source-map-support").install();',
|
|
|
|
'process.nextTick(foo);',
|
|
|
|
'//@ sourceMappingURL=.generated.js.map'
|
|
|
|
];
|
|
|
|
|
|
|
|
fs.writeFileSync('.original.js', 'this is the original code');
|
|
|
|
fs.writeFileSync('.generated.js.map', createSingleLineSourceMap());
|
|
|
|
fs.writeFileSync('.generated.js', source.join('\n'));
|
|
|
|
|
|
|
|
child_process.exec('node ./.generated', function(error, stdout, stderr) {
|
|
|
|
fs.unlinkSync('.generated.js');
|
|
|
|
fs.unlinkSync('.generated.js.map');
|
|
|
|
fs.unlinkSync('.original.js');
|
|
|
|
assert.equal((stdout + stderr).trim(), '');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|