Skip to content

valid-expect should handle expect.poll and expect.element as Vitest expect APIs #922

Description

@nami8824

Summary

valid-expect currently appears to treat expect.poll(...) and expect.element(...) as if poll / element were matchers.

However, in Vitest, these are expect APIs that start an assertion chain:

expect.poll(() => value).toBe(1)
expect.element(element).toBeInTheDocument()

In these chains, poll and element are not matchers. The actual matchers are toBe and toBeInTheDocument.

This can cause valid-expect to miss invalid expect.poll / expect.element usages.

Reproduction

Add the following cases to the invalid section of tests/valid-expect.test.ts:

{
  code: 'expect.poll(() => value);',
  errors: [{ messageId: 'matcherNotFound' }],
},
{
  code: 'expect.element(element);',
  errors: [{ messageId: 'matcherNotFound' }],
},

Then run:

pnpm test tests/valid-expect.test.ts

Currently, these cases fail because valid-expect reports no errors:

FAIL  tests/valid-expect.test.ts > valid-expect > invalid > expect.poll(() => value);
FAIL  tests/valid-expect.test.ts > valid-expect > invalid > expect.element(element);

AssertionError: Should have 1 error but had 0: []

0 !== 1

For comparison, the equivalent plain expect case is already reported correctly:

{
  code: 'expect("something");',
  errors: [{ messageId: 'matcherNotFound' }],
},

Expected behavior

valid-expect should treat expect.poll(...) and expect.element(...) as expect APIs, not as matchers.

For example:

expect.poll(() => value)
expect.element(element)

should be reported as matcherNotFound.

And these should remain valid:

await expect.poll(() => value).toBe(1)
await expect.element(element).toBeInTheDocument()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions