本文へスキップ
Skillpedia
テスト40k · #7262

javascript-typescript-jest

できること

Jestライブラリを使って、JavaScriptやTypeScriptのコードのテストを作成するためのツールです。テストの整理方法、外部APIなど他の部分に依存するコードの模擬(モック)、そして想定どおりに動作しているかの確認方法を学べます。

使う場面

  • ✓JavaScriptやTypeScriptでコードを書くとき。
  • ✓変更を加えたあとも、コードが正しく動作することを確かめたいとき。
  • ✓外部の依存関係を模擬して、自分のロジックだけをテストしたいとき。

スキルのリンク

https://github.com/github/awesome-copilot/blob/main/skills/javascript-typescript-jest/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/github/awesome-copilot/blob/main/skills/javascript-typescript-jest/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“外部APIを呼び出す関数をテストするにはどうすればよいですか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

このツールは、テストを特定のファイルにまとめ、「describe」や「it」といったコマンドでテストの構造を作ります。依存関係を模擬するには、「jest.mock」や「jest.spyOn」などの関数を使って偽物のバージョンを作成し、コードを他から切り離した状態でテストできるようにします。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

Test Structure

  • Name test files with .test.ts or .test.js suffix
  • Place test files next to the code they test or in a dedicated __tests__ directory
  • Use descriptive test names that explain the expected behavior
  • Use nested describe blocks to organize related tests
  • Follow the pattern: describe('Component/Function/Class', () => { it('should do something', () => {}) })

Effective Mocking

  • Mock external dependencies (APIs, databases, etc.) to isolate your tests
  • Use jest.mock() for module-level mocks
  • Use jest.spyOn() for specific function mocks
  • Use mockImplementation() or mockReturnValue() to define mock behavior
  • Reset mocks between tests with jest.resetAllMocks() in afterEach

Testing Async Code

  • Always return promises or use async/await syntax in tests
  • Use resolves/rejects matchers for promises
  • Set appropriate timeouts for slow tests with jest.setTimeout()

Snapshot Testing

  • Use snapshot tests for UI components or complex objects that change infrequently
  • Keep snapshots small and focused
  • Review snapshot changes carefully before committing

Testing React Components

  • Use React Testing Library over Enzyme for testing components
  • Test user behavior and component accessibility
  • Query elements by accessibility roles, labels, or text content
  • Use userEvent over fireEvent for more realistic user interactions

Common Jest Matchers

  • Basic: expect(value).toBe(expected), expect(value).toEqual(expected)
  • Truthiness: expect(value).toBeTruthy(), expect(value).toBeFalsy()
  • Numbers: expect(value).toBeGreaterThan(3), expect(value).toBeLessThanOrEqual(3)
  • Strings: expect(value).toMatch(/pattern/), expect(value).toContain('substring')
  • Arrays: expect(array).toContain(item), expect(array).toHaveLength(3)
  • Objects: expect(object).toHaveProperty('key', value)
  • Exceptions: expect(fn).toThrow(), expect(fn).toThrow(Error)
  • Mock functions: expect(mockFn).toHaveBeenCalled(), expect(mockFn).toHaveBeenCalledWith(arg1, arg2)
よくある質問

「モック」とは何ですか?

テスト中に使う、コードの一部(関数やモジュールなど)の偽物のことです。外部のシステムや複雑な仕組みに頼らずに、その動作を制御してテストできるようにします。

なぜテストを書くのですか?

テストは、コードが想定どおりに動いているかを確認し、特に変更を加えたときにエラーをすばやく見つけるのに役立ちます。コードが壊れていないという安心感も得られます。

「スナップショットテスト」とは何ですか?

ユーザーインターフェースや複雑なデータ構造をテストするための手法です。期待される出力の「スナップショット」を取り、以降のテストで現在の出力と比べて、予期しない変更がないかを確認します。

詳細

作者による元の説明

Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.

repo: github/awesome-copilot

人気

GitHubのスター
40k
ランキング
#7262
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル