summaryrefslogtreecommitdiff
path: root/cli/tools
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-04-18 15:22:23 +0200
committerGitHub <noreply@github.com>2022-04-18 15:22:23 +0200
commitf52031ecdf4520e6d404553691da193356e58691 (patch)
treebdb928a550a77abc9a40180f985e8b0bbb0869cf /cli/tools
parent7919dc902d3664bb8600be884ec775f44fd37389 (diff)
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>
Diffstat (limited to 'cli/tools')
-rw-r--r--cli/tools/test.rs70
1 files changed, 61 insertions, 9 deletions
diff --git a/cli/tools/test.rs b/cli/tools/test.rs
index 96e0dd8c3..aab062a6c 100644
--- a/cli/tools/test.rs
+++ b/cli/tools/test.rs
@@ -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,