lib: fix coverage reporting

Taking the source code of a function and running it in another
context does not play well with coverage instrumentation.
For now, do the simple thing and just write the source code as
a string literal.

Fixes: https://github.com/nodejs/node/issues/19912
Refs: https://github.com/nodejs/node/pull/19524

PR-URL: https://github.com/nodejs/node/pull/20035
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Anna Henningsen 2018-04-14 19:01:37 +02:00
parent c974f1bbe8
commit 85e1819d8b
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9

View File

@ -390,17 +390,16 @@ function isInsideNodeModules() {
// Use `runInNewContext()` to get something tamper-proof and // Use `runInNewContext()` to get something tamper-proof and
// side-effect-free. Since this is currently only used for a deprecated API, // side-effect-free. Since this is currently only used for a deprecated API,
// the perf implications should be okay. // the perf implications should be okay.
getStructuredStack = runInNewContext('(' + function() { getStructuredStack = runInNewContext(`(function() {
Error.prepareStackTrace = function(err, trace) { Error.prepareStackTrace = function(err, trace) {
err.stack = trace; err.stack = trace;
}; };
Error.stackTraceLimit = Infinity; Error.stackTraceLimit = Infinity;
return function structuredStack() { return function structuredStack() {
// eslint-disable-next-line no-restricted-syntax
return new Error().stack; return new Error().stack;
}; };
} + ')()', {}, { filename: 'structured-stack' }); })()`, {}, { filename: 'structured-stack' });
} }
const stack = getStructuredStack(); const stack = getStructuredStack();