Skip to content

feat(valid-expect): support using typechecking to ensure toThrow type assertions are passed a function#1955

Draft
G-Rath wants to merge 9 commits into
mainfrom
valid-expect/check-toThrow
Draft

feat(valid-expect): support using typechecking to ensure toThrow type assertions are passed a function#1955
G-Rath wants to merge 9 commits into
mainfrom
valid-expect/check-toThrow

Conversation

@G-Rath

@G-Rath G-Rath commented Mar 28, 2026

Copy link
Copy Markdown
Collaborator

Resolves #1329

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds an optional type-aware enhancement to jest/valid-expect so that, when enabled, toThrow* matchers require expect() to be passed a callable (addressing #1329).

Changes:

  • Add a typecheck option to valid-expect and, when enabled, use TypeScript parser services to validate toThrow* inputs are callable.
  • Add type-aware test coverage for the new option, including “optional dependency” behavior checks.
  • Document the new typecheck option in the rule docs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/rules/valid-expect.ts Introduces typecheck option and type-based callable validation for toThrow* matchers.
src/rules/__tests__/valid-expect.test.ts Adds tests for the new typecheck behavior and its dependency/type-info requirements.
docs/rules/valid-expect.md Documents the new typecheck option and its requirement for TS type information.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rules/valid-expect.ts Outdated
Comment thread docs/rules/valid-expect.md
@G-Rath G-Rath force-pushed the valid-expect/check-toThrow branch from c48fb12 to 1158149 Compare March 28, 2026 21:01
@G-Rath G-Rath requested a review from Copilot March 28, 2026 21:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/rules/valid-expect.ts
Comment on lines +388 to +393
if (type.flags & (TypeFlags.Any | TypeFlags.Unknown)) {
return null;
}

return fixer.insertTextBefore(expect.arguments[0], '() => ');
},

Copilot AI Mar 28, 2026

Copy link

Choose a reason for hiding this comment

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

The autofix currently only inserts '() => ' before the expect(...) argument. That can produce invalid syntax (e.g. if the argument is an await/yield expression) and can also change parsing/meaning due to operator precedence (e.g. sequence expressions) or object-literal vs block-body ambiguity. Consider making the fix more robust by (a) bailing out for AwaitExpression/YieldExpression (and similar cases), and (b) wrapping the original argument in parentheses via a paired insertTextBefore/insertTextAfter (or using a block body with return) so the resulting code always parses as intended.

Copilot uses AI. Check for mistakes.
Comment thread docs/rules/valid-expect.md
@G-Rath G-Rath force-pushed the valid-expect/check-toThrow branch from cd124ff to 6fca118 Compare March 28, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ensure expect(function).toThrow()

2 participants