Skip to content

Commit 4445855

Browse files
committed
feat(linter): split jest/no-conditional-in-test into vitest/no-conditional-in-test (#21763)
1 parent b8604de commit 4445855

10 files changed

Lines changed: 1249 additions & 129 deletions

File tree

crates/oxc_linter/data/vitest_compatible_jest_rules.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[
2-
"no-conditional-in-test",
32
"no-disabled-tests",
43
"no-duplicate-hooks",
54
"no-focused-tests",

crates/oxc_linter/src/generated/rule_runner_impls.rs

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_linter/src/generated/rules_enum.rs

Lines changed: 28 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_linter/src/rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ pub(crate) mod vitest {
732732
pub mod no_alias_methods;
733733
pub mod no_commented_out_tests;
734734
pub mod no_conditional_expect;
735+
pub mod no_conditional_in_test;
735736
pub mod no_conditional_tests;
736737
pub mod no_import_node_test;
737738
pub mod no_importing_vitest_globals;

crates/oxc_linter/src/rules/jest/no_conditional_in_test.rs

Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,16 @@
11
use oxc_ast::AstKind;
2-
use oxc_diagnostics::OxcDiagnostic;
32
use oxc_macros::declare_oxc_lint;
4-
use oxc_span::Span;
53

64
use crate::{
75
context::LintContext,
86
rule::Rule,
9-
utils::{JestFnKind, PossibleJestNode, is_type_of_jest_fn_call},
7+
rules::shared::no_conditional_in_test::{DOCUMENTATION, run},
108
};
119

12-
fn no_conditional_in_test(span: Span) -> OxcDiagnostic {
13-
OxcDiagnostic::warn("Avoid having conditionals in tests.")
14-
.with_help("Replace conditionals with separate test cases for each branch to keep tests deterministic and easy to understand.")
15-
.with_label(span)
16-
}
17-
1810
#[derive(Debug, Default, Clone)]
1911
pub struct NoConditionalInTest;
2012

21-
declare_oxc_lint!(
22-
/// ### What it does
23-
///
24-
/// Disallow conditional statements in tests.
25-
///
26-
/// ### Why is this bad?
27-
///
28-
/// Conditional statements in tests can make the test harder to read and understand. It is better to have a single test case per test function.
29-
///
30-
/// ### Examples
31-
///
32-
/// Examples of **incorrect** code for this rule:
33-
/// ```js
34-
/// it('foo', () => {
35-
/// if (true) {
36-
/// doTheThing();
37-
/// }
38-
/// });
39-
///
40-
/// it('bar', () => {
41-
/// switch (mode) {
42-
/// case 'none':
43-
/// generateNone();
44-
/// case 'single':
45-
/// generateOne();
46-
/// case 'multiple':
47-
/// generateMany();
48-
/// }
49-
///
50-
/// expect(fixtures.length).toBeGreaterThan(-1);
51-
/// });
52-
///
53-
/// it('baz', async () => {
54-
/// const promiseValue = () => {
55-
/// return something instanceof Promise
56-
/// ? something
57-
/// : Promise.resolve(something);
58-
/// };
59-
///
60-
/// await expect(promiseValue()).resolves.toBe(1);
61-
/// });
62-
/// ```
63-
///
64-
/// Examples of **correct** code for this rule:
65-
/// ```js
66-
/// describe('my tests', () => {
67-
/// if (true) {
68-
/// it('foo', () => {
69-
/// doTheThing();
70-
/// });
71-
/// }
72-
/// });
73-
///
74-
/// beforeEach(() => {
75-
/// switch (mode) {
76-
/// case 'none':
77-
/// generateNone();
78-
/// case 'single':
79-
/// generateOne();
80-
/// case 'multiple':
81-
/// generateMany();
82-
/// }
83-
/// });
84-
///
85-
/// it('bar', () => {
86-
/// expect(fixtures.length).toBeGreaterThan(-1);
87-
/// });
88-
///
89-
/// const promiseValue = something => {
90-
/// return something instanceof Promise ? something : Promise.resolve(something);
91-
/// };
92-
///
93-
/// it('baz', async () => {
94-
/// await expect(promiseValue()).resolves.toBe(1);
95-
/// });
96-
/// ```
97-
///
98-
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/no-conditional-in-test.md),
99-
/// to use it, add the following configuration to your `.oxlintrc.json`:
100-
///
101-
/// ```json
102-
/// {
103-
/// "rules": {
104-
/// "vitest/no-conditional-in-test": "error"
105-
/// }
106-
/// }
107-
/// ```
108-
NoConditionalInTest,
109-
jest,
110-
pedantic,
111-
version = "0.8.0",
112-
);
13+
declare_oxc_lint!(NoConditionalInTest, jest, pedantic, docs = DOCUMENTATION, version = "0.8.0",);
11314

11415
impl Rule for NoConditionalInTest {
11516
fn run<'a>(&self, node: &oxc_semantic::AstNode<'a>, ctx: &LintContext<'a>) {
@@ -121,29 +22,7 @@ impl Rule for NoConditionalInTest {
12122
_ => return,
12223
}
12324

124-
let is_if_statement_in_test = ctx.nodes().ancestors(node.id()).any(|node| {
125-
let AstKind::CallExpression(call_expr) = node.kind() else { return false };
126-
let vitest_node = PossibleJestNode { node, original: None };
127-
128-
is_type_of_jest_fn_call(
129-
call_expr,
130-
&vitest_node,
131-
ctx,
132-
&[JestFnKind::General(crate::utils::JestGeneralFnKind::Test)],
133-
)
134-
});
135-
136-
if is_if_statement_in_test {
137-
let span = match node.kind() {
138-
AstKind::IfStatement(stmt) => stmt.span,
139-
AstKind::SwitchStatement(stmt) => stmt.span,
140-
AstKind::ConditionalExpression(expr) => expr.span,
141-
AstKind::LogicalExpression(expr) => expr.span,
142-
_ => unreachable!(),
143-
};
144-
145-
ctx.diagnostic(no_conditional_in_test(span));
146-
}
25+
run(node, ctx);
14726
}
14827
}
14928

@@ -661,6 +540,5 @@ fn test() {
661540

662541
Tester::new(NoConditionalInTest::NAME, NoConditionalInTest::PLUGIN, pass, fail)
663542
.with_jest_plugin(true)
664-
.with_vitest_plugin(true)
665543
.test_and_snapshot();
666544
}

crates/oxc_linter/src/rules/shared/jest_vitest/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pub mod max_nested_describe;
55
pub mod no_alias_methods;
66
pub mod no_commented_out_tests;
77
pub mod no_conditional_expect;
8+
pub mod no_conditional_in_test;
89
pub mod valid_title;

0 commit comments

Comments
 (0)