From c35d3f2ea2db00b40bc426d66ba6a0254a088925 Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Mon, 28 Oct 2024 12:55:24 +0000 Subject: [PATCH] test(linter): improve test failure output (#6975) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improves the test output when a test fails, based on the type of expectation. The output now includes the config that was passed, as well as the source code that failed. Additional formatting makes it a little bit easier to parse. Before: Screenshot 2024-10-27 at 11 54 02 PM   Screenshot 2024-10-27 at 11 54 56 PM --- After: Screenshot 2024-10-27 at 11 49 36 PM   Screenshot 2024-10-27 at 11 58 56 PM --- crates/oxc_linter/src/tester.rs | 35 +++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/oxc_linter/src/tester.rs b/crates/oxc_linter/src/tester.rs index 74ad6a1466ac5..981a07e3647c8 100644 --- a/crates/oxc_linter/src/tester.rs +++ b/crates/oxc_linter/src/tester.rs @@ -356,17 +356,44 @@ impl Tester { fn test_pass(&mut self) { for TestCase { source, rule_config, eslint_config, path } in self.expect_pass.clone() { - let result = self.run(&source, rule_config, &eslint_config, path, ExpectFixKind::None); + let result = + self.run(&source, rule_config.clone(), &eslint_config, path, ExpectFixKind::None); let passed = result == TestResult::Passed; - assert!(passed, "expect test to pass: {source} {}", self.snapshot); + let config = rule_config.map_or_else( + || "\n\n------------------------\n".to_string(), + |v| { + format!( + "\n-------- rule config --------\n{}", + serde_json::to_string_pretty(&v).unwrap() + ) + }, + ); + assert!( + passed, + "expected test to pass, but it failed:\n\n-------- source --------\n\n{source}\n\n-------- error --------\n{}{config}\n", + self.snapshot + ); } } fn test_fail(&mut self) { for TestCase { source, rule_config, eslint_config, path } in self.expect_fail.clone() { - let result = self.run(&source, rule_config, &eslint_config, path, ExpectFixKind::None); + let result = + self.run(&source, rule_config.clone(), &eslint_config, path, ExpectFixKind::None); let failed = result == TestResult::Failed; - assert!(failed, "expect test to fail: {source}"); + let config = rule_config.map_or_else( + || "\n\n------------------------".to_string(), + |v| { + format!( + "\n-------- rule config --------\n{}", + serde_json::to_string_pretty(&v).unwrap() + ) + }, + ); + assert!( + failed, + "expected test to fail, but it passed:\n\n-------- source --------\n\n{source}{config}\n", + ); } }