The Problem

We’d love integration tests to take 5 minutes, but reality hits differently - they often take 20+ minutes. While tests run in the pipeline anyway, we need a faster feedback loop during development. Manually adding .only to test blocks is tedious and easy to forget.

Before: Always Wait

graph LR A["Local: 10 files"] --> B["Run All Tests"] B --> C["Wait: 20min"] D["Pipeline: 10 files"] --> E["Run All Tests"] E --> F["Wait: 20min"] style A fill:#e3f2fd style B fill:#e3f2fd style C fill:#e3f2fd style D fill:#fff3e0 style E fill:#fff3e0 style F fill:#fff3e0

After: Smart Selective Testing

graph LR A["Local: 2 changed files"] --> B[tdiff Script] B --> C["Fast: 30s"] D["Pipeline: 10 files"] --> E["Run All Tests"] E --> F["Wait: 20min"] style A fill:#e3f2fd style B fill:#4caf50,color:#fff style C fill:#e3f2fd style D fill:#fff3e0 style E fill:#fff3e0 style F fill:#fff3e0

TDD Workflow Example

Here’s how it fits into a typical TDD cycle:

  1. Find the failing test
$ ls | grep test
order.test.js
  1. Add a new test case
// order.test.js
describe("Order API", () => {
  // new test
  it("should calculate order total correctly", async () => {
    expect(calculate()).toEqual(25);
  });
});
  1. Run only changed tests
$ tdiff-integration
Running specific integration test files:
__integrations__/orders/api.test.ts

🧪 Final CHANGED_TESTS: 'should calculate order total correctly'
🎯 Running only changed test blocks with pattern: (should calculate order total correctly)

> Running tests...
  Order API
    ✓ should calculate order total correctly
      
  1 passing (2s)

The Solution

This selective test runner analyzes your git diff against main and runs only the integration tests that actually changed. It’s practical, not perfect - but it gets the job done.

The script does three things:

  1. Detects changed files - Uses git diff against main branch
  2. Extracts test names - Parses added it() and describe() blocks
  3. Runs selectively - Uses mocha’s --grep to run only those tests

Benefits

  • Faster feedback - 20 minutes → 2 seconds for changed tests
  • Works with multiple commits - Handles messy branch histories
  • Fallback safety - Runs full files if test extraction fails
  • Lean configuration - Drop in the script and edit as you wish

This tool doesn’t replace your full test suite - it complements it. You get fast local feedback while the pipeline ensures nothing breaks.