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.

referencesapitest-helper.md

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

API Test Helper

AuthTestHelper Implementation

// test/helpers/auth.helper.ts
import * as jwt from 'jsonwebtoken';

export class AuthTestHelper {
  constructor(private readonly secret: string) {}

  /**
   * Generate valid JWT token
   */
  generateToken(payload: object, options?: jwt.SignOptions): string {
    return jwt.sign(payload, this.secret, {
      expiresIn: '1h',
      ...options,
    });
  }

  /**
   * Generate expired JWT token
   */
  generateExpiredToken(payload: object): string {
    return this.generateToken(payload, { expiresIn: '-1h' });
  }

  /**
   * Generate invalid token
   */
  generateInvalidToken(): string {
    return 'invalid.token.here';
  }

  /**
   * Generate token with specific role
   */
  generateTokenWithRole(userId: string, role: string): string {
    return this.generateToken({ userId, role });
  }

  /**
   * Generate admin token
   */
  generateAdminToken(userId: string = 'admin-1'): string {
    return this.generateTokenWithRole(userId, 'admin');
  }

  /**
   * Generate user token
   */
  generateUserToken(userId: string = 'user-1'): string {
    return this.generateTokenWithRole(userId, 'user');
  }
}

SupertestHelper Implementation

// test/helpers/supertest.helper.ts
import * as request from 'supertest';
import { Server } from 'http';

export class SupertestHelper {
  constructor(
    private httpServer: Server,
    private authHelper: AuthTestHelper
  ) {}

  /**
   * Make authenticated request
   */
  authenticatedRequest(token: string) {
    return {
      get: (url: string) => request(this.httpServer).get(url).set('Authorization', `Bearer ${token}`),
      post: (url: string) => request(this.httpServer).post(url).set('Authorization', `Bearer ${token}`),
      put: (url: string) => request(this.httpServer).put(url).set('Authorization', `Bearer ${token}`),
      patch: (url: string) => request(this.httpServer).patch(url).set('Authorization', `Bearer ${token}`),
      delete: (url: string) => request(this.httpServer).delete(url).set('Authorization', `Bearer ${token}`),
    };
  }

  /**
   * Make request as admin
   */
  asAdmin() {
    return this.authenticatedRequest(this.authHelper.generateAdminToken());
  }

  /**
   * Make request as user
   */
  asUser(userId?: string) {
    return this.authenticatedRequest(this.authHelper.generateUserToken(userId));
  }

  /**
   * Make unauthenticated request
   */
  unauthenticated() {
    return {
      get: (url: string) => request(this.httpServer).get(url),
      post: (url: string) => request(this.httpServer).post(url),
      put: (url: string) => request(this.httpServer).put(url),
      patch: (url: string) => request(this.httpServer).patch(url),
      delete: (url: string) => request(this.httpServer).delete(url),
    };
  }
}

Usage Example

describe('User API E2E', () => {
  let authHelper: AuthTestHelper;
  let apiHelper: SupertestHelper;

  beforeAll(async () => {
    const setup = await createTestApp([UserModule]);
    authHelper = new AuthTestHelper(process.env.JWT_SECRET!);
    apiHelper = new SupertestHelper(setup.httpServer, authHelper);
  });

  it('should allow admin to list all users', async () => {
    const response = await apiHelper.asAdmin()
      .get('/users')
      .expect(200);

    expect(response.body.data).toBeDefined();
  });

  it('should deny regular user access to admin endpoint', async () => {
    await apiHelper.asUser()
      .get('/admin/users')
      .expect(403);
  });

  it('should return 401 for unauthenticated request', async () => {
    await apiHelper.unauthenticated()
      .get('/protected')
      .expect(401);
  });
});

GraphQL Test Helper

// test/helpers/graphql.helper.ts
import * as request from 'supertest';
import { Server } from 'http';

export class GraphQLTestHelper {
  constructor(
    private httpServer: Server,
    private endpoint: string = '/graphql'
  ) {}

  /**
   * Execute GraphQL query
   */
  async query<T = any>(
    query: string,
    variables?: object,
    token?: string
  ): Promise<{ data: T; errors?: any[] }> {
    const req = request(this.httpServer)
      .post(this.endpoint)
      .send({ query, variables });

    if (token) {
      req.set('Authorization', `Bearer ${token}`);
    }

    const response = await req.expect(200);
    return response.body;
  }

  /**
   * Execute GraphQL mutation
   */
  async mutate<T = any>(
    mutation: string,
    variables?: object,
    token?: string
  ): Promise<{ data: T; errors?: any[] }> {
    return this.query(mutation, variables, token);
  }
}

API Reference

AuthTestHelper

Method Description
generateToken(payload, options?) Generate valid JWT token
generateExpiredToken(payload) Generate expired token
generateInvalidToken() Generate invalid token string
generateTokenWithRole(userId, role) Generate token with specific role
generateAdminToken(userId?) Generate admin token
generateUserToken(userId?) Generate user token

SupertestHelper

Method Description
authenticatedRequest(token) Create authenticated request builder
asAdmin() Create admin request builder
asUser(userId?) Create user request builder
unauthenticated() Create unauthenticated request builder

GraphQLTestHelper

Method Description
query(query, variables?, token?) Execute GraphQL query
mutate(mutation, variables?, token?) Execute GraphQL mutation

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.