3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vitestでdata-testidで取得した要素の文字列だけを期待したい

3
Posted at

はじめに

  • それらしいマッチャがなかった

問題

  • domを厳密に比較してしまい落ちる
import { render } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { screen } from "@testing-library/react";
import App from "../App.tsx";
import { Provider } from "../components/ui/provider.tsx";

Object.defineProperty(window, "matchMedia", {
  writable: true,
  value: vi.fn().mockImplementation((query) => ({
    matches: false,
    media: query,
    onchange: null,
    addListener: vi.fn(),
    removeListener: vi.fn(),
    addEventListener: vi.fn(),
    removeEventListener: vi.fn(),
    dispatchEvent: vi.fn(),
  })),
});

describe("App", () => {
  it("タイトルがあること", () => {
    render(
      <Provider>
        <App />
      </Provider>,
    );
    const title = screen.getByTestId("title");
    expect(title).toContain("タイトル");
  });
});

解決

    expect(title).toContain("タイトル");

を

    expect(title.textContent).toContain("タイトル");

にして解決

終わりに

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?