Skip to content

feat(linter/jest): implement prefer-ending-with-an-expect rule#21372

Merged
camc314 merged 1 commit into
oxc-project:mainfrom
Afsoon:jest-prefer-ending-with-an-expect
Apr 13, 2026
Merged

feat(linter/jest): implement prefer-ending-with-an-expect rule#21372
camc314 merged 1 commit into
oxc-project:mainfrom
Afsoon:jest-prefer-ending-with-an-expect

Conversation

@Afsoon

@Afsoon Afsoon commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

Related to #492

AI Disclosure

Only used AI to generate the diagnostic messages and doc. The code has been made from zero.

Summary

Implements prefer-ending-with-an-expect. The pattern logic has been copy from the rule expect-expect

Info

@Afsoon
Afsoon requested a review from camc314 as a code owner April 13, 2026 09:03
@github-actions github-actions Bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Apr 13, 2026
@codspeed-hq

codspeed-hq Bot commented Apr 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 4 untouched benchmarks
⏩ 47 skipped benchmarks1


Comparing Afsoon:jest-prefer-ending-with-an-expect (b117303) with main (e7e1aea)2

Open in CodSpeed

Footnotes

  1. 47 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (995e13e) during the generation of this report, so e7e1aea was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@camc314

camc314 commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

The pattern logic has been copy from the rule expect-expect

@Afsoon is it easy to share the logic? if so, mind making a follow up PR?

Comment on lines +160 to +182
let config = value.get(0);

let assert_function_names = config
.and_then(|config| config.get("assertFunctionNames"))
.and_then(serde_json::Value::as_array)
.map(|v| {
v.iter()
.filter_map(serde_json::Value::as_str)
.map(convert_pattern)
.collect::<Vec<_>>()
})
.unwrap_or(vec!["expect".into()]);

let additional_test_block_functions = config
.and_then(|config| config.get("additionalTestBlockFunctions"))
.and_then(serde_json::Value::as_array)
.map(|v| v.iter().filter_map(serde_json::Value::as_str).map(CompactStr::from).collect())
.unwrap_or_default();

Ok(Self(Box::new(PreferEndingWithAnExpectConfig {
additional_test_block_functions,
assert_function_names,
})))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: ideally this should be refactored to use DefaultRuleConfig, so that config errors can be surfaced to the user. we can do this at another time though.

@Afsoon

Afsoon commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

@camc314 Yep, it's easy. I will make a follow up PR

@camc314 camc314 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank yoU!

@camc314 camc314 changed the title feat(linter/eslint-plugin-jest): Implemented prefer-ending-with-an-expect rule feat(linter/jest): implement prefer-ending-with-an-expect rule Apr 13, 2026
@camc314 camc314 self-assigned this Apr 13, 2026
@camc314
camc314 merged commit bab5c31 into oxc-project:main Apr 13, 2026
27 checks passed
camc314 pushed a commit that referenced this pull request Apr 13, 2026
@robsterlini

Copy link
Copy Markdown

This rule has a few false positives that make it tricky to enable atm.

Async waitFor with expect shows as incorrect:

    await waitFor(() =>
      expect(mockAssign).toHaveBeenCalledWith("https://stub-example.com")
    );

jest.mock shows as incorrect:

jest.mock("../../helpers/polyfillRequestSubmit", () => ({
  ...jest.requireActual("../../helpers/polyfillRequestSubmit"),
  polyfillRequestSubmit: jest.fn(),
}));

@Afsoon

Afsoon commented Apr 13, 2026

Copy link
Copy Markdown
Contributor Author

@robsterlini can you create an issue while I check it?

camc314 added a commit that referenced this pull request Apr 13, 2026
…21390)

Follow up PR #21372, Added the version docs property.

Related to #21362

---------

Signed-off-by: Cameron <[email protected]>
Co-authored-by: Cameron <[email protected]>
@Afsoon

Afsoon commented Apr 14, 2026

Copy link
Copy Markdown
Contributor Author

@robsterlini about the false positives:

  • The waitFor isn't a false positive, OG rule have the main problem with default values. The assert function comes from testing-library and not Jest. I have been able to reproduce in Eslint + Testing library eslint. If you want to suppress the error, you should add waitFor as an valid last assert function in assertFunctionsNames.

  • The second false positive has been solved with this PR linter: jest/prefer-ending-with-an-expect false positive with vi.mock #21413

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants