Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: support vitest-no-commented-out-tests
  • Loading branch information
eryue0220 committed Jul 23, 2024
commit 27f5c4bbffea1f7813891adb9357714e2e647490
77 changes: 66 additions & 11 deletions crates/oxc_linter/src/rules/jest/no_commented_out_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use regex::Regex;

use crate::{context::LintContext, rule::Rule};
use crate::{
context::LintContext,
rule::Rule,
utils::{get_test_plugin_name, TestPluginName},
};

fn no_commented_out_tests_diagnostic(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented",
)
.with_help("Remove or uncomment this comment")
.with_label(span0)
fn no_commented_out_tests_diagnostic(x0: TestPluginName, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("{x0}(no-commented-out-tests): Some tests seem to be commented"))
.with_help("Remove or uncomment this comment")
.with_label(span1)
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -39,6 +41,17 @@ declare_oxc_lint!(
/// // it.skip('foo', () => {});
/// // test.skip('foo', () => {});
/// ```
///
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-commented-out-tests.md),
/// to use it, add the following configuration to your `.eslintrc.json`:
///
/// ```json
/// {
/// "rules": {
/// "vitest/no-commented-out-tests": "error"
/// }
/// }
/// ```
NoCommentedOutTests,
suspicious
);
Expand All @@ -60,9 +73,10 @@ impl Rule for NoCommentedOutTests {
None
}
});
let plugin_name = get_test_plugin_name(ctx);

for span in commented_tests {
ctx.diagnostic(no_commented_out_tests_diagnostic(span));
ctx.diagnostic(no_commented_out_tests_diagnostic(plugin_name, span));
}
}
}
Expand All @@ -71,7 +85,7 @@ impl Rule for NoCommentedOutTests {
fn test() {
use crate::tester::Tester;

let pass = vec![
let mut pass = vec![
("// foo('bar', function () {})", None),
("describe('foo', function () {})", None),
("it('foo', function () {})", None),
Expand Down Expand Up @@ -122,7 +136,7 @@ fn test() {
),
];

let fail = vec![
let mut fail = vec![
("// fdescribe('foo', function () {})", None),
("// describe['skip']('foo', function () {})", None),
("// describe['skip']('foo', function () {})", None),
Expand Down Expand Up @@ -172,5 +186,46 @@ fn test() {
),
];

Tester::new(NoCommentedOutTests::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
let pass_vitest = vec![
r#"// foo("bar", function () {})"#,
r#"describe("foo", function () {})"#,
r#"it("foo", function () {})"#,
r#"describe.only("foo", function () {})"#,
r#"it.only("foo", function () {})"#,
r#"it.concurrent("foo", function () {})"#,
r#"test("foo", function () {})"#,
r#"test.only("foo", function () {})"#,
r#"test.concurrent("foo", function () {})"#,
r"var appliedSkip = describe.skip; appliedSkip.apply(describe)",
r"var calledSkip = it.skip; calledSkip.call(it)",
r"({ f: function () {} }).f()",
r"(a || b).f()",
r"itHappensToStartWithIt()",
r"testSomething()",
r"// latest(dates)",
r"// TODO: unify with Git implementation from Shipit (?)",
"#!/usr/bin/env node#",
];

let fail_vitest = vec![
r"// describe(\'foo\', function () {})\'",
r#"// test.concurrent("foo", function () {})"#,
r#"// test["skip"]("foo", function () {})"#,
r#"// xdescribe("foo", function () {})"#,
r#"// xit("foo", function () {})"#,
r#"// fit("foo", function () {})"#,
r#"
// test(
// "foo", function () {}
// )
"#,
];

pass.extend(pass_vitest.into_iter().map(|x| (x, None)));
fail.extend(fail_vitest.into_iter().map(|x| (x, None)));

Tester::new(NoCommentedOutTests::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.test_and_snapshot();
}
52 changes: 52 additions & 0 deletions crates/oxc_linter/src/snapshots/no_commented_out_tests.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_linter/src/tester.rs
assertion_line: 216
---
⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
Expand Down Expand Up @@ -164,3 +165,54 @@ source: crates/oxc_linter/src/tester.rs
6 │ bar()
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // describe(\'foo\', function () {})\'
· ────────────────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // test.concurrent("foo", function () {})
· ───────────────────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // test["skip"]("foo", function () {})
· ────────────────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // xdescribe("foo", function () {})
· ─────────────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // xit("foo", function () {})
· ───────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:1:3]
1 │ // fit("foo", function () {})
· ───────────────────────────
╰────
help: Remove or uncomment this comment

⚠ eslint-plugin-jest(no-commented-out-tests): Some tests seem to be commented
╭─[no_commented_out_tests.tsx:2:15]
1 │
2 │ // test(
· ──────
3 │ // "foo", function () {}
╰────
help: Remove or uncomment this comment
1 change: 1 addition & 0 deletions crates/oxc_linter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn is_jest_rule_adapted_to_vitest(rule_name: &str) -> bool {
"consistent-test-it",
"expect-expect",
"no-alias-methods",
"no-commented-out-tests",
"no-disabled-tests",
"no-focused-tests",
"no-test-prefixes",
Expand Down