Skip to content

afterEach(cleanup); isn't cleaning up after each test #368

Closed
@TidyIQ

Description

@TidyIQ
  • react-testing-library version: 7.0.0
  • react version: 16.8.6
  • node version: 10.15.3
  • npm (or yarn) version: 6.9.0

Relevant code or config:

afterEach(cleanup);

describe("Unit: <OutlinedInput />", (): void => {
  describe.each`
    data                 | showIcon | name                                     | validInput
    ${nameFieldData}     | ${false} | ${"text fields"}                         | ${"John Smith"}
    ${nameFieldData}     | ${true}  | ${"text fields with icon adornment"}     | ${"John Smith"}
    ${passwordFieldData} | ${false} | ${"password fields"}                     | ${"1234567a"}
    ${passwordFieldData} | ${true}  | ${"password fields with icon adornment"} | ${"1234567a"}
    ${emailFieldData}    | ${false} | ${"email fields"}                        | ${"me@domain.com"}
    ${emailFieldData}    | ${true}  | ${"email fields with icon adornment"}    | ${"me@domain.com"}
  `(
    "Initial render",
    ({ data, showIcon, name, validInput }): void => {
      it(`renders as snapshot for ${name}`, (): void => {
        const { asFragment } = render(
          <OutlinedInput fieldData={data} showIcon={showIcon} />
        );
        expect(asFragment()).toMatchSnapshot();
      });

      describe("THEN focus in", (): void => {
        it(`renders as snapshot for ${name}`, (): void => {
          const { asFragment, getByLabelText } = render(
            <OutlinedInput fieldData={data} showIcon={showIcon} />
          );
          const input = getByLabelText(data.label);
          fireEvent.focus(input);
          expect(asFragment()).toMatchSnapshot();
        });
      });

      describe("THEN valid input", (): void => {
        it(`renders as snapshot for ${name}`, (): void => {
          const { asFragment, getByLabelText } = render(
            <OutlinedInput fieldData={data} showIcon={showIcon} />
          );
          const input = getByLabelText(data.label);
          fireEvent.focus(input);
          fireEvent.change(input, { target: { value: validInput } });
          expect(asFragment()).toMatchSnapshot();
        });
      });
    }
  );
});

Notice how I have afterEach(cleanup); declared at the top of the file.

When I run the test, the snapshots created for the first test Unit: <OutlinedInput /> Initial render renders as snapshot for ${name} with icon adornment all include the validInput that is declared in the describe.each() at the top. Only the with adornment tests include the validinput, not the ones without.

This is a bug.

I know this because nowhere in that first snapshot test do I run fireEvent.change(input, { target: { value: validInput } });. That fireEvent is only run in the last snapshot test, and that is the only place that the input value can be changed.

If I change the values of validInput, e.g. to "John Brown", then the snapshot shows this new value, so I know it's getting the value from the describe.each() and not anywhere else in my code.

Also, it only occurs on every second test in the describe.each() (the ones where showIcon === true).

Here are two snapshots that show the input value is incorrectly being added for this test:

