vitest/valid-expect

March 1, 2026 ยท View on GitHub

๐Ÿ“ Enforce valid expect() usage.

๐Ÿ’ผโš ๏ธ This rule is enabled in the โœ… recommended config. This rule warns in the ๐ŸŒ all config.

๐Ÿ”ง This rule is automatically fixable by the --fix CLI option.

This rule triggers a warning if expect is called with no argument or with more than one argument. You change that behavior by setting the minArgs and maxArgs options.

Options

NameDescriptionType
alwaysAwaitRequire awaiting every async assertion.Boolean
asyncMatchersMatchers that should be considered async assertions.String[]
maxArgsMaximum number of arguments allowed for expect.Number
minArgsMinimum number of arguments allowed for expect.Number
  1. alwaysAwait
  • Type: boolean

  • Default: false

  • Enforce expect to be called with an await expression.

    // โœ… good
    await expect(Promise.resolve(1)).resolves.toBe(1)
    await expect(Promise.reject(1)).rejects.toBe(1)
    
    // โŒ bad
    expect(Promise.resolve(1)).resolves.toBe(1)
    expect(Promise.reject(1)).rejects.toBe(1)
    
  1. asyncMatchers
  • Type: string[]
  • Default: []
{
	"vitest/valid-expect": ["error", {
	  "asyncMatchers": ["toBeResolved", "toBeRejected"]
	}]
}

avoid using asyncMatchers with expect:

  1. minArgs
  • Type: number

  • Default: 1

  • Enforce expect to be called with at least minArgs arguments.

    // โœ… good
    expect(1).toBe(1)
    expect(1, 2).toBe(1)
    expect(1, 2, 3).toBe(1)
    
    // โŒ bad
    expect().toBe(1)
    expect(1).toBe()
    
  1. maxArgs
  • Type: number

  • Default: 1

  • Enforce expect to be called with at most maxArgs arguments.

  • Exception: expect(value, "message") is allowed.

    // โœ… good
    expect(1).toBe(1)
    expect(1, 'expect value to be one').toBe(1)
    const message = 'expect value to be one'
    expect(1, `Error Message: ${message}`).toBe(1)
    
    // โŒ bad
    expect(1, 2, 3, 4).toBe(1)