BookatlasTwo-Day MuleSoft → Salesforce Bootcamp
266 / 278 · Module 18 — Interview Coding Scenarios · filtered: code← prev⊞ allnext →

18.30Scenario 29 — Idempotency test in TypeScript

If they shift from MUnit to your stronger area:

test('repeated customer request does not duplicate Account', async ({ request }) => {
  const customer = {
    customerId: `IDEMP-${Date.now()}`,
    companyName: 'Acme'
  };

  const first = await request.post('/customers', {
    data: customer
  });

  expect(first.ok()).toBeTruthy();

  const second = await request.post('/customers', {
    data: customer
  });

  expect(second.ok()).toBeTruthy();

  const accounts =
    await salesforce.findAccountsByExternalId(
      customer.customerId
    );

  expect(accounts).toHaveLength(1);
});

And the stronger concurrency version:

await Promise.all(
  Array.from({ length: 10 }, () =>
    request.post('/customers', {
      data: customer
    })
  )
);

const accounts =
  await salesforce.findAccountsByExternalId(
    customer.customerId
  );

expect(accounts).toHaveLength(1);

This is excellent interview material for you.