checkoutOs
Guides

Testing

The unit/integration split, what each one mocks, and the commands that actually work.

`npm test` is a dead placeholder

It exits 1. Run the two projects explicitly:

npm run test:unit && npm run test:integration

The split

UnitIntegration
Locationtests/unit/tests/integration/
Redisioredis → ioredis-mockReal Redis at localhost:6379
HTTPNo external servicesaxios is not mocked
Timeout5 s15 s
ParallelismParallelfileParallelism: false — files run serially

Unit tests mock ioredis via vi.mock() in vitest.setup.unit.ts and silence the logger.

Integration tests need Redis running first:

docker start checkoutos-redis

Integration tests target the real Razorpay sandbox

vitest.setup.integration.ts overrides RAZORPAY_BASE_URL to https://api.razorpay.com/v1 and uses hardcoded Razorpay test-mode credentials. The integration suite therefore needs network access and hits the Razorpay sandbox, not the MSW mock on :9090.

Test isolation

beforeEach in the integration setup deletes all chk:* keys from Redis. That is what prevents cross-test contamination, and it is also why you should never point test:integration at a Redis instance holding anything you care about.

Helpers

tests/helpers/app.helper.ts provides:

  • createTestApp() — a singleton buildApp()
  • registerTestGateway() — registers a stub plugin

createTestApp() does not call initialise()

buildApp() is a pure factory with no side effects, so a test gets an Express app without connecting Redis or registering gateways. Call registerTestGateway() when the code under test reaches getActiveGateway().

Commands

npm run test:unit          # no Redis needed
npm run test:integration   # Redis + network access required
npm run test:watch         # watch mode, unit only
npm run test:coverage      # lines 70%, functions 70%, branches 60%
npm run typecheck          # tsc --noEmit, strict
npm run lint:circular      # madge --circular over src/
npm run verify:telemetry   # boots both entry points and checks /metrics

There is no lint script; run npx eslint . directly.

What to cover in a new gateway

The mapper is where correctness is cheapest to assert:

  • Every native status the gateway documents → its unified PaymentStatus
  • One undocumented status → asserts GatewayMappingError

That second case is the one that matters. It is the test that fails loudly the day the gateway ships a new state, instead of letting it be coerced into FAILED.

For webhook verification, assert both directions: a correctly signed body verifies, and a body with a tampered byte does not.

Linting

ScopeRules
src/**/*.tsno-explicit-any: error, explicit-function-return-type: error (allowExpressions: false), import/no-default-export: error
tests/, mocks/explicit-function-return-type, no-non-null-assertion, ban-ts-comment off

ESLint uses tsconfig.eslint.json rather than tsconfig.json, which widens rootDir to . so tests and mocks are type-checked too. Prettier: single quotes, semicolons, trailing commas, 100-character width.

On this page