Skip to content

Send testId with mock collection for contract validation#203

Merged
kevinccbsg merged 1 commit into
mainfrom
feat/mock-collect-test-id
Apr 12, 2026
Merged

Send testId with mock collection for contract validation#203
kevinccbsg merged 1 commit into
mainfrom
feat/mock-collect-test-id

Conversation

@kevinccbsg

Copy link
Copy Markdown
Member

Summary

Add testId to the __twdCollectMock payload so twd-cli can identify which test registered each mock. This enables contract validation to:

  • Distinguish mocks with the same alias registered across different tests
  • Show which test caused a contract validation failure
  • Track how many times the same alias is used within a single test

Changes

  • Find the currently running test by scanning window.TWD_STATE.handlers for status === 'running'
  • Include its id in the __twdCollectMock payload as testId field
  • Only call __twdCollectMock when a test is actually running (guard on running status)
  • Updated 2 existing tests to set up running handler and expect testId
  • Added 2 new tests for guard condition (no collection when test not running)

Mock Payload

The __twdCollectMock function now receives:

{
  alias: string,
  url: string,
  method: string,
  status: number,
  response: unknown,
  urlRegex: boolean,
  testId: string  // NEW — handler ID from window.__TWD_STATE__.handlers
}

Testing

  • All 354 tests passing
  • No regressions
  • 4 new tests for testId payload and guard conditions

Related

Paired with twd-cli PR: Fix mock overlap in contract validation (feat/fix-mock-overlap branch)

Backward Compatibility

  • Consumers of __twdCollectMock that don't expect testId will simply ignore the extra field
  • If twd-cli is on an older version that doesn't read testId, nothing breaks
  • If twd-js is on an older version that doesn't send testId, twd-cli handles gracefully

Find the currently running test by scanning window.__TWD_STATE__.handlers
for status === 'running' and include its id in the __twdCollectMock payload.
Skip collection entirely when no test is running.
@kevinccbsg kevinccbsg merged commit fc0958c into main Apr 12, 2026
8 checks passed
@kevinccbsg kevinccbsg deleted the feat/mock-collect-test-id branch April 12, 2026 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant