diff --git a/crates/oxc_linter/data/vitest_compatible_jest_rules.json b/crates/oxc_linter/data/vitest_compatible_jest_rules.json index 4d6c5ab4d31fd..d88d35eed5ca6 100644 --- a/crates/oxc_linter/data/vitest_compatible_jest_rules.json +++ b/crates/oxc_linter/data/vitest_compatible_jest_rules.json @@ -30,6 +30,7 @@ "prefer-hooks-on-top", "prefer-lowercase-title", "prefer-mock-promise-shorthand", + "prefer-mock-return-shorthand", "prefer-spy-on", "prefer-strict-equal", "prefer-to-be", diff --git a/crates/oxc_linter/src/generated/rule_runner_impls.rs b/crates/oxc_linter/src/generated/rule_runner_impls.rs index bd5fdf6182652..2d7cbd86ead51 100644 --- a/crates/oxc_linter/src/generated/rule_runner_impls.rs +++ b/crates/oxc_linter/src/generated/rule_runner_impls.rs @@ -2254,6 +2254,12 @@ impl RuleRunner for crate::rules::jest::prefer_mock_promise_shorthand::PreferMoc const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; } +impl RuleRunner for crate::rules::jest::prefer_mock_return_shorthand::PreferMockReturnShorthand { + const NODE_TYPES: Option<&AstTypesBitset> = + Some(&AstTypesBitset::from_types(&[AstType::CallExpression])); + const RUN_FUNCTIONS: RuleRunFunctionsImplemented = RuleRunFunctionsImplemented::Run; +} + impl RuleRunner for crate::rules::jest::prefer_spy_on::PreferSpyOn { const NODE_TYPES: Option<&AstTypesBitset> = Some(&AstTypesBitset::from_types(&[AstType::AssignmentExpression])); diff --git a/crates/oxc_linter/src/generated/rules_enum.rs b/crates/oxc_linter/src/generated/rules_enum.rs index 433cbd09b855c..6958cb9ae5e4d 100644 --- a/crates/oxc_linter/src/generated/rules_enum.rs +++ b/crates/oxc_linter/src/generated/rules_enum.rs @@ -249,6 +249,7 @@ pub use crate::rules::jest::prefer_hooks_on_top::PreferHooksOnTop as JestPreferH pub use crate::rules::jest::prefer_jest_mocked::PreferJestMocked as JestPreferJestMocked; pub use crate::rules::jest::prefer_lowercase_title::PreferLowercaseTitle as JestPreferLowercaseTitle; pub use crate::rules::jest::prefer_mock_promise_shorthand::PreferMockPromiseShorthand as JestPreferMockPromiseShorthand; +pub use crate::rules::jest::prefer_mock_return_shorthand::PreferMockReturnShorthand as JestPreferMockReturnShorthand; pub use crate::rules::jest::prefer_spy_on::PreferSpyOn as JestPreferSpyOn; pub use crate::rules::jest::prefer_strict_equal::PreferStrictEqual as JestPreferStrictEqual; pub use crate::rules::jest::prefer_to_be::PreferToBe as JestPreferToBe; @@ -1055,6 +1056,7 @@ pub enum RuleEnum { JestPreferJestMocked(JestPreferJestMocked), JestPreferLowercaseTitle(JestPreferLowercaseTitle), JestPreferMockPromiseShorthand(JestPreferMockPromiseShorthand), + JestPreferMockReturnShorthand(JestPreferMockReturnShorthand), JestPreferSpyOn(JestPreferSpyOn), JestPreferStrictEqual(JestPreferStrictEqual), JestPreferToBe(JestPreferToBe), @@ -1792,7 +1794,8 @@ const JEST_PREFER_HOOKS_ON_TOP_ID: usize = JEST_PREFER_HOOKS_IN_ORDER_ID + 1usiz const JEST_PREFER_JEST_MOCKED_ID: usize = JEST_PREFER_HOOKS_ON_TOP_ID + 1usize; const JEST_PREFER_LOWERCASE_TITLE_ID: usize = JEST_PREFER_JEST_MOCKED_ID + 1usize; const JEST_PREFER_MOCK_PROMISE_SHORTHAND_ID: usize = JEST_PREFER_LOWERCASE_TITLE_ID + 1usize; -const JEST_PREFER_SPY_ON_ID: usize = JEST_PREFER_MOCK_PROMISE_SHORTHAND_ID + 1usize; +const JEST_PREFER_MOCK_RETURN_SHORTHAND_ID: usize = JEST_PREFER_MOCK_PROMISE_SHORTHAND_ID + 1usize; +const JEST_PREFER_SPY_ON_ID: usize = JEST_PREFER_MOCK_RETURN_SHORTHAND_ID + 1usize; const JEST_PREFER_STRICT_EQUAL_ID: usize = JEST_PREFER_SPY_ON_ID + 1usize; const JEST_PREFER_TO_BE_ID: usize = JEST_PREFER_STRICT_EQUAL_ID + 1usize; const JEST_PREFER_TO_CONTAIN_ID: usize = JEST_PREFER_TO_BE_ID + 1usize; @@ -2589,6 +2592,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => JEST_PREFER_JEST_MOCKED_ID, Self::JestPreferLowercaseTitle(_) => JEST_PREFER_LOWERCASE_TITLE_ID, Self::JestPreferMockPromiseShorthand(_) => JEST_PREFER_MOCK_PROMISE_SHORTHAND_ID, + Self::JestPreferMockReturnShorthand(_) => JEST_PREFER_MOCK_RETURN_SHORTHAND_ID, Self::JestPreferSpyOn(_) => JEST_PREFER_SPY_ON_ID, Self::JestPreferStrictEqual(_) => JEST_PREFER_STRICT_EQUAL_ID, Self::JestPreferToBe(_) => JEST_PREFER_TO_BE_ID, @@ -3384,6 +3388,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => JestPreferJestMocked::NAME, Self::JestPreferLowercaseTitle(_) => JestPreferLowercaseTitle::NAME, Self::JestPreferMockPromiseShorthand(_) => JestPreferMockPromiseShorthand::NAME, + Self::JestPreferMockReturnShorthand(_) => JestPreferMockReturnShorthand::NAME, Self::JestPreferSpyOn(_) => JestPreferSpyOn::NAME, Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::NAME, Self::JestPreferToBe(_) => JestPreferToBe::NAME, @@ -4195,6 +4200,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => JestPreferJestMocked::CATEGORY, Self::JestPreferLowercaseTitle(_) => JestPreferLowercaseTitle::CATEGORY, Self::JestPreferMockPromiseShorthand(_) => JestPreferMockPromiseShorthand::CATEGORY, + Self::JestPreferMockReturnShorthand(_) => JestPreferMockReturnShorthand::CATEGORY, Self::JestPreferSpyOn(_) => JestPreferSpyOn::CATEGORY, Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::CATEGORY, Self::JestPreferToBe(_) => JestPreferToBe::CATEGORY, @@ -5005,6 +5011,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => JestPreferJestMocked::FIX, Self::JestPreferLowercaseTitle(_) => JestPreferLowercaseTitle::FIX, Self::JestPreferMockPromiseShorthand(_) => JestPreferMockPromiseShorthand::FIX, + Self::JestPreferMockReturnShorthand(_) => JestPreferMockReturnShorthand::FIX, Self::JestPreferSpyOn(_) => JestPreferSpyOn::FIX, Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::FIX, Self::JestPreferToBe(_) => JestPreferToBe::FIX, @@ -5885,6 +5892,9 @@ impl RuleEnum { Self::JestPreferMockPromiseShorthand(_) => { JestPreferMockPromiseShorthand::documentation() } + Self::JestPreferMockReturnShorthand(_) => { + JestPreferMockReturnShorthand::documentation() + } Self::JestPreferSpyOn(_) => JestPreferSpyOn::documentation(), Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::documentation(), Self::JestPreferToBe(_) => JestPreferToBe::documentation(), @@ -7349,6 +7359,10 @@ impl RuleEnum { JestPreferMockPromiseShorthand::config_schema(generator) .or_else(|| JestPreferMockPromiseShorthand::schema(generator)) } + Self::JestPreferMockReturnShorthand(_) => { + JestPreferMockReturnShorthand::config_schema(generator) + .or_else(|| JestPreferMockReturnShorthand::schema(generator)) + } Self::JestPreferSpyOn(_) => JestPreferSpyOn::config_schema(generator) .or_else(|| JestPreferSpyOn::schema(generator)), Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::config_schema(generator) @@ -8673,6 +8687,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => "jest", Self::JestPreferLowercaseTitle(_) => "jest", Self::JestPreferMockPromiseShorthand(_) => "jest", + Self::JestPreferMockReturnShorthand(_) => "jest", Self::JestPreferSpyOn(_) => "jest", Self::JestPreferStrictEqual(_) => "jest", Self::JestPreferToBe(_) => "jest", @@ -10145,6 +10160,9 @@ impl RuleEnum { Self::JestPreferMockPromiseShorthand(_) => Ok(Self::JestPreferMockPromiseShorthand( JestPreferMockPromiseShorthand::from_configuration(value)?, )), + Self::JestPreferMockReturnShorthand(_) => Ok(Self::JestPreferMockReturnShorthand( + JestPreferMockReturnShorthand::from_configuration(value)?, + )), Self::JestPreferSpyOn(_) => { Ok(Self::JestPreferSpyOn(JestPreferSpyOn::from_configuration(value)?)) } @@ -11584,6 +11602,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.to_configuration(), Self::JestPreferLowercaseTitle(rule) => rule.to_configuration(), Self::JestPreferMockPromiseShorthand(rule) => rule.to_configuration(), + Self::JestPreferMockReturnShorthand(rule) => rule.to_configuration(), Self::JestPreferSpyOn(rule) => rule.to_configuration(), Self::JestPreferStrictEqual(rule) => rule.to_configuration(), Self::JestPreferToBe(rule) => rule.to_configuration(), @@ -12279,6 +12298,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.run(node, ctx), Self::JestPreferLowercaseTitle(rule) => rule.run(node, ctx), Self::JestPreferMockPromiseShorthand(rule) => rule.run(node, ctx), + Self::JestPreferMockReturnShorthand(rule) => rule.run(node, ctx), Self::JestPreferSpyOn(rule) => rule.run(node, ctx), Self::JestPreferStrictEqual(rule) => rule.run(node, ctx), Self::JestPreferToBe(rule) => rule.run(node, ctx), @@ -12972,6 +12992,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.run_once(ctx), Self::JestPreferLowercaseTitle(rule) => rule.run_once(ctx), Self::JestPreferMockPromiseShorthand(rule) => rule.run_once(ctx), + Self::JestPreferMockReturnShorthand(rule) => rule.run_once(ctx), Self::JestPreferSpyOn(rule) => rule.run_once(ctx), Self::JestPreferStrictEqual(rule) => rule.run_once(ctx), Self::JestPreferToBe(rule) => rule.run_once(ctx), @@ -13731,6 +13752,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.run_on_jest_node(jest_node, ctx), Self::JestPreferLowercaseTitle(rule) => rule.run_on_jest_node(jest_node, ctx), Self::JestPreferMockPromiseShorthand(rule) => rule.run_on_jest_node(jest_node, ctx), + Self::JestPreferMockReturnShorthand(rule) => rule.run_on_jest_node(jest_node, ctx), Self::JestPreferSpyOn(rule) => rule.run_on_jest_node(jest_node, ctx), Self::JestPreferStrictEqual(rule) => rule.run_on_jest_node(jest_node, ctx), Self::JestPreferToBe(rule) => rule.run_on_jest_node(jest_node, ctx), @@ -14456,6 +14478,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.should_run(ctx), Self::JestPreferLowercaseTitle(rule) => rule.should_run(ctx), Self::JestPreferMockPromiseShorthand(rule) => rule.should_run(ctx), + Self::JestPreferMockReturnShorthand(rule) => rule.should_run(ctx), Self::JestPreferSpyOn(rule) => rule.should_run(ctx), Self::JestPreferStrictEqual(rule) => rule.should_run(ctx), Self::JestPreferToBe(rule) => rule.should_run(ctx), @@ -15303,6 +15326,9 @@ impl RuleEnum { Self::JestPreferMockPromiseShorthand(_) => { JestPreferMockPromiseShorthand::IS_TSGOLINT_RULE } + Self::JestPreferMockReturnShorthand(_) => { + JestPreferMockReturnShorthand::IS_TSGOLINT_RULE + } Self::JestPreferSpyOn(_) => JestPreferSpyOn::IS_TSGOLINT_RULE, Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::IS_TSGOLINT_RULE, Self::JestPreferToBe(_) => JestPreferToBe::IS_TSGOLINT_RULE, @@ -16231,6 +16257,7 @@ impl RuleEnum { Self::JestPreferJestMocked(_) => JestPreferJestMocked::HAS_CONFIG, Self::JestPreferLowercaseTitle(_) => JestPreferLowercaseTitle::HAS_CONFIG, Self::JestPreferMockPromiseShorthand(_) => JestPreferMockPromiseShorthand::HAS_CONFIG, + Self::JestPreferMockReturnShorthand(_) => JestPreferMockReturnShorthand::HAS_CONFIG, Self::JestPreferSpyOn(_) => JestPreferSpyOn::HAS_CONFIG, Self::JestPreferStrictEqual(_) => JestPreferStrictEqual::HAS_CONFIG, Self::JestPreferToBe(_) => JestPreferToBe::HAS_CONFIG, @@ -16990,6 +17017,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.types_info(), Self::JestPreferLowercaseTitle(rule) => rule.types_info(), Self::JestPreferMockPromiseShorthand(rule) => rule.types_info(), + Self::JestPreferMockReturnShorthand(rule) => rule.types_info(), Self::JestPreferSpyOn(rule) => rule.types_info(), Self::JestPreferStrictEqual(rule) => rule.types_info(), Self::JestPreferToBe(rule) => rule.types_info(), @@ -17683,6 +17711,7 @@ impl RuleEnum { Self::JestPreferJestMocked(rule) => rule.run_info(), Self::JestPreferLowercaseTitle(rule) => rule.run_info(), Self::JestPreferMockPromiseShorthand(rule) => rule.run_info(), + Self::JestPreferMockReturnShorthand(rule) => rule.run_info(), Self::JestPreferSpyOn(rule) => rule.run_info(), Self::JestPreferStrictEqual(rule) => rule.run_info(), Self::JestPreferToBe(rule) => rule.run_info(), @@ -18460,6 +18489,7 @@ pub static RULES: std::sync::LazyLock> = std::sync::LazyLock::new( RuleEnum::JestPreferJestMocked(JestPreferJestMocked::default()), RuleEnum::JestPreferLowercaseTitle(JestPreferLowercaseTitle::default()), RuleEnum::JestPreferMockPromiseShorthand(JestPreferMockPromiseShorthand::default()), + RuleEnum::JestPreferMockReturnShorthand(JestPreferMockReturnShorthand::default()), RuleEnum::JestPreferSpyOn(JestPreferSpyOn::default()), RuleEnum::JestPreferStrictEqual(JestPreferStrictEqual::default()), RuleEnum::JestPreferToBe(JestPreferToBe::default()), diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 18f8c1ea65e42..3c212232156d4 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -361,6 +361,7 @@ pub(crate) mod jest { pub mod prefer_jest_mocked; pub mod prefer_lowercase_title; pub mod prefer_mock_promise_shorthand; + pub mod prefer_mock_return_shorthand; pub mod prefer_spy_on; pub mod prefer_strict_equal; pub mod prefer_to_be; diff --git a/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs b/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs new file mode 100644 index 0000000000000..fec0971368dda --- /dev/null +++ b/crates/oxc_linter/src/rules/jest/prefer_mock_return_shorthand.rs @@ -0,0 +1,1446 @@ +use oxc_ast::{ + AstKind, + ast::{Argument, Expression, IdentifierReference, Statement, VariableDeclarationKind}, +}; +use oxc_ast_visit::Visit; +use oxc_diagnostics::OxcDiagnostic; +use oxc_macros::declare_oxc_lint; +use oxc_semantic::{ReferenceId, SymbolId}; +use oxc_span::{GetSpan, Span}; +use rustc_hash::FxHashSet; + +use crate::{AstNode, context::LintContext, rule::Rule}; + +fn prefer_mock_return_shorthand_diagnostic( + span: Span, + current_property: &str, + replacement: &str, +) -> OxcDiagnostic { + let help = format!("Replace `{current_property}` with `{replacement}`."); + + OxcDiagnostic::warn("Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`.") + .with_help(help) + .with_label(span) +} + +#[derive(Debug, Default, Clone)] +pub struct PreferMockReturnShorthand; + +declare_oxc_lint!( + /// ### What it does + /// + /// When working with mocks of functions that return simple values, Jest provides some API sugar functions to reduce the amount of boilerplate you have to write. + /// + /// ### Why is this bad? + /// + /// Not using Jest’s API sugar functions adds unnecessary boilerplate and makes tests harder to read. These helpers clearly express intent + /// and reduce errors, keeping tests simple and maintainable. + /// + /// ### Examples + /// + /// Examples of **incorrect** code for this rule: + /// ```js + /// jest.fn().mockImplementation(() => 'hello world'); + /// + /// jest + /// .spyOn(fs.promises, 'readFile') + /// .mockImplementationOnce(() => Promise.reject(new Error('oh noes!'))); + /// + /// myFunction + /// .mockImplementationOnce(() => 42) + /// .mockImplementationOnce(() => Promise.resolve(42)) + /// .mockReturnValue(0); + /// ``` + /// + /// Examples of **correct** code for this rule: + /// ```js + /// jest.fn().mockResolvedValue(123); + /// + /// jest + /// .spyOn(fs.promises, 'readFile') + /// .mockReturnValue(Promise.reject(new Error('oh noes!'))); + /// jest.spyOn(fs.promises, 'readFile').mockRejectedValue(new Error('oh noes!')); + /// + /// jest.spyOn(fs, 'readFileSync').mockImplementationOnce(() => { + /// throw new Error('oh noes!'); + /// }); + /// + /// myFunction + /// .mockResolvedValueOnce(42) + /// .mockResolvedValueOnce(42) + /// .mockReturnValue(0); + /// ``` + /// + /// This rule is compatible with [eslint-plugin-vitest](https://github.com/vitest-dev/eslint-plugin-vitest/blob/main/docs/rules/prefer-mock-return-shorthand.md), + /// to use it, add the following configuration to your `.oxlintrc.json`: + /// + /// ```json + /// { + /// "rules": { + /// "vitest/prefer-mock-return-shorthand": "error" + /// } + /// } + /// ``` + PreferMockReturnShorthand, + jest, + style, + fix, +); + +impl Rule for PreferMockReturnShorthand { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + let AstKind::CallExpression(call_expr) = node.kind() else { + return; + }; + + let Some(mem_expr) = call_expr.callee.as_member_expression() else { + return; + }; + + if call_expr.arguments.is_empty() { + return; + } + + let Some((property_span, property_name)) = mem_expr.static_property_info() else { + return; + }; + + let Some(expr) = call_expr.arguments.first().and_then(Argument::as_expression) else { + return; + }; + + let is_once = property_name.ends_with("Once"); + + if !property_name.eq("mockImplementation") && !property_name.eq("mockImplementationOnce") { + return; + } + + let Some(return_expression) = get_mock_return(expr) else { + return; + }; + + if let Expression::UpdateExpression(_) = return_expression { + return; + } + + let mut visitor = IdentifierCollectorVisitor::new(); + + visitor.visit_expression(return_expression); + + for reference_id in visitor.references { + if let Some(symbol_id) = ctx.scoping().get_reference(reference_id).symbol_id() + && Self::is_mutable(symbol_id, ctx) + { + return; + } + } + + let new_property_name = if is_once { "mockReturnValueOnce" } else { "mockReturnValue" }; + ctx.diagnostic_with_fix( + prefer_mock_return_shorthand_diagnostic( + property_span, + property_name, + new_property_name, + ), + |fixer| { + let return_text = { + let source = ctx.source_range(GetSpan::span(return_expression)); + + source + .strip_prefix("(") + .and_then(|source| source.strip_suffix(")")) + .map_or(source.to_owned(), std::borrow::ToOwned::to_owned) + }; + let argument_span = GetSpan::span(expr); + + let mut multifixer = fixer.for_multifix().new_fix_with_capacity(2); + + multifixer.push(fixer.replace(property_span, new_property_name)); + multifixer.push(fixer.replace(argument_span, return_text)); + + multifixer.with_message("Replaced successfully") + }, + ); + } +} + +impl PreferMockReturnShorthand { + fn is_mutable(symbol_id: SymbolId, ctx: &LintContext<'_>) -> bool { + let scoping = ctx.scoping(); + + if scoping.symbol_is_mutated(symbol_id) { + return true; + } + + let decl_node_id = scoping.symbol_declaration(symbol_id); + if let AstKind::VariableDeclarator(_) = ctx.nodes().kind(decl_node_id) + && let AstKind::VariableDeclaration(parent) = ctx.nodes().parent_kind(decl_node_id) + { + return parent.kind != VariableDeclarationKind::Const; + } + + false + } +} + +fn get_mock_return<'a>(argument_expression: &'a Expression<'a>) -> Option<&'a Expression<'a>> { + match argument_expression { + Expression::ArrowFunctionExpression(arrow_func) => { + if arrow_func.r#async + || arrow_func.body.statements.len() > 1 + || !arrow_func.params.is_empty() + { + return None; + } + + let stmt = arrow_func.body.statements.first()?; + + match stmt { + Statement::ExpressionStatement(stmt_expr) => Some(&stmt_expr.expression), + Statement::ReturnStatement(return_statement) => { + let Some(arg_expr) = &return_statement.argument else { + return None; + }; + + Some(arg_expr) + } + _ => None, + } + } + Expression::FunctionExpression(function) => { + if function.r#async || !function.params.is_empty() { + return None; + } + + let Some(body) = &function.body else { + return None; + }; + + if body.statements.len() > 1 { + return None; + } + + let stmt = body.statements.first()?; + + match stmt { + Statement::ExpressionStatement(stmt_expr) => Some(&stmt_expr.expression), + Statement::ReturnStatement(return_statement) => { + let Some(arg_expr) = &return_statement.argument else { + return None; + }; + + Some(arg_expr) + } + _ => None, + } + } + _ => None, + } +} + +struct IdentifierCollectorVisitor { + references: FxHashSet, +} + +impl IdentifierCollectorVisitor { + fn new() -> Self { + Self { references: FxHashSet::default() } + } +} + +impl<'a> Visit<'a> for IdentifierCollectorVisitor { + fn visit_identifier_reference(&mut self, ident: &IdentifierReference<'a>) { + self.references.insert(ident.reference_id()); + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let mut pass = vec![ + "describe()", + "it()", + "describe.skip()", + "it.skip()", + "test()", + "test.skip()", + "var appliedOnly = describe.only; appliedOnly.apply(describe)", + "var calledOnly = it.only; calledOnly.call(it)", + "it.each()()", + "it.each`table`()", + "test.each()()", + "test.each`table`()", + "test.concurrent()", + "jest.fn().mockReturnValue(42)", + "jest.fn(() => Promise.resolve(42))", + "jest.fn(() => 42)", + "jest.fn(() => ({}))", + "aVariable.mockImplementation", + "aVariable.mockImplementation()", + "jest.fn().mockImplementation(async () => 1);", + "jest.fn().mockImplementation(async function () {});", + "jest.fn().mockImplementation(async function () { + return 42; + });", + "aVariable.mockImplementation(() => { + if (true) { + return 1; + } + return 2; + });", + "aVariable.mockImplementation(() => value++)", + "aVariable.mockImplementationOnce(() => --value)", + "const aValue = 0; + aVariable.mockImplementation(() => { + return aValue++; + });", + "aVariable.mockImplementation(() => { + aValue += 1; + return aValue; + });", + "aVariable.mockImplementation(() => { + aValue++; + return aValue; + });", + "aVariable.mockReturnValue()", + "aVariable.mockReturnValue(1)", + r#"aVariable.mockReturnValue("hello world")"#, + "jest.spyOn(Thingy, 'method').mockImplementation(param => param * 2);", + "jest.spyOn(Thingy, 'method').mockImplementation(param => true ? param : 0);", + "aVariable.mockImplementation(() => { + const value = new Date(); + return Promise.resolve(value); + });", + "aVariable.mockImplementation(() => { + throw new Error('oh noes!'); + });", + "aVariable.mockImplementation(() => { /* do something */ });", + "aVariable.mockImplementation(() => { + const x = 1; + console.log(x + 2); + });", + "aVariable.mockReturnValue(Promise.all([1, 2, 3]));", + "let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + // stuff happens + currentX++; + // more stuff happens", + "let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "let currentX = 0; + currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "var currentX = 0; + currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "var currentX = 0; + var currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "let doSomething = () => {}; + jest.spyOn(X, getCount).mockImplementation(() => doSomething);", + "let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => { + currentX += 1; + return currentX; + });", + "const currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => { + console.log('returning', currentX); + return currentX; + });", + "let value = 1; + jest.fn().mockImplementation(() => ({ value }));", + "let value = 1; + aVariable.mockImplementation(() => [value]);", + "var value = 1; + aVariable.mockImplementation(() => [0, value, 2]);", + "let value = 1; + aVariable.mockImplementation(() => value + 1);", + "let value = 1; + aVariable.mockImplementation(() => 1 - value);", + "var value = 1; + aVariable.mockImplementation(() => { + return { value: value + 1 }; + });", + "var value = 1; + aVariable.mockImplementation(() => value * value + 1); + aVariable.mockImplementation(() => 1 + value / 2); + aVariable.mockImplementation(() => (1 + value) / 2); + aVariable.mockImplementation(() => { + return { value: value + 1 }; + });", + "let value = 1; + aVariable.mockImplementation(function () { + return { items: [value] }; + });", + "let value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: { value }, + } + });", + "let value = 1; + jest.fn().mockImplementationOnce(() => { + return [{ + type: 'object', + with: [1, 2, value], + }] + });", + "let value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, value]} + ]; + });", + "let value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 3]}, + {type: 'object', with: [1, value]} + ]; + });", + "let value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: { + inner: { + value, + }, + }, + } + });", + "let value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + } + });", + "let value = 1; + aVariable.mockImplementation(() => { + return [{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }] + });", + "let value = 1; + aVariable.mockImplementation(() => value & 1); + aVariable.mockImplementation(() => value | 1); + aVariable.mockImplementation(() => 1 & value); + aVariable.mockImplementation(() => 1 | value);", + "let value = 1; + aVariable.mockImplementation(() => !value); + aVariable.mockImplementation(() => ~value); + aVariable.mockImplementation(() => typeof value);", + "const mx = 1 + let my = 2; + aVariable.mockImplementation(() => mx & my); + aVariable.mockImplementation(() => my | mx);", + "let value = 1; + aVariable.mockImplementation(() => value || 0); + aVariable.mockImplementation(() => 1 && value); + aVariable.mockImplementation(() => 1 ?? value); + aVariable.mockImplementation(() => 1 ?? (value && 0));", + "const mx = 1 + let my = 2; + aVariable.mockImplementation(() => mx || my); + aVariable.mockImplementation(() => my && mx); + aVariable.mockImplementation(() => my ?? mx); + aVariable.mockImplementation(() => mx ?? (7 && my));", + "let value = [1]; + aVariable.mockImplementation(() => { + return [{ + type: 'object', + with: { + inner: { + items: [1, 2, ...value], + }, + }, + }] + });", + "let value = 1; + aVariable.mockImplementation(() => { + return [{ + type: 'object', + with: { + inner: { + items: [1, 2, ...[value]], + }, + }, + }] + });", + "let obj = {}; + aVariable.mockImplementation(() => { + return { + type: 'object', + ...obj, + } + });", + "let value = 1; + aVariable.mockImplementation(function () { + function mx() { + return value; + } + return mx(); + });", + "let value = 1; + jest.fn().mockImplementation(() => new Mx(value)); + jest.fn().mockImplementation(() => new Mx(() => value)); + jest.fn().mockImplementation(() => new Mx(() => { return value }));", + "let value = 1; + jest.fn().mockImplementation(() => mx(value)); + jest.fn().mockImplementation(() => mx(value)); + jest.fn().mockImplementation(() => mx?.(value)); + jest.fn().mockImplementation(() => mx(value).my()); + jest.fn().mockImplementation(() => mx(value).my); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx?.my(value)); + jest.fn().mockImplementation(() => mx?.my?.(value)); + jest.fn().mockImplementation(() => mx.my?.(value)); + jest.fn().mockImplementation(() => mx().my(value)); + jest.fn().mockImplementation(() => mx()?.my(value)); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx(value).my(value)); + jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + jest.fn().mockImplementation(() => new Mx().add(value)); + jest.fn().mockImplementation(() => { + return mx([{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }]) + });", + "let propName = 'world'; + aVariable.mockImplementation(() => mx[propName]()); + aVariable.mockImplementation(() => mx[propName]); + aVariable.mockImplementation(() => ({ [propName]: 1 }));", + "const x = true; + let value = 1; + aVariable.mockImplementation(() => value ? true : false); + aVariable.mockImplementation(() => x ? value : false); + aVariable.mockImplementation(() => x ? true : value); + aVariable.mockImplementation(() => true ? true : value); + aVariable.mockImplementation(() => true ? true : value ? true : false); + aVariable.mockImplementation(() => true ? true : true ? value : false); + aVariable.mockImplementation(() => true ? true : true ? false : value); + aVariable.mockImplementation(function() { + if (x) { + return value; + } else { + return 0; + } + });", + ]; + + let mut fail = vec![ + r#"jest.fn().mockImplementation(() => "hello sunshine")"#, + "jest.fn().mockImplementation(() => ({}))", + "jest.fn().mockImplementation(() => x)", + "jest.fn().mockImplementation(() => true ? x : y)", + r#"jest.fn().mockImplementation(function () { + return "hello world"; + })"#, + r#"jest.fn().mockImplementation(() => "hello world")"#, + r#"jest.fn().mockImplementation(() => { + return "hello world"; + })"#, + r#"aVariable.mockImplementation(() => "hello world")"#, + r#"aVariable.mockImplementation(() => { + return "hello world"; + })"#, + r#"jest.fn().mockImplementationOnce(() => "hello world")"#, + r#"aVariable.mockImplementationOnce(() => "hello world")"#, + "aVariable.mockImplementation(() => ({ + target: 'world', + message: 'hello' + }))", + r#"aVariable + .mockImplementation(() => 42) + .mockImplementation(async () => 42) + .mockImplementation(() => Promise.resolve(42)) + .mockReturnValue("hello world")"#, + r#"aVariable + .mockImplementationOnce(() => Promise.reject(42)) + .mockImplementation(() => "hello sunshine") + .mockReturnValueOnce(Promise.reject(42))"#, + "jest.fn().mockImplementation(() => [], xyz)", + r#"jest.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, + "aVariable.mockImplementation(() => { + return Promise.resolve(value) + .then(value => value + 1); + });", + "aVariable.mockImplementation(() => { + return Promise.all([1, 2, 3]); + });", + "const currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "import { currentX } from './elsewhere'; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "const currentX = 0; + describe('some tests', () => { + it('works', () => { + jest.spyOn(X, getCount).mockImplementation(() => currentX); + }); + });", + "function doSomething() {}; + jest.spyOn(X, getCount).mockImplementation(() => doSomething);", + "const doSomething = () => {}; + jest.spyOn(X, getCount).mockImplementation(() => doSomething);", + "const value = 1; + aVariable.mockImplementation(() => [value]);", + "const value = 1; + aVariable.mockImplementation(() => [0, value, 2]);", + "const value = 1; + aVariable.mockImplementation(() => [0,, value, 2]);", + "const value = 1; + jest.fn().mockImplementation(() => ({ value }));", + "const value = 1; + aVariable.mockImplementation(() => ({ items: [value] }));", + "const value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: { value }, + } + });", + "const vX = 1; + let vY = 1; + getPoint.mockImplementation(() => vX + vY); + getPoint.mockImplementation(() => { + return { x: vX, y: 1 } + });", + "const value = 1; + aVariable.mockImplementation(() => value & 0); + aVariable.mockImplementation(() => 0 & value); + aVariable.mockImplementation(() => value | 1); + aVariable.mockImplementation(() => 1 | value);", + "const value = 1; + aVariable.mockImplementation(() => ~value); + aVariable.mockImplementation(() => !value);", + "const value = 1; + aVariable.mockImplementation(() => value + 1); + aVariable.mockImplementation(() => 1 + value); + aVariable.mockImplementation(() => value * value + 1); + aVariable.mockImplementation(() => 1 + value / 2); + aVariable.mockImplementation(() => (1 + value) / 2);", + "const value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: [1, 2, value], + } + });", + "const obj = {}; + aVariable.mockImplementation(() => { + return { + type: 'object', + ...obj, + } + });", + "const value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, value]} + ]; + });", + "const value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, 0 + value]} + ]; + });", + "const value = 1; + aVariable.mockImplementationOnce(() => { + return { + type: 'object', + with: { + inner: { + value, + }, + }, + } + });", + "const value = 1; + aVariable.mockImplementationOnce(() => { + return { + type: 'object', + with: { + inner: { + ...{ value }, + }, + }, + } + });", + "const value = 1; + jest.fn().mockImplementation(() => { + return { + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + } + });", + "const value = 1; + jest.fn().mockImplementation(() => { + return [{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }] + });", + "const mx = 1 + let my = 2; + aVariable.mockImplementation(() => mx || my); + aVariable.mockImplementation(() => mx || 0); + aVariable.mockImplementation(() => my && mx); + aVariable.mockImplementation(() => mx ?? (7 && my)); + aVariable.mockImplementation(() => mx ?? (7 && 0));", + "const value = 1; + jest.fn().mockImplementation(() => new Mx(value)); + jest.fn().mockImplementation(() => new Mx(() => value)); + jest.fn().mockImplementation(() => new Mx(() => { return value }));", + "const value = 1; + jest.fn().mockImplementation(() => mx(value)); + jest.fn().mockImplementation(() => mx?.(value)); + jest.fn().mockImplementation(() => mx().my()); + jest.fn().mockImplementation(() => mx().my); + jest.fn().mockImplementation(() => mx.my()); + jest.fn().mockImplementation(() => mx?.my()); + jest.fn().mockImplementation(() => mx.my); + jest.fn().mockImplementation(() => mx(value).my()); + jest.fn().mockImplementation(() => mx(value)?.my()); + jest.fn().mockImplementation(() => mx(value).my); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx().my(value)); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx.my?.(value)); + jest.fn().mockImplementation(() => mx(value).my(value)); + jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + jest.fn().mockImplementation(() => new Mx().add(value)); + jest.fn().mockImplementation(() => { + return mx([{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }]) + });", + "const propName = 'world'; + aVariable.mockImplementation(() => mx[propName]()); + aVariable.mockImplementation(() => mx[propName]); + aVariable.mockImplementation(() => ({ [propName]: 1 }));", + "const x = true; + let value = 1; + aVariable.mockImplementation(() => value ? true : false); + aVariable.mockImplementation(() => x ? true : false); + aVariable.mockImplementation(() => x ? true : value); + aVariable.mockImplementation(() => true ? true : value); + aVariable.mockImplementation(() => true ? true : true ? value : false); + aVariable.mockImplementation(() => true ? true : true ? x : false); + aVariable.mockImplementation(() => true ? true : true ? true : false);", + ]; + + let mut fix = vec![ + ( + r#"jest.fn().mockImplementation(() => "hello sunshine")"#, + r#"jest.fn().mockReturnValue("hello sunshine")"#, + ), + ("jest.fn().mockImplementation(() => ({}))", "jest.fn().mockReturnValue({})"), + ("jest.fn().mockImplementation(() => x)", "jest.fn().mockReturnValue(x)"), + ( + "jest.fn().mockImplementation(() => true ? x : y)", + "jest.fn().mockReturnValue(true ? x : y)", + ), + ( + r#"jest.fn().mockImplementation(function () { + return "hello world"; + })"#, + r#"jest.fn().mockReturnValue("hello world")"#, + ), + ( + r#"jest.fn().mockImplementation(() => "hello world")"#, + r#"jest.fn().mockReturnValue("hello world")"#, + ), + ( + r#"jest.fn().mockImplementation(() => { + return "hello world"; + })"#, + r#"jest.fn().mockReturnValue("hello world")"#, + ), + ( + r#"aVariable.mockImplementation(() => "hello world")"#, + r#"aVariable.mockReturnValue("hello world")"#, + ), + ( + r#"aVariable.mockImplementation(() => { + return "hello world"; + })"#, + r#"aVariable.mockReturnValue("hello world")"#, + ), + ( + r#"jest.fn().mockImplementationOnce(() => "hello world")"#, + r#"jest.fn().mockReturnValueOnce("hello world")"#, + ), + ( + r#"aVariable.mockImplementationOnce(() => "hello world")"#, + r#"aVariable.mockReturnValueOnce("hello world")"#, + ), + ( + "aVariable.mockImplementation(() => ({ + target: 'world', + message: 'hello' + }))", + "aVariable.mockReturnValue({ + target: 'world', + message: 'hello' + })", + ), + ( + r#"aVariable + .mockImplementation(() => 42) + .mockImplementation(async () => 42) + .mockImplementation(() => Promise.resolve(42)) + .mockReturnValue("hello world")"#, + r#"aVariable + .mockReturnValue(42) + .mockImplementation(async () => 42) + .mockReturnValue(Promise.resolve(42)) + .mockReturnValue("hello world")"#, + ), + ( + r#"aVariable + .mockImplementationOnce(() => Promise.reject(42)) + .mockImplementation(() => "hello sunshine") + .mockReturnValueOnce(Promise.reject(42))"#, + r#"aVariable + .mockReturnValueOnce(Promise.reject(42)) + .mockReturnValue("hello sunshine") + .mockReturnValueOnce(Promise.reject(42))"#, + ), + ("jest.fn().mockImplementation(() => [], xyz)", "jest.fn().mockReturnValue([], xyz)"), + ( + r#"jest.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, + r#"jest.spyOn(fs, "readFile").mockReturnValue(new Error("oh noes!"))"#, + ), + ( + "aVariable.mockImplementation(() => { + return Promise.resolve(value) + .then(value => value + 1); + });", + "aVariable.mockReturnValue(Promise.resolve(value) + .then(value => value + 1));", + ), + ( + "aVariable.mockImplementation(() => { + return Promise.all([1, 2, 3]); + });", + "aVariable.mockReturnValue(Promise.all([1, 2, 3]));", + ), + ( + "const currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "const currentX = 0; + jest.spyOn(X, getCount).mockReturnValue(currentX);", + ), + ( + "import { currentX } from './elsewhere'; + jest.spyOn(X, getCount).mockImplementation(() => currentX);", + "import { currentX } from './elsewhere'; + jest.spyOn(X, getCount).mockReturnValue(currentX);", + ), + ( + "const currentX = 0; + describe('some tests', () => { + it('works', () => { + jest.spyOn(X, getCount).mockImplementation(() => currentX); + }); + });", + "const currentX = 0; + describe('some tests', () => { + it('works', () => { + jest.spyOn(X, getCount).mockReturnValue(currentX); + }); + });", + ), + ( + "function doSomething() {}; + jest.spyOn(X, getCount).mockImplementation(() => doSomething);", + "function doSomething() {}; + jest.spyOn(X, getCount).mockReturnValue(doSomething);", + ), + ( + "const doSomething = () => {}; + jest.spyOn(X, getCount).mockImplementation(() => doSomething);", + "const doSomething = () => {}; + jest.spyOn(X, getCount).mockReturnValue(doSomething);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => [value]);", + "const value = 1; + aVariable.mockReturnValue([value]);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => [0, value, 2]);", + "const value = 1; + aVariable.mockReturnValue([0, value, 2]);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => [0,, value, 2]);", + "const value = 1; + aVariable.mockReturnValue([0,, value, 2]);", + ), + ( + "const value = 1; + jest.fn().mockImplementation(() => ({ value }));", + "const value = 1; + jest.fn().mockReturnValue({ value });", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => ({ items: [value] }));", + "const value = 1; + aVariable.mockReturnValue({ items: [value] });", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: { value }, + } + });", + "const value = 1; + aVariable.mockReturnValue({ + type: 'object', + with: { value }, + });", + ), + ( + "const vX = 1; + let vY = 1; + getPoint.mockImplementation(() => vX + vY); + getPoint.mockImplementation(() => { + return { x: vX, y: 1 } + });", + "const vX = 1; + let vY = 1; + getPoint.mockImplementation(() => vX + vY); + getPoint.mockReturnValue({ x: vX, y: 1 });", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => value & 0); + aVariable.mockImplementation(() => 0 & value); + aVariable.mockImplementation(() => value | 1); + aVariable.mockImplementation(() => 1 | value);", + "const value = 1; + aVariable.mockReturnValue(value & 0); + aVariable.mockReturnValue(0 & value); + aVariable.mockReturnValue(value | 1); + aVariable.mockReturnValue(1 | value);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => ~value); + aVariable.mockImplementation(() => !value);", + "const value = 1; + aVariable.mockReturnValue(~value); + aVariable.mockReturnValue(!value);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => value + 1); + aVariable.mockImplementation(() => 1 + value); + aVariable.mockImplementation(() => value * value + 1); + aVariable.mockImplementation(() => 1 + value / 2); + aVariable.mockImplementation(() => (1 + value) / 2);", + "const value = 1; + aVariable.mockReturnValue(value + 1); + aVariable.mockReturnValue(1 + value); + aVariable.mockReturnValue(value * value + 1); + aVariable.mockReturnValue(1 + value / 2); + aVariable.mockReturnValue((1 + value) / 2);", + ), + ( + "const value = 1; + aVariable.mockImplementation(() => { + return { + type: 'object', + with: [1, 2, value], + } + });", + "const value = 1; + aVariable.mockReturnValue({ + type: 'object', + with: [1, 2, value], + });", + ), + ( + "const obj = {}; + aVariable.mockImplementation(() => { + return { + type: 'object', + ...obj, + } + });", + "const obj = {}; + aVariable.mockReturnValue({ + type: 'object', + ...obj, + });", + ), + ( + "const value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, value]} + ]; + });", + "const value = 1; + jest.fn().mockReturnValueOnce([ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, value]} + ]);", + ), + ( + "const value = 1; + jest.fn().mockImplementationOnce(() => { + return [ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, 0 + value]} + ]; + });", + "const value = 1; + jest.fn().mockReturnValueOnce([ + 1, + {type: 'object', with: [1, 2, 3]}, + {type: 'object', with: [1, 2, 0 + value]} + ]);", + ), + ( + "const value = 1; + aVariable.mockImplementationOnce(() => { + return { + type: 'object', + with: { + inner: { + value, + }, + }, + } + });", + "const value = 1; + aVariable.mockReturnValueOnce({ + type: 'object', + with: { + inner: { + value, + }, + }, + });", + ), + ( + "const value = 1; + aVariable.mockImplementationOnce(() => { + return { + type: 'object', + with: { + inner: { + ...{ value }, + }, + }, + } + });", + "const value = 1; + aVariable.mockReturnValueOnce({ + type: 'object', + with: { + inner: { + ...{ value }, + }, + }, + });", + ), + ( + "const value = 1; + jest.fn().mockImplementation(() => { + return { + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + } + });", + "const value = 1; + jest.fn().mockReturnValue({ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + });", + ), + ( + "const value = 1; + jest.fn().mockImplementation(() => { + return [{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }] + });", + "const value = 1; + jest.fn().mockReturnValue([{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }]);", + ), + ( + "const mx = 1 + let my = 2; + aVariable.mockImplementation(() => mx || my); + aVariable.mockImplementation(() => mx || 0); + aVariable.mockImplementation(() => my && mx); + aVariable.mockImplementation(() => mx ?? (7 && my)); + aVariable.mockImplementation(() => mx ?? (7 && 0));", + "const mx = 1 + let my = 2; + aVariable.mockImplementation(() => mx || my); + aVariable.mockReturnValue(mx || 0); + aVariable.mockImplementation(() => my && mx); + aVariable.mockImplementation(() => mx ?? (7 && my)); + aVariable.mockReturnValue(mx ?? (7 && 0));", + ), + ( + "const value = 1; + jest.fn().mockImplementation(() => new Mx(value)); + jest.fn().mockImplementation(() => new Mx(() => value)); + jest.fn().mockImplementation(() => new Mx(() => { return value }));", + "const value = 1; + jest.fn().mockReturnValue(new Mx(value)); + jest.fn().mockReturnValue(new Mx(() => value)); + jest.fn().mockReturnValue(new Mx(() => { return value }));", + ), + ( + "const value = 1; + jest.fn().mockImplementation(() => mx(value)); + jest.fn().mockImplementation(() => mx?.(value)); + jest.fn().mockImplementation(() => mx().my()); + jest.fn().mockImplementation(() => mx().my); + jest.fn().mockImplementation(() => mx.my()); + jest.fn().mockImplementation(() => mx?.my()); + jest.fn().mockImplementation(() => mx.my); + jest.fn().mockImplementation(() => mx(value).my()); + jest.fn().mockImplementation(() => mx(value)?.my()); + jest.fn().mockImplementation(() => mx(value).my); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx().my(value)); + jest.fn().mockImplementation(() => mx.my(value)); + jest.fn().mockImplementation(() => mx.my?.(value)); + jest.fn().mockImplementation(() => mx(value).my(value)); + jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + jest.fn().mockImplementation(() => new Mx().add(value)); + jest.fn().mockImplementation(() => { + return mx([{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }]) + });", + "const value = 1; + jest.fn().mockReturnValue(mx(value)); + jest.fn().mockReturnValue(mx?.(value)); + jest.fn().mockReturnValue(mx().my()); + jest.fn().mockReturnValue(mx().my); + jest.fn().mockReturnValue(mx.my()); + jest.fn().mockReturnValue(mx?.my()); + jest.fn().mockReturnValue(mx.my); + jest.fn().mockReturnValue(mx(value).my()); + jest.fn().mockReturnValue(mx(value)?.my()); + jest.fn().mockReturnValue(mx(value).my); + jest.fn().mockReturnValue(mx.my(value)); + jest.fn().mockReturnValue(mx().my(value)); + jest.fn().mockReturnValue(mx.my(value)); + jest.fn().mockReturnValue(mx.my?.(value)); + jest.fn().mockReturnValue(mx(value).my(value)); + jest.fn().mockReturnValue(mx?.(value)?.my?.(value)); + jest.fn().mockReturnValue(new Mx().add(value)); + jest.fn().mockReturnValue(mx([{ + type: 'object', + with: { + inner: { + items: [1, 2, value], + }, + }, + }]));", + ), + ( + "const propName = 'world'; + aVariable.mockImplementation(() => mx[propName]()); + aVariable.mockImplementation(() => mx[propName]); + aVariable.mockImplementation(() => ({ [propName]: 1 }));", + "const propName = 'world'; + aVariable.mockReturnValue(mx[propName]()); + aVariable.mockReturnValue(mx[propName]); + aVariable.mockReturnValue({ [propName]: 1 });", + ), + ( + "const x = true; + let value = 1; + aVariable.mockImplementation(() => value ? true : false); + aVariable.mockImplementation(() => x ? true : false); + aVariable.mockImplementation(() => x ? true : value); + aVariable.mockImplementation(() => true ? true : value); + aVariable.mockImplementation(() => true ? true : true ? value : false); + aVariable.mockImplementation(() => true ? true : true ? x : false); + aVariable.mockImplementation(() => true ? true : true ? true : false);", + "const x = true; + let value = 1; + aVariable.mockImplementation(() => value ? true : false); + aVariable.mockReturnValue(x ? true : false); + aVariable.mockImplementation(() => x ? true : value); + aVariable.mockImplementation(() => true ? true : value); + aVariable.mockImplementation(() => true ? true : true ? value : false); + aVariable.mockReturnValue(true ? true : true ? x : false); + aVariable.mockReturnValue(true ? true : true ? true : false);", + ), + ]; + + let vitest_pass = vec![ + "describe()", + "it()", + "describe.skip()", + "it.skip()", + "test()", + "test.skip()", + "var appliedOnly = describe.only; appliedOnly.apply(describe)", + "var calledOnly = it.only; calledOnly.call(it)", + "it.each()()", + "it.each`table`()", + "test.each()()", + "test.each`table`()", + "test.concurrent()", + "vi.fn().mockReturnValue(42)", + "vi.fn(() => Promise.resolve(42))", + "vi.fn(() => 42)", + "vi.fn(() => ({}))", + "aVariable.mockImplementation", + "aVariable.mockImplementation()", + "jest.fn().mockImplementation(async () => 1);", // { "parserOptions": { "ecmaVersion": 2017 } }, + "jest.fn().mockImplementation(async function () {});", // { "parserOptions": { "ecmaVersion": 2017 } }, + " + jest.fn().mockImplementation(async function () { + return 42; + }); + ", // { "parserOptions": { "ecmaVersion": 2017 } }, + " + aVariable.mockImplementation(() => { + if (true) { + return 1; + } + + return 2; + }); + ", + "aVariable.mockImplementation(() => value++)", + "aVariable.mockImplementationOnce(() => --value)", + " + const aValue = 0; + aVariable.mockImplementation(() => { + return aValue++; + }); + ", + " + aVariable.mockImplementation(() => { + aValue += 1; + + return aValue; + }); + ", + " + aVariable.mockImplementation(() => { + aValue++; + + return aValue; + }); + ", + "aVariable.mockReturnValue()", + "aVariable.mockReturnValue(1)", + r#"aVariable.mockReturnValue("hello world")"#, + "vi.spyOn(Thingy, 'method').mockImplementation(param => param * 2);", + "vi.spyOn(Thingy, 'method').mockImplementation(param => true ? param : 0);", + " + aVariable.mockImplementation(() => { + const value = new Date(); + + return Promise.resolve(value); + }); + ", + " + aVariable.mockImplementation(() => { + throw new Error('oh noes!'); + }); + ", + "aVariable.mockImplementation(() => { /* do something */ });", + " + aVariable.mockImplementation(() => { + const x = 1; + + console.log(x + 2); + }); + ", + "aVariable.mockReturnValue(Promise.all([1, 2, 3]));", + " + let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + + // stuff happens + + currentX++; + + // more stuff happens + ", + " + let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + ", + " + let currentX = 0; + currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + ", + " + var currentX = 0; + currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + ", + " + var currentX = 0; + var currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => currentX); + ", + " + let doSomething = () => {}; + + jest.spyOn(X, getCount).mockImplementation(() => doSomething); + ", + " + let currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => { + currentX += 1; + + return currentX; + }); + ", + " + const currentX = 0; + jest.spyOn(X, getCount).mockImplementation(() => { + console.log('returning', currentX); + + return currentX; + }); + ", + ]; + + let vitest_fail = vec![ + r#"vi.fn().mockImplementation(() => "hello sunshine")"#, + "vi.fn().mockImplementation(() => ({}))", + "vi.fn().mockImplementation(() => x)", + "vi.fn().mockImplementation(() => true ? x : y)", + r#"vi.fn().mockImplementation(() => "hello world")"#, + r#"aVariable.mockImplementation(() => "hello world")"#, + r#"vi.fn().mockImplementationOnce(() => "hello world")"#, + r#"aVariable.mockImplementationOnce(() => "hello world")"#, + "vi.fn().mockImplementation(() => [], xyz)", + r#"vi.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, + ]; + + let vitest_fix = vec![ + ( + r#"vi.fn().mockImplementation(() => "hello sunshine")"#, + r#"vi.fn().mockReturnValue("hello sunshine")"#, + ), + ("vi.fn().mockImplementation(() => ({}))", "vi.fn().mockReturnValue({})"), + ("vi.fn().mockImplementation(() => x)", "vi.fn().mockReturnValue(x)"), + ("vi.fn().mockImplementation(() => true ? x : y)", "vi.fn().mockReturnValue(true ? x : y)"), + ( + r#"vi.fn().mockImplementation(() => "hello world")"#, + r#"vi.fn().mockReturnValue("hello world")"#, + ), + ( + r#"aVariable.mockImplementation(() => "hello world")"#, + r#"aVariable.mockReturnValue("hello world")"#, + ), + ( + r#"vi.fn().mockImplementationOnce(() => "hello world")"#, + r#"vi.fn().mockReturnValueOnce("hello world")"#, + ), + ( + r#"aVariable.mockImplementationOnce(() => "hello world")"#, + r#"aVariable.mockReturnValueOnce("hello world")"#, + ), + ("vi.fn().mockImplementation(() => [], xyz)", "vi.fn().mockReturnValue([], xyz)"), + ( + r#"vi.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!"))"#, + r#"vi.spyOn(fs, "readFile").mockReturnValue(new Error("oh noes!"))"#, + ), + ]; + + pass.extend(vitest_pass); + fail.extend(vitest_fail); + fix.extend(vitest_fix); + + Tester::new(PreferMockReturnShorthand::NAME, PreferMockReturnShorthand::PLUGIN, pass, fail) + .expect_fix(fix) + .with_jest_plugin(true) + .with_vitest_plugin(true) + .test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap b/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap new file mode 100644 index 0000000000000..ce804c3d266c1 --- /dev/null +++ b/crates/oxc_linter/src/snapshots/jest_prefer_mock_return_shorthand.snap @@ -0,0 +1,751 @@ +--- +source: crates/oxc_linter/src/tester.rs +--- + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => "hello sunshine") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => ({})) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => x) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => true ? x : y) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(function () { + · ────────────────── + 2 │ return "hello world"; + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => "hello world") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => { + · ────────────────── + 2 │ return "hello world"; + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => "hello world") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => { + · ────────────────── + 2 │ return "hello world"; + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementationOnce(() => "hello world") + · ────────────────────── + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementationOnce(() => "hello world") + · ────────────────────── + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => ({ + · ────────────────── + 2 │ target: 'world', + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:16] + 3 │ .mockImplementation(async () => 42) + 4 │ .mockImplementation(() => Promise.resolve(42)) + · ────────────────── + 5 │ .mockReturnValue("hello world") + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:16] + 1 │ aVariable + 2 │ .mockImplementation(() => 42) + · ────────────────── + 3 │ .mockImplementation(async () => 42) + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:16] + 2 │ .mockImplementationOnce(() => Promise.reject(42)) + 3 │ .mockImplementation(() => "hello sunshine") + · ────────────────── + 4 │ .mockReturnValueOnce(Promise.reject(42)) + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:16] + 1 │ aVariable + 2 │ .mockImplementationOnce(() => Promise.reject(42)) + · ────────────────────── + 3 │ .mockImplementation(() => "hello sunshine") + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ jest.fn().mockImplementation(() => [], xyz) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:28] + 1 │ jest.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!")) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => { + · ────────────────── + 2 │ return Promise.resolve(value) + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => { + · ────────────────── + 2 │ return Promise.all([1, 2, 3]); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:37] + 1 │ const currentX = 0; + 2 │ jest.spyOn(X, getCount).mockImplementation(() => currentX); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:37] + 1 │ import { currentX } from './elsewhere'; + 2 │ jest.spyOn(X, getCount).mockImplementation(() => currentX); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:41] + 3 │ it('works', () => { + 4 │ jest.spyOn(X, getCount).mockImplementation(() => currentX); + · ────────────────── + 5 │ }); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:37] + 1 │ function doSomething() {}; + 2 │ jest.spyOn(X, getCount).mockImplementation(() => doSomething); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:37] + 1 │ const doSomething = () => {}; + 2 │ jest.spyOn(X, getCount).mockImplementation(() => doSomething); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => [value]); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => [0, value, 2]); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => [0,, value, 2]); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementation(() => ({ value })); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => ({ items: [value] })); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => { + · ────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:22] + 3 │ getPoint.mockImplementation(() => vX + vY); + 4 │ getPoint.mockImplementation(() => { + · ────────────────── + 5 │ return { x: vX, y: 1 } + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => value & 0); + · ────────────────── + 3 │ aVariable.mockImplementation(() => 0 & value); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ aVariable.mockImplementation(() => value & 0); + 3 │ aVariable.mockImplementation(() => 0 & value); + · ────────────────── + 4 │ aVariable.mockImplementation(() => value | 1); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ aVariable.mockImplementation(() => 0 & value); + 4 │ aVariable.mockImplementation(() => value | 1); + · ────────────────── + 5 │ aVariable.mockImplementation(() => 1 | value); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:5:23] + 4 │ aVariable.mockImplementation(() => value | 1); + 5 │ aVariable.mockImplementation(() => 1 | value); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => ~value); + · ────────────────── + 3 │ aVariable.mockImplementation(() => !value); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ aVariable.mockImplementation(() => ~value); + 3 │ aVariable.mockImplementation(() => !value); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => value + 1); + · ────────────────── + 3 │ aVariable.mockImplementation(() => 1 + value); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ aVariable.mockImplementation(() => value + 1); + 3 │ aVariable.mockImplementation(() => 1 + value); + · ────────────────── + 4 │ aVariable.mockImplementation(() => value * value + 1); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ aVariable.mockImplementation(() => 1 + value); + 4 │ aVariable.mockImplementation(() => value * value + 1); + · ────────────────── + 5 │ aVariable.mockImplementation(() => 1 + value / 2); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:5:23] + 4 │ aVariable.mockImplementation(() => value * value + 1); + 5 │ aVariable.mockImplementation(() => 1 + value / 2); + · ────────────────── + 6 │ aVariable.mockImplementation(() => (1 + value) / 2); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:6:23] + 5 │ aVariable.mockImplementation(() => 1 + value / 2); + 6 │ aVariable.mockImplementation(() => (1 + value) / 2); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementation(() => { + · ────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const obj = {}; + 2 │ aVariable.mockImplementation(() => { + · ────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementationOnce(() => { + · ────────────────────── + 3 │ return [ + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementationOnce(() => { + · ────────────────────── + 3 │ return [ + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementationOnce(() => { + · ────────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ aVariable.mockImplementationOnce(() => { + · ────────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementation(() => { + · ────────────────── + 3 │ return { + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementation(() => { + · ────────────────── + 3 │ return [{ + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ aVariable.mockImplementation(() => mx || my); + 4 │ aVariable.mockImplementation(() => mx || 0); + · ────────────────── + 5 │ aVariable.mockImplementation(() => my && mx); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:7:23] + 6 │ aVariable.mockImplementation(() => mx ?? (7 && my)); + 7 │ aVariable.mockImplementation(() => mx ?? (7 && 0)); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementation(() => new Mx(value)); + · ────────────────── + 3 │ jest.fn().mockImplementation(() => new Mx(() => value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ jest.fn().mockImplementation(() => new Mx(value)); + 3 │ jest.fn().mockImplementation(() => new Mx(() => value)); + · ────────────────── + 4 │ jest.fn().mockImplementation(() => new Mx(() => { return value })); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ jest.fn().mockImplementation(() => new Mx(() => value)); + 4 │ jest.fn().mockImplementation(() => new Mx(() => { return value })); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const value = 1; + 2 │ jest.fn().mockImplementation(() => mx(value)); + · ────────────────── + 3 │ jest.fn().mockImplementation(() => mx?.(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ jest.fn().mockImplementation(() => mx(value)); + 3 │ jest.fn().mockImplementation(() => mx?.(value)); + · ────────────────── + 4 │ jest.fn().mockImplementation(() => mx().my()); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ jest.fn().mockImplementation(() => mx?.(value)); + 4 │ jest.fn().mockImplementation(() => mx().my()); + · ────────────────── + 5 │ jest.fn().mockImplementation(() => mx().my); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:5:23] + 4 │ jest.fn().mockImplementation(() => mx().my()); + 5 │ jest.fn().mockImplementation(() => mx().my); + · ────────────────── + 6 │ jest.fn().mockImplementation(() => mx.my()); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:6:23] + 5 │ jest.fn().mockImplementation(() => mx().my); + 6 │ jest.fn().mockImplementation(() => mx.my()); + · ────────────────── + 7 │ jest.fn().mockImplementation(() => mx?.my()); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:7:23] + 6 │ jest.fn().mockImplementation(() => mx.my()); + 7 │ jest.fn().mockImplementation(() => mx?.my()); + · ────────────────── + 8 │ jest.fn().mockImplementation(() => mx.my); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:8:23] + 7 │ jest.fn().mockImplementation(() => mx?.my()); + 8 │ jest.fn().mockImplementation(() => mx.my); + · ────────────────── + 9 │ jest.fn().mockImplementation(() => mx(value).my()); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:9:23] + 8 │ jest.fn().mockImplementation(() => mx.my); + 9 │ jest.fn().mockImplementation(() => mx(value).my()); + · ────────────────── + 10 │ jest.fn().mockImplementation(() => mx(value)?.my()); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:10:23] + 9 │ jest.fn().mockImplementation(() => mx(value).my()); + 10 │ jest.fn().mockImplementation(() => mx(value)?.my()); + · ────────────────── + 11 │ jest.fn().mockImplementation(() => mx(value).my); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:11:23] + 10 │ jest.fn().mockImplementation(() => mx(value)?.my()); + 11 │ jest.fn().mockImplementation(() => mx(value).my); + · ────────────────── + 12 │ jest.fn().mockImplementation(() => mx.my(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:12:23] + 11 │ jest.fn().mockImplementation(() => mx(value).my); + 12 │ jest.fn().mockImplementation(() => mx.my(value)); + · ────────────────── + 13 │ jest.fn().mockImplementation(() => mx().my(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:13:23] + 12 │ jest.fn().mockImplementation(() => mx.my(value)); + 13 │ jest.fn().mockImplementation(() => mx().my(value)); + · ────────────────── + 14 │ jest.fn().mockImplementation(() => mx.my(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:14:23] + 13 │ jest.fn().mockImplementation(() => mx().my(value)); + 14 │ jest.fn().mockImplementation(() => mx.my(value)); + · ────────────────── + 15 │ jest.fn().mockImplementation(() => mx.my?.(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:15:23] + 14 │ jest.fn().mockImplementation(() => mx.my(value)); + 15 │ jest.fn().mockImplementation(() => mx.my?.(value)); + · ────────────────── + 16 │ jest.fn().mockImplementation(() => mx(value).my(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:16:23] + 15 │ jest.fn().mockImplementation(() => mx.my?.(value)); + 16 │ jest.fn().mockImplementation(() => mx(value).my(value)); + · ────────────────── + 17 │ jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:17:23] + 16 │ jest.fn().mockImplementation(() => mx(value).my(value)); + 17 │ jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + · ────────────────── + 18 │ jest.fn().mockImplementation(() => new Mx().add(value)); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:18:23] + 17 │ jest.fn().mockImplementation(() => mx?.(value)?.my?.(value)); + 18 │ jest.fn().mockImplementation(() => new Mx().add(value)); + · ────────────────── + 19 │ jest.fn().mockImplementation(() => { + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:19:23] + 18 │ jest.fn().mockImplementation(() => new Mx().add(value)); + 19 │ jest.fn().mockImplementation(() => { + · ────────────────── + 20 │ return mx([{ + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:2:23] + 1 │ const propName = 'world'; + 2 │ aVariable.mockImplementation(() => mx[propName]()); + · ────────────────── + 3 │ aVariable.mockImplementation(() => mx[propName]); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:3:23] + 2 │ aVariable.mockImplementation(() => mx[propName]()); + 3 │ aVariable.mockImplementation(() => mx[propName]); + · ────────────────── + 4 │ aVariable.mockImplementation(() => ({ [propName]: 1 })); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ aVariable.mockImplementation(() => mx[propName]); + 4 │ aVariable.mockImplementation(() => ({ [propName]: 1 })); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:4:23] + 3 │ aVariable.mockImplementation(() => value ? true : false); + 4 │ aVariable.mockImplementation(() => x ? true : false); + · ────────────────── + 5 │ aVariable.mockImplementation(() => x ? true : value); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:8:23] + 7 │ aVariable.mockImplementation(() => true ? true : true ? value : false); + 8 │ aVariable.mockImplementation(() => true ? true : true ? x : false); + · ────────────────── + 9 │ aVariable.mockImplementation(() => true ? true : true ? true : false); + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:9:23] + 8 │ aVariable.mockImplementation(() => true ? true : true ? x : false); + 9 │ aVariable.mockImplementation(() => true ? true : true ? true : false); + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => "hello sunshine") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => ({})) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => x) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => true ? x : y) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => "hello world") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementation(() => "hello world") + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementationOnce(() => "hello world") + · ────────────────────── + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:11] + 1 │ aVariable.mockImplementationOnce(() => "hello world") + · ────────────────────── + ╰──── + help: Replace `mockImplementationOnce` with `mockReturnValueOnce`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:9] + 1 │ vi.fn().mockImplementation(() => [], xyz) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. + + ⚠ eslint-plugin-jest(prefer-mock-return-shorthand): Mock functions that return simple values should use `mockReturnValue/mockReturnValueOnce`. + ╭─[prefer_mock_return_shorthand.tsx:1:26] + 1 │ vi.spyOn(fs, "readFile").mockImplementation(() => new Error("oh noes!")) + · ────────────────── + ╰──── + help: Replace `mockImplementation` with `mockReturnValue`. diff --git a/crates/oxc_linter/src/utils/mod.rs b/crates/oxc_linter/src/utils/mod.rs index 981088b4ecce9..47318884e51fd 100644 --- a/crates/oxc_linter/src/utils/mod.rs +++ b/crates/oxc_linter/src/utils/mod.rs @@ -38,7 +38,7 @@ pub use self::{ // the crates/oxc_linter/data/vitest_compatible_jest_rules.json // file is also updated. The JSON file is used by the oxlint-migrate // and eslint-plugin-oxlint repos to keep everything synced. -const VITEST_COMPATIBLE_JEST_RULES: [&str; 42] = [ +const VITEST_COMPATIBLE_JEST_RULES: [&str; 43] = [ "consistent-test-it", "expect-expect", "max-expects", @@ -70,6 +70,7 @@ const VITEST_COMPATIBLE_JEST_RULES: [&str; 42] = [ "prefer-hooks-on-top", "prefer-lowercase-title", "prefer-mock-promise-shorthand", + "prefer-mock-return-shorthand", "prefer-spy-on", "prefer-strict-equal", "prefer-to-be",