agentleFS
razorpayrazorpay/blade/.agents/skills/write-unit-tests/SKILL.md

write-unit-tests

Skill658 starsChanged 4 months agoMITView on GitHub

This rule helps in writing and running unit tests for components of blade design system

We split this skill into the rules it gives an agent

21 rules. 0 are common, 12 are distinctive, and 9 are very specific to this project.

  • 0

    Common

    Most of the 10 edited versions we read keep these.

  • 12

    Distinctive

    This author's own choices. Worth a look.

  • 9

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    When component is interactive, you test its core functionality

    line 10
  2. Distinctive:

    Before writing tests, you discuss which tests you're planning to write and only write tests post confirmation

    line 11
  3. Very specific:

    Create tests in a __tests__ directory within the component folder

    line 19
  4. Very specific:

    Use .web.test.tsx for web-specific tests and .ssr.test.tsx for server-side rendering tests

    line 20
  5. Very specific:

    Follow the naming pattern: ComponentName.web.test.tsx and ComponentName.ssr.test.tsx

    line 21
  6. Distinctive:

    Always start by examining existing test patterns (e.g., Box component tests) to understand established conventions

    line 25
  7. Very specific:

    Use renderWithTheme for web tests and renderWithSSR for SSR tests

    line 26
  8. Very specific:

    Import testing utilities from ~utils/testing/

    line 28
  9. Very specific:

    Always prefer getByRole over anything else wherever possible

    line 40
  10. Very specific:

    Use testID props for precise element targeting in other cases where getByRole is not possible

    line 41
  11. Distinctive:

    Keep test cases focused and well-named with clear descriptions

    line 45
  12. Distinctive:

    Avoid writing tests that can get flaky

    line 46
  13. Very specific:

    Prefer toMatchSnapshot over toMatchInlineSnapshot especially in complex snapshots

    line 47
  14. Distinctive:

    Avoid unnecessary repeatitive tests.

    line 48
  15. Distinctive:

    Test different orientations, alignments, and layout configurations

    line 52
  16. Distinctive:

    When testing computed styles, use specific pixel values that match design tokens

    line 60
  17. Distinctive:

    Test both presence and absence of styling conditions

    line 62
  18. Distinctive:

    Use this pattern for triggering any events

    line 66
  19. Distinctive:

    Test the core Logic

    line 78
  20. Distinctive:

    If there is any logic that is too complex or too flaky for unit tests, ignore it and suggest user to write integration test instead

    line 79
  21. Very specific:

    Run test using following command from packages/blade directory

    line 134

“Common” is measured: kept by at least 6 in 10 of the 10 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

You work in a design system team of Razorpay and you're good at writing unit tests. You make sure to cover all important scenarios in tests while also ensuring that you don't write too many unnecessary tests.

  • You refer to existing tests for formats and inspirations. Refer to atleast 2 similar complexity components tests
  • You refer to story docs of that component for references on realisitic examples to test
  • When component is interactive, you test its core functionality
  • Before writing tests, you discuss which tests you're planning to write and only write tests post confirmation
  • You don't write ComponentName.native.test.tsx tests since that is not maintained anymore
  • Whenever you're unsure about some practice, just ask for confirmation

Blade Component Testing Guidelines

Test File Structure

  • Create tests in a __tests__ directory within the component folder
  • Use .web.test.tsx for web-specific tests and .ssr.test.tsx for server-side rendering tests
  • Follow the naming pattern: ComponentName.web.test.tsx and ComponentName.ssr.test.tsx

Testing Patterns

  • Always start by examining existing test patterns (e.g., Box component tests) to understand established conventions
  • Use renderWithTheme for web tests and renderWithSSR for SSR tests
  • Include assertAccessible test to ensure components meet accessibility standards
  • Import testing utilities from ~utils/testing/

Testing Utilities and Imports

import renderWithTheme from '~utils/testing/renderWithTheme.web';
import renderWithSSR from '~utils/testing/renderWithSSR';
import assertAccessible from '~utils/testing/assertAccessible.web';

Element Selection Best Practices

  • Always prefer getByRole over anything else wherever possible
  • Use testID props for precise element targeting in other cases where getByRole is not possible

Code Style in Tests

  • Keep test cases focused and well-named with clear descriptions
  • Avoid writing tests that can get flaky
  • Prefer toMatchSnapshot over toMatchInlineSnapshot especially in complex snapshots
  • Avoid unnecessary repeatitive tests. Keep tests concise

Component Testing Patterns

  • Test different orientations, alignments, and layout configurations
  • For focussed testing of particular style, use getComputedStyle instead of adding snapshot for whole code
  • Include tests for conditional behavior that change the DOM structure heavily

Types of Tests

Styling/Layout Testing

  • When testing computed styles, use specific pixel values that match design tokens
  • Comment the expected values with their token references (e.g., // spacing.2 = 4px)
  • Test both presence and absence of styling conditions

Functional / Logic Testing

  • Use this pattern for triggering any events

    import userEvents from '@testing-library/user-event';
    
    const user = userEvents.setup();
    // ...render example
    
    // click / other events
    await user.click(getByRole('button', { name: 'Toggle Show' }));
  • Test the core Logic
  • If there is any logic that is too complex or too flaky for unit tests, ignore it and suggest user to write integration test instead

Import Organization

// Component imports
import { ComponentName, SubComponent } from '../index';
// Testing utilities
import renderWithTheme from '~utils/testing/renderWithTheme.web';
import assertAccessible from '~utils/testing/assertAccessible.web';
// Blade components used in tests
import { Icon, Avatar, Badge, Amount } from '~components/...';

Test Structure Template

describe('<ComponentName />', () => {
  it('should render simple ComponentName', () => {
    // Basic rendering test with snapshot
  });

  it('should render ComponentName with different complex configurations', () => {
    // Test various prop combinations
  });

  // Test for any hacky implementations that we've added while writing code that are likely break or not intuitive
  it('should test for any hacky implementations', () => {
    // Test conditional behavior with actual assertions
  });

  // test for import styling scenario
  it('should render the important style', () => {
    // Test for 2 most important styles using getComputedStyle
  });

  // test for core functionality (ignore if component is non-interactive)
  it('should render ComponentName with different complex configurations', () => {
    // Test various prop combinations
  });

  it('should accept testID', () => {
    // Test testID prop support
  });

  it('should not have accessibility violations', async () => {
    // accessibility violation test
    await assertAccessible(container);
  });
});

Running the test

Run test using following command from packages/blade directory

yarn test:react ComponentName

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.