Purpose
This skill equips the AI to assist with unit testing using frameworks like Jest/Vitest for JavaScript, pytest for Python, Go's testify, RSpec for Ruby, XCTest for Swift, and JUnit5 for Java. It focuses on mocking dependencies, creating stubs, and ensuring test isolation to verify code behavior in controlled environments.
When to Use
Use this skill when writing new unit tests, debugging existing ones, or refactoring code that requires isolated testing. Apply it for TDD workflows, mocking external APIs or databases, or ensuring functions work independently. For example, use it when a function depends on a network call—mock it to test locally without real dependencies.
Key Capabilities
- Mocking: Create mocks for functions or modules, e.g., in Jest with to replace real implementations.
- Stubbing: Define stub responses, like in pytest using to fake object methods.
- Isolation: Run tests in isolation, such as using Go's testify to assert without side effects, or JUnit5's for isolated file operations.
- Framework-Specific: Support for Jest/Vitest assertions (e.g., ), pytest fixtures for setup/teardown, RSpec's for mocks, XCTest's for isolation, and JUnit5's annotations.
- Config Formats: Use Jest's with
{ testEnvironment: 'node', setupFilesAfterEnv: ['<rootDir>/setupTests.js'] }
for custom mocks; pytest's with for verbose output.
Usage Patterns
To accomplish unit testing tasks, follow these steps: 1) Identify dependencies to mock (e.g., HTTP calls). 2) Set up mocks or stubs in your test file. 3) Write assertions to verify outputs. 4) Run tests with appropriate flags. For TDD, write a failing test first, then implement code. In Jest, structure tests with
blocks; in pytest, use fixtures for shared setup. Always isolate tests by avoiding global state—use
in Jest or
@pytest.fixture(scope='function')
in pytest.
Common Commands/API
- Jest/Vitest: Run tests with for interactive mode; mock modules via
jest.mock('axios', () => ({ get: jest.fn().mockResolvedValue({ data: {} }) }));
. API: Use expect(value).toEqual(expected)
for assertions.
- pytest: Execute with for coverage; stub functions with
def test_function(monkeypatch): monkeypatch.setattr('module.func', lambda: 'stubbed')
. API: Leverage for isolation, e.g., def mock_db(): return {'query': lambda: []}
.
- Go (testify): Build with and use from testify; e.g.,
mockCtrl := gomock.NewController(t); mockObj := NewMockInterface(mockCtrl); mockObj.EXPECT().Method().Return(value)
.
- RSpec: Run via ; create doubles with
double('Object', method: -> 'stubbed')
. API: Use expect { code }.to change { something }
.
- XCTest: Compile and run with ; mock in Swift using stubs, e.g.,
class MockService: ServiceProtocol { func fetchData() -> Data { return Data() } }
.
- JUnit5: Execute via ; use Mockito with
@ExtendWith(MockitoExtension.class) public class TestClass { @Mock private Dependency dep; @InjectMocks private ClassUnderTest cut; }
. If integrating external services, set env vars like for authentication in tests.
Integration Notes
Integrate this skill by embedding unit tests into CI/CD pipelines, e.g., add Jest to a GitHub Actions workflow with
. For multi-language projects, use a monorepo setup with tools like Nx for Jest and pytest. Configure environment variables for secrets, e.g., export
in your test runner. Link with code coverage tools like Istanbul for Jest or Coverage.py for pytest; ensure mocks respect these by using
flags when debugging. For API-dependent tests, inject env vars like
into your test command, e.g.,
pytest --api-key=$SERVICE_API_KEY
.
Error Handling
Handle common errors prescriptively: For assertion failures in Jest, check stack traces and use
jest.fn().mockImplementation()
to debug mocks; if a mock isn't called, add
assertions. In pytest, catch fixture errors by wrapping in try/except, e.g.,
def test_with_fixture(fix): try: fix.setup() except Exception as e: pytest.fail(str(e))
. For Go, use testify's
to fail tests on errors. In RSpec, handle doubles with
allow(double).to receive(:method).and_raise(Error)
. For JUnit5, use
@Test(expected = Exception.class)
for expected failures. Always isolate error-prone code with stubs to prevent cascading failures; rerun with verbose flags like
or
for detailed output.
Concrete Usage Examples
-
Jest Mock Example: To test a function that fetches data, mock the fetch API:
jest.mock('node-fetch'); const fetch = require('node-fetch'); fetch.mockResolvedValue({ json: () => Promise.resolve({ data: 'mocked' }) }); test('fetches data', async () => { expect(await fetchData()).toEqual('mocked'); });
-
pytest Fixture Example: To isolate a database-dependent test, use a fixture:
import pytest @pytest.fixture def mock_db(): return {'query': lambda: [{'id': 1}]} def test_query(mock_db): assert mock_db['query']()[0]['id'] == 1
Graph Relationships
- Related to: testing-integration (for broader testing workflows), code-debugging (for fixing test failures)
- Clusters with: testing (as part of the testing cluster), code-execution (for running tests in isolated environments)
- Depends on: environment-setup (for managing test dependencies like mocks)