Archived
1
0
This repository has been archived on 2023-05-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
home-page/node_modules/eslint-plugin-jest/docs/rules/no-try-expect.md
Sean Edmunds 0584430b5c First commit
2021-12-13 17:00:40 -06:00

1.3 KiB

Prevent catch assertions in tests (no-try-expect)

Deprecated

This rule has been deprecated in favor of no-conditional-expect.


This rule prevents the use of expect inside catch blocks.

Rule Details

Expectations inside a catch block can be silently skipped. While Jest provides an expect.assertions(number) helper, it might be cumbersome to add this to every single test. Using toThrow concisely guarantees that an exception was thrown, and that its contents match expectations.

The following patterns are warnings:

it('foo', () => {
  try {
    foo(); // `foo` may be refactored to not throw exceptions, yet still appears to be tested here.
  } catch (err) {
    expect(err).toMatch(/foo error/);
  }
});

it('bar', async () => {
  try {
    await foo();
  } catch (err) {
    expect(err).toMatch(/foo error/);
  }
});

it('baz', async () => {
  try {
    await foo();
  } catch (err) {
    expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' });
  }
});

The following patterns are not warnings:

it('foo', () => {
  expect(() => foo()).toThrow(/foo error/);
});

it('bar', async () => {
  await expect(fooPromise).rejects.toThrow(/foo error/);
});

it('baz', async () => {
  await expect(() => foo()).rejects.toThrow(
    expect.objectContaining({ code: 'MODULE_NOT_FOUND' }),
  );
});