Skip to content

Commit

Permalink
feat(linter): eslint-plugin-vitest/no-conditional-expect (#4425)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 committed Jul 23, 2024
1 parent 27fdd69 commit c936782
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 49 deletions.
121 changes: 118 additions & 3 deletions crates/oxc_linter/src/rules/jest/no_conditional_expect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ declare_oxc_lint!(
// await foo().catch(error => expect(error).toBeInstanceOf(error));
// });
/// ```
///
/// This rule is compatible with [eslint-plugin-vitest](https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/no-conditional-expect.md),
/// to use it, add the following configuration to your `.eslintrc.json`:
///
/// ```json
/// {
/// "rules": {
/// "vitest/no-conditional-expect": "error"
/// }
/// }
/// ```
NoConditionalExpect,
correctness
);
Expand Down Expand Up @@ -163,7 +174,7 @@ fn check_parents<'a>(
fn test() {
use crate::tester::Tester;

let pass = vec![
let mut pass = vec![
(
"
it('foo', () => {
Expand Down Expand Up @@ -431,7 +442,7 @@ fn test() {
),
];

let fail = vec![
let mut fail = vec![
(
"
it('foo', () => {
Expand Down Expand Up @@ -886,5 +897,109 @@ fn test() {
),
];

Tester::new(NoConditionalExpect::NAME, pass, fail).with_jest_plugin(true).test_and_snapshot();
let pass_vitest = vec![
"
it('foo', () => {
process.env.FAIL && setNum(1);
expect(num).toBe(2);
});
",
"
function getValue() {
let num = 2;
process.env.FAIL && setNum(1);
return num;
}
it('foo', () => {
expect(getValue()).toBe(2);
});
",
"
function getValue() {
let num = 2;
process.env.FAIL || setNum(1);
return num;
}
it('foo', () => {
expect(getValue()).toBe(2);
});
",
"
it('foo', () => {
const num = process.env.FAIL ? 1 : 2;
expect(num).toBe(2);
});
",
"
function getValue() {
return process.env.FAIL ? 1 : 2
}
it('foo', () => {
expect(getValue()).toBe(2);
});
",
];

let fail_vitest = vec![
"
it('foo', () => {
something && expect(something).toHaveBeenCalled();
})
",
"
it('foo', () => {
a || (b && expect(something).toHaveBeenCalled());
})
",
"
it.each``('foo', () => {
something || expect(something).toHaveBeenCalled();
});
",
"
it.each()('foo', () => {
something || expect(something).toHaveBeenCalled();
})
",
"
function getValue() {
something || expect(something).toHaveBeenCalled();
}
it('foo', getValue);
",
"
it('foo', () => {
something ? expect(something).toHaveBeenCalled() : noop();
})
",
"
function getValue() {
something ? expect(something).toHaveBeenCalled() : noop();
}
it('foo', getValue);
",
"
it('foo', () => {
something ? noop() : expect(something).toHaveBeenCalled();
})
",
];

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

Tester::new(NoConditionalExpect::NAME, pass, fail)
.with_jest_plugin(true)
.with_vitest_plugin(true)
.test_and_snapshot();
}
Loading

0 comments on commit c936782

Please sign in to comment.