Feature Flag Service Tutorial · Module 05 of 10

Testing Flag Logic

Write tests for flag evaluation, rule matching, percentage rollout, and edge cases. Test rule priority, attribute matching, segment membership. Use Jest with integration tests using testcontainers. By the end, flag logic is well-tested and confident.

~3–4 hrsIntermediateTesting focus
← Back to Module 05 overview
What You'll Have at the End

Definition of Done

  • Unit tests: Rule evaluation, operators (eq, contains, regex, etc), percentage logic.
  • Integration tests: Flag creation, rule evaluation via API.
  • Edge case tests: Empty rules, conflicting rules, priority ordering.
  • Percentage rollout test: 50% of users get flag, distribution is even.
  • Attribute matching tests: Different data types (string, number, boolean, array).
  • Segment membership tests: User in segment enabled, user not in segment disabled.
  • Coverage > 85% for core logic.
The Steps

Build It

STEP 1

Set up Jest and testing infrastructure

Install: npm install --save-dev jest ts-jest @types/jest testcontainers

Create jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['/src'],
  testMatch: ['**/__tests__/**/*.test.ts'],
  collectCoverageFrom: ['src/**/*.ts', '!src/**/*.d.ts', '!src/index.ts'],
  coverageThreshold: {
    global: { branches: 75, functions: 75, lines: 85, statements: 85 }
  }
};