Skip to content

Commit 37390e6

Browse files
committed
Handle esm's error "improvements"
esm changes error stacks in a way that AVA is not expecting. This adds a few workarounds, though admittedly all the packages we're using and workarounds we're applying are making a bit of a mess. See https://github.com/standard-things/esm/wiki/improved-errors for esm's behavior.
1 parent a130a9e commit 37390e6

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/beautify-stack.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,8 @@ module.exports = stack => {
6868

6969
return stackUtils.clean(stack)
7070
// Remove the trailing newline inserted by the `stack-utils` module
71-
.trim();
71+
.trim()
72+
// Remove remaining file:// prefixes, inserted by `esm`, that are not
73+
// cleaned up by `stack-utils`
74+
.split('\n').map(line => line.replace(/\(file:\/\/([^/].+:\d+:\d+)\)$/, '($1)')).join('\n');
7275
};

lib/serialize-error.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ function trySerializeError(err, shouldBeautifyStack) {
101101
}
102102
retval.summary = retval.summary.trim();
103103
} else {
104-
retval.summary = lines[0];
104+
// Skip the source line header inserted by `esm`:
105+
// <https://github.com/standard-things/esm/wiki/improved-errors>
106+
retval.summary = lines.find(line => !/:\d+$/.test(line));
105107
}
106108
}
107109

test/serialize-error.js

+26
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,29 @@ test('creates multiline summaries for syntax errors', t => {
176176
t.is(serializedError.summary, 'Hello\nThere\nSyntaxError here');
177177
t.end();
178178
});
179+
180+
test('skips esm enhancement lines when finding the summary', t => {
181+
const error = new Error();
182+
Object.defineProperty(error, 'stack', {
183+
value: 'file://file.js:1\nHello'
184+
});
185+
const serializedError = serialize(error);
186+
t.is(serializedError.summary, 'Hello');
187+
t.end();
188+
});
189+
190+
test('works around esm\'s insertion of file:// urls', t => {
191+
const fixture = sourceMapFixtures.mapFile('throws');
192+
try {
193+
fixture.require().run();
194+
t.fail('Fixture should have thrown');
195+
} catch (error) {
196+
const expected = serialize(error);
197+
Object.defineProperty(error, 'stack', {
198+
value: error.stack.split('\n').map(line => line.replace('(/', '(file:///')).join('\n')
199+
});
200+
const serializedError = serialize(error);
201+
t.is(serializedError.source.file, expected.source.file);
202+
t.end();
203+
}
204+
});

0 commit comments

Comments
 (0)