Skip to content

Commit c792a33

Browse files
authored
tests: show unminified stacktraces for Errors (pouchdb#8841)
Re-maps stacktraces of Errors thrown in mocha tests run against pouchdb.min.js in browser tests when run with playwright.
1 parent 5cd750f commit c792a33

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

bin/build-utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ function writeFile(filename, contents) {
2929
}
3030

3131
function doUglify(pkgName, code, prepend, fileOut) {
32-
var miniCode = prepend + terser.minify(code, { output: { ascii_only: true }}).code;
33-
return writeFile(addPath(pkgName, fileOut), miniCode);
32+
const filename = fileOut.replace(/.*\//, '');
33+
const sourceMap = { filename, url: filename + '.map' };
34+
const minified = terser.minify(code, { output: { ascii_only: true }, sourceMap });
35+
return writeFile(addPath(pkgName, fileOut), prepend + minified.code)
36+
.then(() => writeFile(addPath(pkgName, fileOut) + '.map', minified.map));
3437
}
3538

3639
var browserifyCache = {};

bin/test-browser.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env node
22
'use strict';
33

4+
const fs = require('node:fs');
45
const playwright = require('playwright');
5-
66
const { identity, pickBy } = require('lodash');
7+
const { SourceMapConsumer } = require('source-map');
8+
const stacktraceParser = require('stacktrace-parser');
79

810
var MochaSpecReporter = require('mocha').reporters.Spec;
911
const createMochaStatsCollector = require('mocha/lib/stats-collector');
@@ -61,6 +63,8 @@ const qs = {
6163
testUrl += '?';
6264
testUrl += new URLSearchParams(pickBy(qs, identity));
6365

66+
let stackConsumer;
67+
6468
class ArrayMap extends Map {
6569
get(key) {
6670
if (!this.has(key)) {
@@ -108,6 +112,35 @@ class RemoteRunner {
108112

109113
this.triggerHandlers(event.name, [ obj, event.err ]);
110114

115+
if (event.err && stackConsumer) {
116+
let stackMapped;
117+
const mappedStack = stacktraceParser
118+
.parse(event.err.stack)
119+
.map(v => {
120+
if (v.file === 'http://127.0.0.1:8000/packages/node_modules/pouchdb/dist/pouchdb.min.js') {
121+
const NON_UGLIFIED_HEADER_LENGTH = 6; // number of lines of header added in build-pouchdb.js
122+
const target = { line:v.lineNumber-NON_UGLIFIED_HEADER_LENGTH, column:v.column-1 };
123+
const mapped = stackConsumer.originalPositionFor(target);
124+
v.file = 'packages/node_modules/pouchdb/dist/pouchdb.js';
125+
v.lineNumber = mapped.line;
126+
v.column = mapped.column+1;
127+
if (mapped.name !== null) {
128+
v.methodName = mapped.name;
129+
}
130+
stackMapped = true;
131+
}
132+
return v;
133+
})
134+
// NodeJS stack frame format: https://nodejs.org/docs/latest/api/errors.html#errorstack
135+
.map(v => `at ${v.methodName} (${v.file}:${v.lineNumber}:${v.column})`)
136+
.join('\n ');
137+
if (stackMapped) {
138+
console.log(` [${obj.title}] Minified error stacktrace mapped to:`);
139+
console.log(` ${event.err.name||'Error'}: ${event.err.message}`);
140+
console.log(` ${mappedStack}`);
141+
}
142+
}
143+
111144
switch (event.name) {
112145
case 'fail': this.handleFailed(); break;
113146
case 'end': this.handleEnd(); break;
@@ -152,19 +185,24 @@ function BenchmarkJsonReporter(runner) {
152185
if (runner.failed) {
153186
console.log('Runner failed; JSON will not be writted.');
154187
} else {
155-
const { mkdirSync, writeFileSync } = require('fs');
156-
157188
const resultsDir = 'perf-test-results';
158-
mkdirSync(resultsDir, { recursive: true });
189+
fs.mkdirSync(resultsDir, { recursive: true });
159190

160191
const jsonPath = `${resultsDir}/${new Date().toISOString()}.json`;
161-
writeFileSync(jsonPath, JSON.stringify(results, null, 2));
192+
fs.writeFileSync(jsonPath, JSON.stringify(results, null, 2));
162193
console.log('Wrote JSON results to:', jsonPath);
163194
}
164195
});
165196
}
166197

167198
async function startTest() {
199+
if (qs.src === '../../packages/node_modules/pouchdb/dist/pouchdb.min.js') {
200+
const mapPath = './packages/node_modules/pouchdb/dist/pouchdb.min.js.map';
201+
const rawMap = fs.readFileSync(mapPath, { encoding:'utf8' });
202+
const jsonMap = JSON.parse(rawMap);
203+
stackConsumer = await new SourceMapConsumer(jsonMap);
204+
}
205+
168206
try {
169207
console.log('Starting', browserName, 'on', testUrl);
170208

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
"rollup-plugin-node-resolve": "4.2.4",
100100
"rollup-plugin-replace": "1.2.1",
101101
"seedrandom": "3.0.5",
102+
"source-map": "0.7.4",
103+
"stacktrace-parser": "0.1.10",
102104
"stream-to-promise": "1.1.1",
103105
"tape": "4.13.0",
104106
"terser": "4.8.0",

0 commit comments

Comments
 (0)