Область применения в библиотеке реагирования-тестирования - PullRequest
0 голосов
/ 11 июля 2019

У меня есть этот тест, который проходит:

describe("full app rendering/navigating", () => {
  const { container, getByText } = renderWithRouter(
    <RecipeIndexPage recipes={recipes} />
  );
  const cookiesLink = getByText(/cookies/i);

  it("should render the index page with the list of recipes", () => {
    expect(container.innerHTML).toMatch("Recipes");
    expect(container.innerHTML).toContain("Chocolate Chip Cookies");
  });
  it("should navigate to a recipe", () => {
    expect(cookiesLink).toBeDefined();
  });
});

Однако, когда я объявляю cookiesLink внутри второго блока. it, он не может найти этот текст:

  const { container, getByText } = renderWithRouter(
    <RecipeIndexPage recipes={recipes} />
  );

  it("should render the index page with the list of recipes", () => {
    expect(container.innerHTML).toMatch("Recipes");
    expect(container.innerHTML).toContain("Chocolate Chip Cookies");
  });
  it("should navigate to a recipe", () => {
    const cookiesLink = getByText(/cookies/i);
    expect(cookiesLink).toBeDefined();
  });
});

-----
FAIL:

    Unable to find an element with the text: /cookies/i. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

    <body />

      37 |   });
      38 |   it("should navigate to a recipe", () => {
    > 39 |     const cookiesLink = getByText(/cookies/i);
         |                         ^
      40 |     expect(cookiesLink).toBeDefined();
      41 |   });

Почему это не работает, когда я так пишу?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...