feat(test): skip internal stack frames for errors (#14302)

This commit changes "deno test" to filter out stack frames if it is beneficial to the user.

This is the case when there are stack frames coming from "internal" code
below frames coming from user code.

Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2022-04-18 15:22:23 +02:00 committed by GitHub
parent 7919dc902d
commit f52031ecdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 72 additions and 54 deletions

View file

@ -301,10 +301,7 @@ impl PrettyTestReporter {
);
if let Some(js_error) = result.error() {
let err_string = PrettyJsError::create(js_error.clone())
.to_string()
.trim_start_matches("Uncaught ")
.to_string();
let err_string = format_test_error(js_error);
for line in err_string.lines() {
println!("{}{}", " ".repeat(description.level + 1), line);
}
@ -464,11 +461,7 @@ impl TestReporter for PrettyTestReporter {
colors::gray(">"),
description.name
);
let err_string = PrettyJsError::create(*js_error.clone())
.to_string()
.trim_start_matches("Uncaught ")
.to_string();
println!("{}", err_string);
println!("{}", format_test_error(js_error));
println!();
}
@ -525,6 +518,65 @@ impl TestReporter for PrettyTestReporter {
}
}
fn abbreviate_test_error(js_error: &JsError) -> JsError {
let mut js_error = js_error.clone();
let frames = std::mem::take(&mut js_error.frames);
// check if there are any stack frames coming from user code
let should_filter = frames.iter().any(|f| {
if let Some(file_name) = &f.file_name {
!(file_name.starts_with("[deno:") || file_name.starts_with("deno:"))
} else {
true
}
});
if should_filter {
let mut frames = frames
.into_iter()
.rev()
.skip_while(|f| {
if let Some(file_name) = &f.file_name {
file_name.starts_with("[deno:") || file_name.starts_with("deno:")
} else {
false
}
})
.into_iter()
.collect::<Vec<_>>();
frames.reverse();
js_error.frames = frames;
} else {
js_error.frames = frames;
}
js_error.cause = js_error
.cause
.as_ref()
.map(|e| Box::new(abbreviate_test_error(e)));
js_error.aggregated = js_error
.aggregated
.as_ref()
.map(|es| es.iter().map(abbreviate_test_error).collect());
js_error
}
// This function maps JsError to PrettyJsError and applies some changes
// specifically for test runner purposes:
//
// - filter out stack frames:
// - if stack trace consists of mixed user and internal code, the frames
// below the first user code frame are filtered out
// - if stack trace consists only of internal code it is preserved as is
pub fn format_test_error(js_error: &JsError) -> String {
let mut js_error = abbreviate_test_error(js_error);
js_error.exception_message = js_error
.exception_message
.trim_start_matches("Uncaught ")
.to_string();
PrettyJsError::create(js_error).to_string()
}
fn create_reporter(
concurrent: bool,
echo_output: bool,