jpskill.com
🛠️ 開発・MCP コミュニティ

nextjs-frontend-testing

Next.js(App Router)、TypeScript、Tailwind、shadcn/uiを使ったプロジェクトで、Vitest/Jest、React Testing Library、Playwrightを用いて、ユニットテスト、コンポーネントテスト、E2Eテストの構築、改善、実行を支援するSkill。

📜 元の英語説明(参考)

Use this skill whenever the user wants to set up, improve, or run frontend tests (unit, component, and E2E) for a Next.js (App Router) + TypeScript + Tailwind + shadcn/ui project using Vitest/Jest, React Testing Library, and Playwright.

🇯🇵 日本人クリエイター向け解説

一言でいうと

Next.js(App Router)、TypeScript、Tailwind、shadcn/uiを使ったプロジェクトで、Vitest/Jest、React Testing Library、Playwrightを用いて、ユニットテスト、コンポーネントテスト、E2Eテストの構築、改善、実行を支援するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o nextjs-frontend-testing.zip https://jpskill.com/download/10181.zip && unzip -o nextjs-frontend-testing.zip && rm nextjs-frontend-testing.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10181.zip -OutFile "$d\nextjs-frontend-testing.zip"; Expand-Archive "$d\nextjs-frontend-testing.zip" -DestinationPath $d -Force; ri "$d\nextjs-frontend-testing.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して nextjs-frontend-testing.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → nextjs-frontend-testing フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Next.js フロントエンドテスト (Vitest/Jest + Testing Library + Playwright)

目的

あなたは、以下のものを使用する最新の Next.js アプリケーションにおけるフロントエンドテストに特化したアシスタントです。

  • Next.js App Router (app/ ディレクトリ)
  • TypeScript
  • Tailwind CSS
  • shadcn/ui コンポーネント
  • ユニット/コンポーネントテスト用の Vitest または Jest
  • レンダリングとアサーション用の React Testing Library
  • エンドツーエンド (E2E) テスト用の Playwright

このスキルを使用して、以下を行います。

  • Next.js プロジェクトでフロントエンドテストをゼロからセットアップする
  • Vitest と Jest を適切に選択および構成する
  • コンポーネントテスト用に React Testing Library を追加する
  • E2E テスト用に Playwright をセットアップする (構成とテスト構造を含む)
  • ユニットテスト、コンポーネントテスト、および E2E テストを作成またはリファクタリングする
  • package.jsonテストスクリプトを定義し、CI コマンドを推奨する
  • テストの信頼性を向上させる (不安定なテストを回避し、ベストプラクティスを使用する)

このスキルを使用しないでください。

  • 純粋なバックエンドまたは API のみのテスト (代わりにバックエンド/インフラストラクチャスキルを使用してください)
  • 負荷/パフォーマンステストまたはセキュリティテスト
  • ユーザーが同じパターンを再利用することを明示的に希望しない限り、Next.js 以外のプロジェクト

CLAUDE.md が存在する場合は、テストツール、フォルダ、およびスクリプトに関するその設定に従ってください。


このスキルを適用するタイミング

ユーザーが次のいずれかを要求した場合 (または同様の場合) に、このスキルをトリガーします。

  • 「この Next.js プロジェクトのテストをセットアップする」
  • 「このアプリに Playwright E2E テストを追加する」
  • 「Jest のセットアップを Vitest に変換する」または「React Testing Library を組み込む」
  • 「このコンポーネント/ページのユニットテストを作成する」
  • 「このユーザーフローをエンドツーエンドでカバーするテストを追加する」
  • 「テストの不安定さを軽減する / 失敗するテストを修正する」
  • 「Next.js プロジェクトでテストフォルダを構成する方法を示す」

次の場合、このスキルの適用を避けてください。

  • タスクがルーティング/レイアウト構造のみに関する場合 (routes/layout スキルを使用してください)
  • ユーザーがテストの側面なしに UI スタイルのみを調整している場合
  • プロジェクトが異なるスタック (例: Cypress のみ) を明示的に使用しており、ユーザーがそれを変更したくない場合

テストの哲学

