All skills
asyrafhussin avatar

/testing-best-practices

@575c801

Unit testing, integration testing, and test-driven development principles. Use when writing tests, reviewing test code, improving test coverage, or setting up testing strategy. Triggers on "write tests", "review tests", "testing best practices", or "TDD".

Use this Skill: https://skilld.dev/gh/asyrafhussin/agent-skills/testing-best-practices

This session only. Nothing lands on disk.

rulesiso-no-order-dependency.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

No Order Dependency

Impact: CRITICAL (parallel execution and test stability)

Tests should pass regardless of the order in which they run. Many test runners shuffle test order to catch dependencies, and tests running in parallel have no guaranteed order.

Incorrect

// ❌ Bad: Tests that depend on execution order
describe('Database Integration', () => {
  test('inserts user into database', async () => {
    await db.query('INSERT INTO users (id, name) VALUES (1, "Alice")');
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows[0].name).toBe('Alice');
  });

  test('updates user in database', async () => {
    // Depends on previous test's insert
    await db.query('UPDATE users SET name = "Alicia" WHERE id = 1');
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows[0].name).toBe('Alicia');
  });

  test('verifies user state', async () => {
    // Depends on update from previous test
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows[0].name).toBe('Alicia');
  });

  test('deletes user from database', async () => {
    // Must run last or will break other tests
    await db.query('DELETE FROM users WHERE id = 1');
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows.length).toBe(0);
  });
});

// Static counter creates order dependency
let testSequence = 0;

test('first operation', () => {
  testSequence = 1;
  expect(processStep(testSequence)).toBe('step-1');
});

test('second operation', () => {
  // Fails if run before 'first operation'
  expect(testSequence).toBe(1);
  testSequence = 2;
  expect(processStep(testSequence)).toBe('step-2');
});

Problems:

  • Each test depends on state left behind by the previous test
  • Running tests in random order or in isolation will fail
  • Static counters and module-level variables create hidden coupling
  • Cannot safely parallelize or selectively run tests

Correct

// ✅ Good: Each test is self-contained
describe('Database Integration', () => {
  beforeEach(async () => {
    await db.query('DELETE FROM users');
  });

  test('inserts user into database', async () => {
    await db.query('INSERT INTO users (id, name) VALUES (1, "Alice")');

    const result = await db.query('SELECT * FROM users WHERE id = 1');

    expect(result.rows[0].name).toBe('Alice');
  });

  test('updates existing user in database', async () => {
    // Arrange: Create the user this test needs
    await db.query('INSERT INTO users (id, name) VALUES (1, "Alice")');

    // Act
    await db.query('UPDATE users SET name = "Alicia" WHERE id = 1');

    // Assert
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows[0].name).toBe('Alicia');
  });

  test('deletes user from database', async () => {
    // Arrange: Create the user this test needs
    await db.query('INSERT INTO users (id, name) VALUES (1, "Alice")');

    // Act
    await db.query('DELETE FROM users WHERE id = 1');

    // Assert
    const result = await db.query('SELECT * FROM users WHERE id = 1');
    expect(result.rows.length).toBe(0);
  });
});

// Each test is independent
describe('Step Processing', () => {
  test('processes first step correctly', () => {
    expect(processStep(1)).toBe('step-1');
  });

  test('processes second step correctly', () => {
    expect(processStep(2)).toBe('step-2');
  });

  test('processes steps in sequence', () => {
    const sequence = [1, 2, 3];
    const results = sequence.map(step => processStep(step));

    expect(results).toEqual(['step-1', 'step-2', 'step-3']);
  });
});

Benefits:

  • Tests can be shuffled with jest --randomize to detect hidden dependencies
  • Parallel execution is safe because no test depends on another
  • Developers can run single tests or subsets during development
  • Watch mode correctly re-runs only changed tests
  • Individual tests are easy to debug in isolation

Reference: Jest CLI — Randomize

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides comprehensive guidelines and best practices for unit testing, integration testing, and Test-Driven Development (TDD). It consists entirely of documentation and illustrative code examples using industry-standard tools like Jest and Vitest. No security risks or malicious patterns were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    29 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 575c801. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub last month.

Steadyupdated 7 months ago
metadata
{
  "author": "agent-skills",
  "version": "2.0.0"
}

README badge

README badge for asyrafhussin/agent-skills/testing-best-practices