Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 1.7 KB

no-wait-for-snapshot.md

File metadata and controls

56 lines (42 loc) · 1.7 KB

Ensures no snapshot is generated inside a waitFor call (testing-library/no-wait-for-snapshot)

Ensure that no calls to toMatchSnapshot or toMatchInlineSnapshot are made from within a waitFor method (or any of the other async utility methods).

Rule Details

The waitFor() method runs in a timer loop. So it'll retry every n amount of time. If a snapshot is generated inside the wait condition, jest will generate one snapshot per each loop.

The problem then is the amount of loop ran until the condition is met will vary between different computers (or CI machines). This leads to tests that will regenerate a lot of snapshots until the condition is matched when devs run those tests locally updating the snapshots; e.g. devs cannot run jest -u locally, or it'll generate a lot of invalid snapshots which will fail during CI.

Note that this lint rule prevents from generating a snapshot from within any of the async utility methods.

Examples of incorrect code for this rule:

const foo = async () => {
  // ...
  await waitFor(() => expect(container).toMatchSnapshot());
  // ...
};

const bar = async () => {
  // ...
  await waitFor(() => expect(container).toMatchInlineSnapshot());
  // ...
};

const baz = async () => {
  // ...
  await wait(() => {
    expect(container).toMatchSnapshot();
  });
  // ...
};

Examples of correct code for this rule:

const foo = () => {
  // ...
  expect(container).toMatchSnapshot();
  // ...
};

const bar = () => {
  // ...
  expect(container).toMatchInlineSnapshot();
  // ...
};

Further Reading