All skills
bmad-labs avatar

/typescript-e2e-testing

@3bcc7a6
by bmad-labsbmad-labs/skills15 stars
4

E2E and integration testing for TypeScript/NestJS projects using Jest, supertest, and real infrastructure via Docker (Kafka, PostgreSQL, MongoDB, Redis) with the Given-When-Then pattern. Use whenever the user is working on `.e2e-spec.ts` files or anything under `test/e2e/`, or asks to set up, write, review, run, debug, or optimize E2E or integration tests — including flaky tests, docker-compose for tests, Kafka/Redpanda consumers, test isolation, or GWT compliance.

Use this Skill: https://skilld.dev/gh/bmad-labs/skills/typescript-e2e-testing

This session only. Nothing lands on disk.

referencesmongodbrules.md

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

MongoDB E2E Testing Rules

Core Rules

Rule Requirement
Port Use different port from dev (e.g., 27018)
Cleanup deleteMany({}) in beforeEach
Connection Single connection pool (maxPoolSize: 1)
ObjectId Compare using .toString() or .equals()
Lean Queries Use .lean() for read-only operations

Cleanup Rules

deleteMany (Recommended)

beforeEach(async () => {
  await productModel.deleteMany({});
  await categoryModel.deleteMany({});
  await userModel.deleteMany({});
});

// Also clean after to prevent interference
afterEach(async () => {
  await productModel.deleteMany({});
});

Clear All Collections

beforeEach(async () => {
  const collections = Object.keys(connection.collections);
  for (const collectionName of collections) {
    await connection.collections[collectionName].deleteMany({});
  }
});

Drop and Recreate (Full Reset)

beforeEach(async () => {
  const collections = await connection.db.collections();
  for (const collection of collections) {
    await collection.drop().catch(() => {
      // Ignore "collection doesn't exist" error
    });
  }
});

Connection Rules

// Mongoose connection config for tests
mongoose.connect(uri, {
  connectTimeoutMS: 30000,
  serverSelectionTimeoutMS: 30000,
  maxPoolSize: 1,  // Single connection for tests
});

Assertion Rules

Assert Document Exists

// GOOD: Assert specific values
await helper.assertDocumentExists(productModel, { name: 'Widget' }, {
  price: 29.99,
  stock: 100,
});

// BAD: Only check existence
const product = await productModel.findOne({ name: 'Widget' });
expect(product).toBeDefined();

Assert Document Count

await helper.assertDocumentCount(Order, { userId: user.id }, 3);

ObjectId Comparison

// WRONG: Direct comparison
expect(product.categoryId).toBe(category._id);

// CORRECT: Use toString()
expect(product.categoryId.toString()).toBe(category._id.toString());

// CORRECT: Use equals()
expect(product.categoryId.equals(category._id)).toBe(true);

Date Comparison

// WRONG: Direct date comparison
expect(product.createdAt).toBe(expectedDate);

// CORRECT: Compare timestamps
expect(product.createdAt.getTime()).toBe(expectedDate.getTime());

// CORRECT: Use toISOString
expect(product.createdAt.toISOString()).toBe(expectedDate.toISOString());

Query Rules

Use Lean Queries for Assertions

// SLOW: Returns Mongoose documents
const products = await productModel.find();

// FAST: Returns plain objects
const products = await productModel.find().lean();

Use Projection for Needed Fields

// SLOW: Fetch all fields
const user = await userModel.findById(id);

// FAST: Only fetch needed fields
const user = await userModel.findById(id).select('email name');

Performance Rules

Bulk Operations

// SLOW: Individual inserts
for (const product of products) {
  await productModel.create(product);
}

// FAST: insertMany
await productModel.insertMany(products);

// FASTEST: Unordered bulk
await productModel.insertMany(products, { ordered: false });

Index Management

// Ensure indexes once in beforeAll
beforeAll(async () => {
  await productModel.ensureIndexes();
  await categoryModel.ensureIndexes();
});

Transaction Rules (MongoDB 4.0+)

describe('with transactions', () => {
  let session: ClientSession;

  beforeEach(async () => {
    session = await connection.startSession();
    session.startTransaction();
  });

  afterEach(async () => {
    await session.abortTransaction();
    session.endSession();
  });

  it('should rollback changes', async () => {
    await productModel.create([{ name: 'Test' }], { session });
    // Transaction will be rolled back after test
  });
});

Checklist

Setup:

  • Different port from dev (e.g., 27018)
  • WiredTiger cache size limited
  • Single connection pool (maxPoolSize: 1)

Cleanup:

  • deleteMany({}) in beforeEach
  • Optional afterEach cleanup
  • Index recreation if using drop

Assertions:

  • Use .lean() for read operations
  • Use .toString() for ObjectId comparison
  • Use .getTime() for Date comparison
  • Assert specific values, not just existence

Performance:

  • Use insertMany for seeding
  • Use projection for needed fields only
  • Ensure indexes in beforeAll

Source: SKILL.md on GitHub

1 alert16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides comprehensive workflows and reference materials for E2E testing in TypeScript/NestJS projects. It uses standard development tools like Jest, Docker, and shell commands. No malicious patterns or security risks were detected.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    30/103 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

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

Last checked against GitHub 2 days ago.

Activeupdated 5 months ago
  • TypeScript
  • jest
  • nestjs
  • e2e-testing
  • supertest
  • docker
  • kafka
  • postgres
  • mongodb
  • redis
  • integration-testing
  • gwt-pattern

README badge

README badge for bmad-labs/skills

Writes E2E and integration tests for NestJS projects using Jest and supertest against real Docker infrastructure (Kafka, PostgreSQL, MongoDB, Redis) following the Given-When-Then pattern. Covers test setup, writing, review, debugging, and optimization workflows with technology-specific helpers and isolation strategies for each service.

Generated from the current SKILL.md.

Does this skill work with NestJS only?
The skill is built for NestJS projects using Jest and supertest, but the patterns and workflows apply to any TypeScript backend with HTTP endpoints. Technology-specific helpers exist for Kafka, PostgreSQL, MongoDB, and Redis.
Do I need Docker running to use this skill?
Yes. The skill assumes real infrastructure via Docker (Kafka, PostgreSQL, MongoDB, Redis). Tests execute against actual services, not mocks.
What if my E2E tests are flaky or failing?
The Debugging E2E Test workflow provides a systematic protocol: fix one test at a time using isolated test runs, verify with 3-5 consecutive runs before moving to the next failure, then run the full suite only once all tests pass individually.
Does this skill cover GraphQL or gRPC testing?
Yes. The API testing reference includes examples for REST, GraphQL, and gRPC, plus external API mocking with MSW and Nock.
What is the Given-When-Then pattern and is it mandatory?
GWT is the mandatory test structure: Given describes setup state, When describes the action under test, Then describes expected outcomes. All E2E tests in this skill must follow this pattern.

Generated from the current SKILL.md. These answers refresh after source changes.