Skip to main content

react_auditor/rules/testing/
no_skipped_tests.rs

1use oxc_ast::ast::Program;
2use oxc_ast_visit::Visit;
3use oxc_semantic::Semantic;
4
5use crate::rules::{Rule, RuleFinding, RuleMeta, Severity};
6
7pub struct NoSkippedTests;
8
9const RULE_META: RuleMeta = RuleMeta {
10    id: "no-skipped-tests",
11    default_severity: Severity::Warning,
12    category: "testing",
13    description: "No skipped or disabled tests committed",
14};
15
16impl Rule for NoSkippedTests {
17    fn meta(&self) -> &RuleMeta {
18        &RULE_META
19    }
20
21    fn run(&self, program: &Program, _semantic: &Semantic, source_text: &str) -> Vec<RuleFinding> {
22        let mut collector = SkippedTestCollector {
23            findings: Vec::new(),
24            source: source_text,
25        };
26        collector.visit_program(program);
27        collector.findings
28    }
29}
30
31struct SkippedTestCollector<'a> {
32    findings: Vec<RuleFinding>,
33    source: &'a str,
34}
35
36impl<'a> SkippedTestCollector<'a> {
37    fn push_finding(&mut self, start: usize, msg: String) {
38        let line = self.source[..start].lines().count().max(1);
39        let col = start - self.source[..start].rfind('\n').map(|i| i + 1).unwrap_or(0);
40        self.findings.push(RuleFinding {
41            line,
42            column: col + 1,
43            message: msg,
44        });
45    }
46}
47
48impl<'a> Visit<'a> for SkippedTestCollector<'a> {
49    fn visit_call_expression(&mut self, expr: &oxc_ast::ast::CallExpression<'a>) {
50        let callee_str = match &expr.callee {
51            oxc_ast::ast::Expression::Identifier(ident) => ident.name.as_str().to_string(),
52            oxc_ast::ast::Expression::StaticMemberExpression(member) => {
53                if let oxc_ast::ast::Expression::Identifier(obj) = &member.object {
54                    format!("{}.{}", obj.name.as_str(), member.property.name.as_str())
55                } else {
56                    return;
57                }
58            }
59            _ => return,
60        };
61
62        if callee_str == "it.skip"
63            || callee_str == "describe.skip"
64            || callee_str == "xit"
65            || callee_str == "xdescribe"
66        {
67            self.push_finding(
68                expr.span.start as usize,
69                format!("Skipped test: `{callee_str}` — remove or implement before committing"),
70            );
71        }
72    }
73}