1use fallow_types::extract::{byte_offset_to_line_col, compute_line_offsets};
6use oxc_ast::ast::{
7 Argument, CallExpression, Expression, ObjectExpression, ObjectPropertyKind, PropertyKey,
8};
9use oxc_ast_visit::{Visit, walk};
10use serde::Serialize;
11
12use super::layout::Layout;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
16#[serde(rename_all = "kebab-case")]
17pub enum SkipKind {
18 Skip,
20 Todo,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize)]
27pub struct SkippedTest {
28 pub file: String,
30 pub line: u32,
32 pub kind: SkipKind,
34 pub name: Option<String>,
36}
37
38const TEST_ROOTS: [&str; 4] = ["it", "test", "describe", "suite"];
40
41const MODIFIERS: [&str; 3] = ["concurrent", "sequential", "shuffle"];
44
45const SKIP_ALIASES: [&str; 3] = ["xit", "xtest", "xdescribe"];
47
48pub(super) fn skipped_tests_in(file: &str, source: &str, layout: Layout) -> Vec<SkippedTest> {
52 let line_offsets = compute_line_offsets(source);
53 let (text, kind) = layout.parse_view(source);
54 let mut tests = fallow_extract::parse_bounded_or_default(&text, kind.source_type(), |parsed| {
55 let mut scan = Scan {
56 file,
57 line_offsets: &line_offsets,
58 tests: Vec::new(),
59 };
60 scan.visit_program(&parsed.program);
61 scan.tests
62 });
63 tests.sort();
64 tests
65}
66
67struct Scan<'a> {
68 file: &'a str,
69 line_offsets: &'a [u32],
70 tests: Vec<SkippedTest>,
71}
72
73impl<'a> Visit<'a> for Scan<'_> {
74 fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
75 if let Some(kind) = skip_kind(call) {
76 let (line, _) = byte_offset_to_line_col(self.line_offsets, call.span.start);
77 self.tests.push(SkippedTest {
78 file: self.file.to_owned(),
79 line,
80 kind,
81 name: test_name(call),
82 });
83 }
84 walk::walk_call_expression(self, call);
85 }
86}
87
88fn skip_kind(call: &CallExpression<'_>) -> Option<SkipKind> {
89 match &call.callee {
90 Expression::Identifier(name) if SKIP_ALIASES.contains(&name.name.as_str()) => {
91 Some(SkipKind::Skip)
92 }
93 Expression::Identifier(name) if TEST_ROOTS.contains(&name.name.as_str()) => {
94 call.arguments.iter().find_map(|argument| match argument {
95 Argument::ObjectExpression(options) => option_kind(options),
96 _ => None,
97 })
98 }
99 callee => member_kind(callee),
100 }
101}
102
103fn member_kind(callee: &Expression<'_>) -> Option<SkipKind> {
105 let mut names: Vec<&str> = Vec::new();
106 let mut current = callee;
107 while let Expression::StaticMemberExpression(member) = current {
108 names.push(member.property.name.as_str());
109 current = &member.object;
110 }
111 let Expression::Identifier(root) = current else {
112 return None;
113 };
114 if !TEST_ROOTS.contains(&root.name.as_str()) {
115 return None;
116 }
117 let (outer, inner) = names.split_first()?;
118 if !inner.iter().all(|name| MODIFIERS.contains(name)) {
119 return None;
120 }
121 match *outer {
122 "skip" => Some(SkipKind::Skip),
123 "todo" => Some(SkipKind::Todo),
124 _ => None,
125 }
126}
127
128#[derive(Clone, Copy, PartialEq, Eq)]
130enum Flag {
131 Unset,
133 On,
135 Off,
137 Unknown,
140}
141
142fn option_kind(options: &ObjectExpression<'_>) -> Option<SkipKind> {
150 let (mut skip, mut todo) = (Flag::Unset, Flag::Unset);
151 for property in &options.properties {
152 let ObjectPropertyKind::ObjectProperty(property) = property else {
153 (skip, todo) = (Flag::Unknown, Flag::Unknown);
154 continue;
155 };
156 if property.computed {
157 (skip, todo) = (Flag::Unknown, Flag::Unknown);
158 continue;
159 }
160 let flag = match key_name(&property.key) {
161 Some("skip") => &mut skip,
162 Some("todo") => &mut todo,
163 _ => continue,
164 };
165 *flag = match &property.value {
166 Expression::BooleanLiteral(value) if value.value => Flag::On,
167 Expression::StringLiteral(value) if !value.value.is_empty() => Flag::On,
168 Expression::BooleanLiteral(_) | Expression::StringLiteral(_) => Flag::Off,
169 _ => Flag::Unknown,
170 };
171 }
172 match (skip, todo) {
173 (Flag::On, _) => Some(SkipKind::Skip),
174 (_, Flag::On) => Some(SkipKind::Todo),
175 _ => None,
176 }
177}
178
179fn key_name<'a>(key: &'a PropertyKey<'_>) -> Option<&'a str> {
180 match key {
181 PropertyKey::StaticIdentifier(name) => Some(name.name.as_str()),
182 PropertyKey::StringLiteral(name) => name.value.as_str(),
183 _ => None,
184 }
185}
186
187fn test_name(call: &CallExpression<'_>) -> Option<String> {
188 match call.arguments.first()? {
189 Argument::StringLiteral(name) => Some(name.value.display().to_string()),
190 Argument::TemplateLiteral(template) if template.expressions.is_empty() => template
191 .quasis
192 .first()
193 .and_then(|part| part.value.cooked)
194 .map(|cooked| cooked.display().to_string()),
195 _ => None,
196 }
197}
198
199#[cfg(test)]
200mod tests {
201 use super::*;
202
203 fn scan(source: &str) -> Vec<(u32, SkipKind, Option<String>)> {
204 let layout = Layout::from_path("src/a.test.ts").expect("a TypeScript path");
205 skipped_tests_in("src/a.test.ts", source, layout)
206 .into_iter()
207 .map(|test| (test.line, test.kind, test.name))
208 .collect()
209 }
210
211 fn named(line: u32, kind: SkipKind, name: &str) -> (u32, SkipKind, Option<String>) {
212 (line, kind, Some(name.to_owned()))
213 }
214
215 #[test]
216 fn each_recognized_form_is_reported_with_its_line_and_name() {
217 let source = "\
218import { describe, it, test } from \"vitest\";
219it.skip(\"a\", () => {});
220test.skip(\"b\", () => {});
221describe.skip(\"c\", () => {});
222it.todo(\"d\");
223test.todo(\"e\");
224describe.todo(\"f\");
225xit(\"g\", () => {});
226xtest(\"h\", () => {});
227xdescribe(\"i\", () => {});
228it.concurrent.skip(\"j\", () => {});
229test(\"k\", { skip: true }, () => {});
230test(\"l\", { todo: \"later\" }, () => {});
231it(\"m\", { skip: \"no network\" }, () => {});
232describe(\"n\", { skip: true }, () => {});
233it.skip(`o`, () => {});
234";
235 assert_eq!(
236 scan(source),
237 vec![
238 named(2, SkipKind::Skip, "a"),
239 named(3, SkipKind::Skip, "b"),
240 named(4, SkipKind::Skip, "c"),
241 named(5, SkipKind::Todo, "d"),
242 named(6, SkipKind::Todo, "e"),
243 named(7, SkipKind::Todo, "f"),
244 named(8, SkipKind::Skip, "g"),
245 named(9, SkipKind::Skip, "h"),
246 named(10, SkipKind::Skip, "i"),
247 named(11, SkipKind::Skip, "j"),
248 named(12, SkipKind::Skip, "k"),
249 named(13, SkipKind::Todo, "l"),
250 named(14, SkipKind::Skip, "m"),
251 named(15, SkipKind::Skip, "n"),
252 named(16, SkipKind::Skip, "o"),
253 ]
254 );
255 }
256
257 #[test]
258 fn a_nested_skip_and_a_skip_with_a_computed_name_are_reported() {
259 let source = "\
260describe(\"suite\", () => {
261 it.skip(name, () => {});
262 describe(\"inner\", () => {
263 it.todo(\"deep\");
264 });
265});
266";
267 assert_eq!(
268 scan(source),
269 vec![(2, SkipKind::Skip, None), named(4, SkipKind::Todo, "deep"),]
270 );
271 }
272
273 #[test]
274 fn normal_tests_conditional_skips_and_other_calls_are_not_reported() {
275 let source = "\
276it(\"normal\", () => {});
277test(\"also normal\", { timeout: 1000 }, () => {});
278test(\"flag off\", { skip: false }, () => {});
279test(\"runtime flag\", { skip: process.env.CI }, () => {});
280it.skipIf(process.env.CI)(\"conditional\", () => {});
281it.only(\"only\", () => {});
282it.each([1])(\"each %s\", () => {});
283other.skip(\"not a test\");
284skip(\"not a test\");
285const skip = true;
286";
287 assert_eq!(scan(source), Vec::new());
288 }
289
290 #[test]
291 fn a_later_literal_overrides_an_earlier_option() {
292 let source = "\
293test(\"off again\", { skip: true, skip: false }, () => {});
294test(\"emptied\", { skip: \"reason\", skip: \"\" }, () => {});
295test(\"todo off\", { todo: true, todo: false }, () => {});
296test(\"on again\", { skip: false, skip: true }, () => {});
297test(\"other key\", { skip: true, todo: false }, () => {});
298";
299 assert_eq!(
300 scan(source),
301 vec![
302 named(4, SkipKind::Skip, "on again"),
303 named(5, SkipKind::Skip, "other key"),
304 ]
305 );
306 }
307
308 #[test]
309 fn a_runtime_value_or_spread_after_the_last_literal_makes_the_result_uncertain() {
310 let source = "\
311test(\"spread after\", { skip: true, ...runtimeOptions }, () => {});
312test(\"todo spread\", { todo: true, ...runtimeOptions }, () => {});
313test(\"runtime value\", { skip: true, skip: flag }, () => {});
314test(\"spread between\", { skip: true, ...runtimeOptions, skip: true }, () => {});
315";
316 assert_eq!(
317 scan(source),
318 vec![named(4, SkipKind::Skip, "spread between")],
319 "only the literal after the spread is certain"
320 );
321 }
322
323 #[test]
324 fn a_spread_before_the_literal_leaves_the_test_skipped() {
325 let source = "\
326test(\"spread before\", { ...base, skip: true }, () => {});
327test(\"spread before todo\", { ...base, todo: \"later\" }, () => {});
328";
329 assert_eq!(
330 scan(source),
331 vec![
332 named(1, SkipKind::Skip, "spread before"),
333 named(2, SkipKind::Todo, "spread before todo"),
334 ]
335 );
336 }
337
338 #[test]
339 fn a_computed_key_after_the_last_literal_makes_the_result_uncertain() {
340 let source = "\
341test(\"computed after\", { skip: true, [key]: false }, () => {});
342test(\"computed before\", { [key]: false, skip: true }, () => {});
343test(\"computed string\", { skip: true, [\"skip\"]: false }, () => {});
344";
345 assert_eq!(
346 scan(source),
347 vec![named(2, SkipKind::Skip, "computed before")]
348 );
349 }
350
351 #[test]
352 fn a_file_that_does_not_parse_has_no_skipped_test() {
353 assert_eq!(scan("it.skip(\"a\", () => {"), Vec::new());
354 }
355}