Stabilize flaky checkout.spec.ts in billing-service
T-2841 · Running · azad / claude-sonnet-4.6 · started by YOYusuf O. · 4m 18s
checkout.spec.ts ×
stripe-webhook.ts ×
stripe.ts ×
tests / integration / checkout.spec.ts +18 / −4
  @@ -42,15 +42,29 @@ describe('checkout flow', () => {
4242
    let stripeServer: MockStripe;
4343
    let webhookFixture: WebhookFixture;
4444
4545
    beforeEach(async () => {
46
      stripeServer = await startMockStripe();
47
      webhookFixture = loadWebhookFixture('checkout.session.completed');
48
      await seedDatabase(testData);
49
    });
46
      // 2841: stripe mock + DB must be ready before the fixture is registered;
47
      // otherwise the webhook handler races the seedDatabase() promise and
48
      // reads from an empty `customers` table on roughly half of all runs.
49
      stripeServer = await startMockStripe();
50
      await seedDatabase(testData);
51
      await stripeServer.readyForFixtures();
52
      webhookFixture = await loadWebhookFixture('checkout.session.completed', {
53
        ackGate: stripeServer.ackGate,
54
      });
55
    });
5056
5157
    afterEach(async () => {
5258
      await stripeServer.stop();
5359
    });
5460
5561
    it('completes a session and writes one invoice', async () => {
5662
      const session = await createSession(testCustomer);
  @@ -71,8 +85,14 @@ describe('checkout flow', () => {
7185
      await webhookFixture.post('/webhooks/stripe');
7286
73
      await sleep(250); // 🚨 race
87
      await webhookFixture.whenAcked();
88
      await stripeServer.flushPending();
7489
7590
      const invoices = await listInvoices(testCustomer.id);
7691
      expect(invoices).toHaveLength(1);
7792
      expect(invoices[0].status).toBe('paid');
7893
    });
agent · zsh
jest --watch
stripe-mock
~/billing-service on agent/flaky-stripe-webhook ⚡ 2 modified
npm test -- tests/integration/checkout.spec.ts --runInBand
PASS tests/integration/checkout.spec.ts (8.241 s)
completes a session and writes one invoice (1342 ms)
reissues invoice when webhook arrives twice (894 ms)
rejects unsigned webhook payloads (412 ms)
retries on transient stripe-mock failures (2018 ms)
Test Suites: 1 passed, 1 total
Tests: 4 passed, 4 total
Snapshots: 0 total
Time: 8.421 s
for i in {1..20}; do npm test -- tests/integration/checkout.spec.ts --silent; done # soak
running 20 iterations…
████████████████████ 20/20 passed · 0 flakes (was 11/20 before patch)