Skip to content

Latest commit

 

History

History
73 lines (53 loc) · 1.66 KB

File metadata and controls

73 lines (53 loc) · 1.66 KB

vitest/no-conditional-expect

📝 Disallow conditional expects.

💼⚠️ This rule is enabled in the ✅ recommended config. This rule warns in the 🌐 all config.

Rule Details

This rule aims to prevent false positive test results by highlighting conditional expect statements.

Examples of incorrect code for this rule:

test('foo', () => {
  if (false) {
    expect(1).toBe(1)
  }
})

test.for([null, { bar: 'baz' }])('quux', (value) => {
  const expected = value === null ? expected : expect.stringContaining(expected)
  expect(actual).toEqual(expected)
})

Examples of correct code for this rule:

test('foo', () => {
  expect(1).toBe(1)
})

test.for([null, expect.objectContaining({ bar: 'baz' })])(
  'quux',
  (expected) => {
    expect(actual).toEqual(expected)
  },
)

Options

Name Description Type
expectAssertions Enable/disable whether expect.assertions() is taken into account Boolean

expectAssertions

{
  "rules": {
    "vitest/no-conditional-expect": ["error", { "expectAssertions": true }]
  }
}

Enable/disable whether to take the usage of expect.assertions() into account. Setting this to true will allow conditional expressions only if a call to expect.assertions() is also made.

test('foo', () => {
  expect.assertions(1)

  if (true) {
    expect(1).toBe(1)
  }
})