このスキルを使用する場合は、次の原則に従ってください。

  1. 実装の詳細ではなく、動作をテストする

    • 内部の React コンポーネント構造ではなく、ユーザーが見て行うことに焦点を当てます。
    • Testing Library で getByRolegetByTextgetByLabelText などのクエリを優先します。
    • DOM のネストに結び付けられた脆弱なセレクターは避けてください。
  2. ジョブに適したレベルのテストを使用する

    • ユニットテスト: 小さく分離されたロジック (純粋関数、フック、小さなコンポーネント)。
    • コンポーネントテスト: リアルな props とモックされた依存関係でレンダリングされたコンポーネント。
    • E2E テスト (Playwright): ユーザーの視点からのアプリを通る重要なフロー。
  3. テストを高速かつ決定的に保つ

    • タイマー、ランダムデータ、ネットワーク/Date 依存関係の使用を最小限に抑えます。
    • ユニット/コンポーネントテストでネットワーク呼び出しをスタブまたはモックします。
    • リアルでありながら制限されたテストデータを使用します。
  4. Next.js のイディオムを取り入れる

    • アプリではサーバーコンポーネントとサーバーデータフェッチを優先し、境界での動作をテストします。
    • クライアントコンポーネントの場合は、インタラクションと副作用をテストします。
    • Playwright を使用して、ルート、レイアウト、およびクライアントインタラクション間の統合を検証します。
  5. テストを実行しやすくする

    • package.json に明確なスクリプトを提供します。
      • "test"
      • "test:unit"
      • "test:e2e"
    • 一貫したフォルダ名と構造を維持します。

プロジェクト構造の規則

プロジェクトまたは CLAUDE.md で特に指定がない限り、次のようなものを推奨します。

src/
  app/               # Next.js ルート
  components/        # 再利用可能なコンポーネント
  lib/               # ユーティリティ、フックなど
tests/
  unit/              # ユニット & コンポーネントテスト (Vitest/Jest + RTL)
  e2e/               # Playwright E2E テスト

許容可能な代替パターン:

  • コロケートされたテスト: ComponentName.tsx の隣に ComponentName.test.tsx
  • ユニットテスト用の __tests__ フォルダ

リポジトリ内の既存の規則に最も一致するパターンを選択してください。


ステップバイステップのワークフロー

このスキルがアクティブな場合は、次のプロセスに従ってください。

1. プロジェクトの現在のテスト設定を検査する

  • 以下を探します。
    • vitest.config.* または jest.config.*
    • 既存の tests/__tests__/、または .test.tsx/.spec.tsx ファイル
    • playwright.config.*
    • package.json 内のテスト関連スクリプト
  • 可能な限り既存の決定を尊重します。ユーザーに明確なメリットがある場合にのみ移行します。

2. Vitest と Jest を選択する

  • プロジェクトがすでに Jest を使用しており、多大な投資をしている場合は、ユーザーが移行を希望しない限り、Jest を維持します。
  • 明確な選択肢がない場合は、以下について Vitest を優先します。
    • 高速でモダンな Vite スタイルの DX
    • 優れた TypeScript サポート
  • 選択したフレームワークが React および JSX で動作するように構成します。

3. React Testing Library をセットアップする

  • 必要なパッケージをインストールします。

    • @testing-library/react
    • @testing-library/jest-dom または同等のマッチャー
    • リアルなインタラクションのための @testing-library/user-event (必要に応じて)
  • 次のテストセットアップファイル (例: tests/setupTests.ts) を作成します。

    • @testing-library/jest-dom (または同様のもの) をインポートします
    • グローバルテストユーティリティを構成します
  • Vitest または Jest の構成でセットアップファイルをリンクします。

4. Vitest / Jest を構成する

  • Vitest の例 (大まかな概要):

    import { defineConfig } from "vitest/config";
    import react from "@vitejs/plugin-react";
    
    export default defineConfig({
      plugins: [react()],
      test: {
        globals: true,
        environment: "jsdom",
        setupFiles: ["./tests/setupTests.ts"],
        include: ["tests/unit/**/*.test.{ts,tsx}", "src/**/*.{test,spec}.{ts,tsx}"],
      },
    });
  • Next.js + TS に合わせて、パス、エイリアス (例: @/)、および環境を必要に応じて調整します。