exports[`Unit: <OutlinedInput /> Initial render renders as snapshot for text fields 2`] = `
<DocumentFragment>
  <div
    class="MuiFormControl-root MuiFormControl-marginDense MuiFormControl-fullWidth"
  >
    <label
      class="MuiFormLabel-root MuiInputLabel-root MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-marginDense MuiInputLabel-outlined makeStyles-inputLabel-1"
      data-shrink="false"
      for="name"
    >
      Name Field Label
    </label>
    <div
      class="MuiInputBase-root MuiOutlinedInput-root MuiInputBase-formControl MuiInputBase-marginDense MuiInputBase-adornedStart MuiOutlinedInput-adornedStart"
      data-testid="outlinedInputInput"
    >
      <fieldset
        aria-hidden="true"
        class="PrivateNotchedOutline-root-61 MuiOutlinedInput-notchedOutline makeStyles-notchedOutline-6"
        style="padding-left: 8px;"
      >
        <legend
          class="PrivateNotchedOutline-legend-62"
          style="width: 0.01px;"
        >
          <span>
            ​
          </span>
        </legend>
      </fieldset>
      <svg
        aria-hidden="true"
        class="MuiSvgIcon-root"
        data-testid="icon"
        focusable="false"
        role="presentation"
        viewBox="0 0 24 24"
      >
        <path
          d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"
        />
        <path
          d="M0 0h24v24H0z"
          fill="none"
        />
      </svg>
      <hr
        class="MuiDivider-root makeStyles-divider-63"
      />
      <input
        aria-invalid="false"
        class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputMarginDense MuiOutlinedInput-inputMarginDense MuiInputBase-inputAdornedStart MuiOutlinedInput-inputAdornedStart"
        id="name"
        minlength="1"
        pattern="^(?!\\\\s*$).+"
        required=""
        type="text"
        value=""     //  ***** NO ERROR HERE FOR THIS SNAPSHOT *****
      />
    </div>
    <div
      class="MuiCollapse-container MuiCollapse-hidden"
      style="min-height: 0px;"
    >
      <div
        class="MuiCollapse-wrapper"
      >
        <div
          class="MuiCollapse-wrapperInner"
        >
          <p
            class="MuiFormHelperText-root MuiFormHelperText-contained MuiFormHelperText-marginDense"
            data-testid="outlinedInputHelperText"
          />
        </div>
      </div>
    </div>
  </div>
</DocumentFragment>
`;
exports[`Unit: <OutlinedInput /> Initial render renders as snapshot for text fields with icon adornment 2`] = `
<DocumentFragment>
  <div
    class="MuiFormControl-root MuiFormControl-marginDense MuiFormControl-fullWidth"
  >
    <label
      class="MuiFormLabel-root MuiFormLabel-filled MuiInputLabel-root makeStyles-labelValid-722 MuiInputLabel-formControl MuiInputLabel-animated MuiInputLabel-shrink MuiInputLabel-marginDense MuiInputLabel-outlined makeStyles-inputLabel-721"
      data-shrink="true"
      for="name"
    >
      Name Field Label
    </label>
    <div
      class="MuiInputBase-root MuiOutlinedInput-root makeStyles-notchedValid-725 MuiInputBase-formControl MuiInputBase-marginDense MuiInputBase-adornedStart MuiOutlinedInput-adornedStart"
      data-testid="outlinedInputInput"
    >
      <fieldset
        aria-hidden="true"
        class="PrivateNotchedOutline-root-781 MuiOutlinedInput-notchedOutline makeStyles-notchedOutline-726"
        style="padding-left: 8px;"
      >
        <legend
          class="PrivateNotchedOutline-legend-782"
          style="width: 0px;"
        >
          <span>
            ​
          </span>
        </legend>
      </fieldset>
      <svg
        aria-hidden="true"
        class="MuiSvgIcon-root makeStyles-validColor-723"
        data-testid="icon"
        focusable="false"
        role="presentation"
        viewBox="0 0 24 24"
      >
        <path
          d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"
        />
        <path
          d="M0 0h24v24H0z"
          fill="none"
        />
      </svg>
      <hr
        class="MuiDivider-root makeStyles-divider-783"
      />
      <input
        aria-invalid="false"
        class="MuiInputBase-input MuiOutlinedInput-input MuiInputBase-inputMarginDense MuiOutlinedInput-inputMarginDense MuiInputBase-inputAdornedStart MuiOutlinedInput-inputAdornedStart"
        id="name"
        minlength="1"
        pattern="^(?!\\\\s*$).+"
        required=""
        type="text"
        value="John Smith"    //  ***** ERROR OCCURS HERE. THIS SHOULD NOT BE HERE *****
      />
    </div>
    <div
      class="MuiCollapse-container MuiCollapse-hidden"
      style="min-height: 0px;"
    >
      <div
        class="MuiCollapse-wrapper"
      >
        <div
          class="MuiCollapse-wrapperInner"
        >
          <p
            class="MuiFormHelperText-root MuiFormHelperText-contained MuiFormHelperText-marginDense MuiFormHelperText-filled"
            data-testid="outlinedInputHelperText"
          />
        </div>
      </div>
    </div>
  </div>
</DocumentFragment>
`;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions