Testing strategy for WhatsApp API integrations
A layered test plan for WhatsApp integrations: unit tests, fixtures, a fake provider that injects failures and a thin real-number smoke suite.
Testing a WhatsApp integration end to end against real phones is slow, flaky and sometimes expensive, so teams either do too much of it or none. A layered strategy puts most of the confidence where tests are fast and deterministic, and keeps real-number tests for the few things only reality can tell you. This article proposes the layers, the cases that deserve a test in each and a few code examples.
What layers does the plan need?
Four layers work well: unit tests for pure logic, contract tests against recorded payloads, integration tests against a fake provider, and a small smoke suite against a real number. The pyramid is wide at the bottom and thin at the top.
| Layer | What it proves | Speed | Count |
|---|---|---|---|
| Unit | Signature check, idempotency keys, status ordering, error classification | Milliseconds | Many |
| Contract | Your parser handles real payload shapes | Fast | Dozens of fixtures |
| Fake-provider integration | Retries, backoff, queue behaviour, tenant isolation | Seconds | Dozens |
| Real-number smoke | Credentials, templates, delivery and webhooks work together | Minutes | A handful |
Which logic deserves unit tests?
Signature verification, deduplication, status ordering and error classification deserve unit tests, because they are pure functions whose bugs are expensive and silent. Each is a few lines and each has well-known failure cases.
- Signature: valid, wrong secret, tampered body, missing header, non-hex signature, wrong length, and old timestamp if you enforce a window.
- Deduplication: same event twice, same event concurrently, same message with different statuses.
- Status ordering: delivered before sent, read before delivered, failed after delivered.
- Error classification: each code you handle mapped to retry, template, human or ignore.
- Idempotency keys: the same input always produces the same key, different input never collides.
import { test } from 'node:test'
import assert from 'node:assert'
import { createHmac } from 'node:crypto'
import { verifyDelivery } from './verify.js'
const secret = 'whsec_test_secret'
const body = '{"id":"evt_1","event":"message.received"}'
const ts = '1766000000'
const id = 'c2d3e4f5-6a7b-4c8d-9e0f-1a2b3c4d5e6f'
const sign = (s, b) => 'sha256=' + createHmac('sha256', s).update(`${ts}.${id}.${b}`).digest('hex')
test('accepts a correctly signed delivery', () => {
assert.equal(verifyDelivery({ secret, ts, id, body, signature: sign(secret, body) }), true)
})
test('rejects a tampered body', () => {
assert.equal(verifyDelivery({ secret, ts, id, body: body + ' ', signature: sign(secret, body) }), false)
})
test('rejects a signature that is not hex without throwing', () => {
assert.equal(verifyDelivery({ secret, ts, id, body, signature: 'sha256=' + 'z'.repeat(64) }), false)
})The third test is the one most suites miss. Comparing buffers of different lengths throws in some runtimes, so a malformed header can crash the handler unless the format is validated first. The webhooks reference gives the signed string for the native format used here.
How should fixtures be used?
Record real payloads, scrub personal data and keep them in the repository as fixtures that every parser test runs against. This catches shape surprises without a live number.
Collect one fixture per case: text, image, audio, location, interactive reply, template reply, each status, and failed with an error code. Meta documents that retries can duplicate notifications, so include a fixture delivered twice. If your integration uses raw Meta payloads, keep them separate from normalised fixtures, since the two shapes are different contracts. Re-record fixtures when the provider announces a payload change, and keep a test that fails loudly on unknown event types rather than silently ignoring them.
What does a fake provider need to do?
It needs to accept sends, return accepted responses, emit status webhooks on demand and inject failures: 429, temporary and permanent error codes, slow responses and dropped webhooks. This is where the retry and recovery logic gets exercised.
// scenario table for a fake provider used in integration tests
const scenarios = [
{ name: 'rate limited then ok', sends: [{ status: 429 }, { status: 202 }], expect: 'one message accepted, one retry' },
{ name: 'maintenance then ok', sends: [{ error: 131057 }, { status: 202 }], expect: 'retried with delay, not failed' },
{ name: 'window closed', sends: [{ error: 131047 }], expect: 'falls back to template or surfaces to a human' },
{ name: 'duplicate webhook', webhooks: ['status:delivered', 'status:delivered'], expect: 'single state transition' },
{ name: 'out of order', webhooks: ['status:read', 'status:delivered'], expect: 'final state is read' },
]Add the multi-tenant cases here too: two tenants, one fake provider, and assertions that an event for tenant A never lands in tenant B, that a credential from A cannot send on B's channel, and that a burst from A does not delay B beyond its budget. Those are the bugs that end customer relationships.
What belongs in the real-number smoke suite?
Only what a fake cannot prove: valid credentials, an approved template that sends, a delivery status that arrives, a reply that reaches your webhook with a valid signature. Keep it to minutes and run it after each production deploy.
- Send an approved template to an internal number and wait for delivered.
- Reply from the phone and assert the inbound event arrives and verifies.
- Call the channel diagnostics endpoint and assert all checks pass.
- Send twice with the same idempotency key and assert one message.
- Run it against production with an internal recipient list only, and against staging with a dedicated test number.
The Hub limits endpoint test events to 10 per hour per endpoint, so the smoke suite should rely on real messages rather than repeated test events. Keep it quiet: a flaky smoke suite trains teams to ignore it. Retry a failed run once and alert only if the second run fails too, and record every run so flakiness is visible as data.
How should the suites run in CI?
Run unit, contract and fake-provider tests on every commit, and run the real-number smoke suite after deploys and on a schedule. Keeping the slow, external layer out of the commit path keeps feedback fast and failures attributable.
- Fail the build on any unhandled event type in fixtures, so new provider events are noticed.
- Keep secrets for the smoke suite in the CI secret store, scoped to the staging organisation, and rotate them on the normal schedule.
- Publish test results per layer, so a flaky smoke run does not hide behind a green unit suite.
- Track test duration; a unit layer that creeps past a minute is a sign that something external has slipped into it.
Treat a failing smoke run after a deploy as a rollback trigger only when the diagnostics call and a plain template send both fail. A single missed status can be a transient delivery matter on the recipient side rather than a regression.
What about load, time and chaos?
Test rate-limit behaviour with load against the fake, control time in tests that depend on windows and expiry, and rehearse failure of your own receiver. Real conditions rarely announce themselves.
Use a fake clock for the 24-hour window, token expiry and retention jobs, so a test that would take a day finishes in a millisecond. Run a load test of your queue against the fake with the provider limits as configuration, as described in the throughput article. Finally, rehearse the outage routine: stop your receiver in staging for an hour, restart it and run the reconciliation script. See the quickstart for the minimal calls needed to wire a smoke suite to a staging organisation.
Frequently asked questions
Do I need a real WhatsApp number to test?
For most logic, no. Use unit tests, recorded fixtures and a fake provider, and keep a small suite with a real number for credentials, templates and end-to-end delivery.
How do I test that duplicates and out-of-order statuses are handled?
Put them in fixtures and fake-provider scenarios: deliver the same event twice, and send read before delivered. Assert on the final state and on the number of side effects.
Where should signature verification be tested?
At the unit level, with valid, tampered, malformed and missing-header cases, plus a test that a non-hex signature returns false instead of throwing.
Fontes
Documentação oficial da Meta, conferida em 31 de outubro de 2026.