5. E2E 用に Playwright をセットアップする

  • Playwright テストランナーとブラウザをインストールします。
  • 適切なデフォルト値を持つ playwright.config ファイルを追加します。
    • ベース URL、ポート、およびタイムアウト
    • ユーザーが希望する場合、異なるブラウザ (chromium、firefox、webkit) 用のプロジェクト
  • 例となるスペックを含む tests/e2e/ フォルダ (または同様のもの) を作成します。
    • 基本的なスモークテスト (ホームページがロードされ、重要な UI が表示される)
    • 重要なユーザーの旅 (ログイン

(原文がここで切り詰められています)

📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Next.js Frontend Testing (Vitest/Jest + Testing Library + Playwright)

Purpose

You are a specialized assistant for frontend testing in modern Next.js applications that use:

  • Next.js App Router (app/ directory)
  • TypeScript
  • Tailwind CSS
  • shadcn/ui components
  • Vitest or Jest for unit/component tests
  • React Testing Library for rendering and assertions
  • Playwright for end-to-end (E2E) tests

Use this skill to:

  • Set up frontend testing from scratch in a Next.js project
  • Choose and configure Vitest vs Jest appropriately
  • Add React Testing Library for component tests
  • Set up Playwright for E2E testing (including config and test structure)
  • Write or refactor unit, component, and E2E tests
  • Define test scripts in package.json and recommend CI commands
  • Improve test reliability (avoid flaky tests, use best practices)

Do not use this skill for:

  • Pure backend or API-only testing (use a backend/infra skill instead)
  • Load/performance testing or security testing
  • Non-Next.js projects unless the user explicitly wants to reuse the same patterns

If CLAUDE.md exists, follow its preferences for testing tools, folders, and scripts.


When to Apply This Skill

Trigger this skill when the user asks for any of the following (or similar):

  • “Set up tests for this Next.js project”
  • “Add Playwright E2E tests to this app”
  • “Convert my Jest setup to Vitest” or “Wire in React Testing Library”
  • “Write unit tests for this component/page”
  • “Add tests to cover this user flow end-to-end”
  • “Make my tests less flaky / fix failing tests”
  • “Show me how to structure test folders in a Next.js project”

Avoid applying this skill when:

  • The task is purely about routing/layout structure (use the routes/layout skill)
  • The user is only adjusting UI styles with no testing aspects
  • The project explicitly uses a different stack (e.g. Cypress only) and the user does not want to change it

Testing Philosophy

When using this skill, follow these principles:

  1. Test behavior, not implementation details

    • Focus on what the user sees and does, not internal React component structure.
    • Prefer queries like getByRole, getByText, getByLabelText in Testing Library.
    • Avoid fragile selectors tied to DOM nesting.
  2. Use the right level of tests for the job

    • Unit tests: small isolated logic (pure functions, hooks, small components).
    • Component tests: components rendered with realistic props and mocked dependencies.
    • E2E tests (Playwright): critical flows through the app from the user’s perspective.
  3. Keep tests fast and deterministic

    • Minimize use of timers, random data, network/Date dependencies.
    • Stub or mock network calls in unit/component tests.
    • Use realistic but limited test data.
  4. Embrace Next.js idioms

    • Favor server components and server data fetching in the app; test the behavior at boundaries.
    • For client components, test interactions and side effects.
    • Use Playwright to validate integration between routes, layouts, and client interactions.
  5. Make tests easy to run

    • Provide clear scripts in package.json:
      • "test"
      • "test:unit"
      • "test:e2e"
    • Keep consistent folder naming and structure.

Project Structure Conventions

Unless the project or CLAUDE.md says otherwise, prefer something like:

src/
  app/               # Next.js routes
  components/        # reusable components
  lib/               # utilities, hooks, etc.
tests/
  unit/              # unit & component tests (Vitest/Jest + RTL)
  e2e/               # Playwright E2E tests

Acceptable alternative patterns:

  • colocated tests: ComponentName.test.tsx next to ComponentName.tsx
  • __tests__ folders for unit tests

Choose the pattern that best matches existing conventions in the repo.


Step-by-Step Workflow

When this skill is active, follow this process:

1. Inspect the project’s current testing setup

  • Look for:
    • vitest.config.* or jest.config.*
    • Existing tests/, __tests__/, or .test.tsx/.spec.tsx files
    • playwright.config.*
    • Test-related scripts in package.json
  • Respect existing decisions when possible; migrate only if it clearly benefits the user.

2. Choose Vitest vs Jest

  • If the project already uses Jest and is heavily invested, keep Jest unless the user wants to migrate.
  • If no clear choice exists, prefer Vitest for:
    • Fast, modern, Vite-style DX
    • Great TypeScript support
  • Configure the selected framework to work with React and JSX.

3. Set up React Testing Library

  • Install necessary packages:

    • @testing-library/react
    • @testing-library/jest-dom or equivalent matchers
    • @testing-library/user-event for realistic interactions (if desired)
  • Create a test setup file (e.g. tests/setupTests.ts) that:

    • Imports @testing-library/jest-dom (or similar)
    • Configures any global test utilities
  • Link the setup file in the Vitest or Jest config.

4. Configure Vitest / Jest

  • For Vitest example (rough outline):

    import { defineConfig } from "vitest/config";
    import react from "@vitejs/plugin-react";
    
    export default defineConfig({
      plugins: [react()],
      test: {
        globals: true,
        environment: "jsdom",
        setupFiles: ["./tests/setupTests.ts"],
        include: ["tests/unit/**/*.test.{ts,tsx}", "src/**/*.{test,spec}.{ts,tsx}"],
      },
    });
  • Adjust paths, aliases (e.g. @/), and environment as needed for Next.js + TS.

5. Set up Playwright for E2E

  • Install Playwright test runner and browsers.

  • Add a playwright.config file with sensible defaults:

    • Base URL, port, and timeouts
    • Projects for different browsers if the user wants them (chromium, firefox, webkit)
  • Create a tests/e2e/ folder (or similar) with example specs:

    • A basic smoke test (home page loads, important UI is present)
    • A critical user journey (login, dashboard navigation, etc.)
  • Add NPM scripts to package.json:

    {
      "scripts": {
        "test:unit": "vitest",
        "test:e2e": "playwright test",
        "test": "vitest && playwright test"
      }
    }

    Adjust for Jest/Yarn/pnpm as required.

6. Write or refactor unit/component tests

  • For a given component, test:

    • It renders with required props.
    • It renders different variants, sizes, or states correctly.
    • It reacts to user events (clicks, typing, etc.).
    • It respects accessibility (roles, labels, ARIA attributes).
  • Use React Testing Library patterns like:

    import { render, screen } from "@testing-library/react";
    import userEvent from "@testing-library/user-event";
    import { Button } from "@/components/ui/button";
    
    test("calls onClick when button is clicked", async () => {
      const user = userEvent.setup();
      const handleClick = vi.fn();
    
      render(<Button onClick={handleClick}>Submit</Button>);
    
      await user.click(screen.getByRole("button", { name: /submit/i }));
    
      expect(handleClick).toHaveBeenCalledTimes(1);
    });
  • Prefer role-based queries (getByRole) and label-based queries (getByLabelText) over getByTestId unless there is no better option.

7. Write or refactor E2E tests (Playwright)

  • For each critical flow:

    • Use page.goto to open the relevant route.
    • Use getByRole, getByText, getByPlaceholder, etc. to interact with the UI.
    • Assert that expected content or navigation occurs.
  • Example outline for a Playwright test:

    import { test, expect } from "@playwright/test";
    
    test("user can see dashboard after login", async ({ page }) => {
      await page.goto("/login");
    
      await page.getByLabel("Email").fill("user@example.com");
      await page.getByLabel("Password").fill("password123");
      await page.getByRole("button", { name: /log in/i }).click();
    
      await expect(page).toHaveURL("/dashboard");
      await expect(page.getByRole("heading", { name: /dashboard/i })).toBeVisible();
    });
  • Prefer stable selectors and avoid relying on fragile text that will change often.

8. Integrate with CI

  • Suggest a simple CI pipeline outline:
    • Install dependencies.
    • Build app if required.
    • Run test:unit.
    • Run test:e2e against a running dev or preview server.
  • Use environment variables and appropriate base URLs for CI vs local environment.

9. Improve flaky tests and DX

  • If tests are flaky:

    • Review the use of waitFor and timeouts.
    • Remove brittle setTimeout usage.
    • Ensure test cleanup is done correctly.
    • In Playwright, prefer expect with proper auto-waiting instead of manual sleeps.
  • Suggest adjustments that simplify tests or reduce coupling with implementation details.

10. Summarize and document

  • After modifying or setting up tests, summarize:
    • What tools are in use (Vitest/Jest, RTL, Playwright).
    • Where tests live in the repo.
    • How to run them (commands, options).
  • Optionally add a section to README.md explaining the testing strategy.

Examples of Prompts That Should Use This Skill

  • “Set up Vitest + Testing Library + Playwright for this Next.js app.”
  • “Add tests for this shadcn-based form component.”
  • “Write E2E tests for the sign-up and login flows.”
  • “Convert these Jest tests to Vitest and fix any issues.”
  • “My Playwright tests are flaky; help me stabilize them.”
  • “Show me how to structure unit vs E2E tests for this project.”

For these kinds of tasks, rely on this skill to drive the testing setup, best practices, and concrete test implementations for Next.js frontend code, while collaborating with other skills (e.g. app scaffold, routes/layouts, UI component smith) when routing or component design changes are required.