Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Improve e2e test performance with polling and parallelization (#3415)

Kiran K693bfd35682c
diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.ts
index 2ab22bee99..ea55faf1fd 100644
--- a/apps/web/tests/tracks/track-sale.test.ts
+++ b/apps/web/tests/tracks/track-sale.test.ts
@@ -39,7 +39,7 @@ const expectValidSaleResponse = (
});
};
-describe("POST /track/sale", async () => {
+describe.concurrent("POST /track/sale", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
@@ -79,48 +79,54 @@ describe("POST /track/sale", async () => {
test("track a sale with regular vs premium product ID (should create the right commission)", async () => {
const regularInvoiceId = `INV_${randomId()}`;
- const response1 = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: 2000,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: regularInvoiceId,
- metadata: {
- productId: "regularProductId",
- },
- },
- });
- expect(response1.status).toEqual(200);
-
const premiumInvoiceId = `INV_${randomId()}`;
- const response2 = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: 3000,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: premiumInvoiceId,
- metadata: {
- productId: "premiumProductId",
+
+ const [response1, response2] = await Promise.all([
+ http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: 2000,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ invoiceId: regularInvoiceId,
+ metadata: {
+ productId: "regularProductId",
+ },
},
- },
- });
+ }),
+
+ http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: 3000,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ invoiceId: premiumInvoiceId,
+ metadata: {
+ productId: "premiumProductId",
+ },
+ },
+ }),
+ ]);
+
+ expect(response1.status).toEqual(200);
expect(response2.status).toEqual(200);
- await verifyCommission({
- http,
- invoiceId: regularInvoiceId,
- expectedAmount: response1.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.amountInCents,
- });
+ await Promise.all([
+ verifyCommission({
+ http,
+ invoiceId: regularInvoiceId,
+ expectedAmount: response1.data.sale?.amount!,
+ expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ }),
- await verifyCommission({
- http,
- invoiceId: premiumInvoiceId,
- expectedAmount: response2.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
- });
+ verifyCommission({
+ http,
+ invoiceId: premiumInvoiceId,
+ expectedAmount: response2.data.sale?.amount!,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[0].amountInCents!,
+ }),
+ ]);
});
test("track a sale with an externalId that does not exist (should return null customer and sale)", async () => {
@@ -257,48 +263,52 @@ describe("POST /track/sale", async () => {
const smallSaleInvoiceId = `INV_${randomId()}`;
const smallSaleAmount = 10000; // $100.00
- const response1 = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: smallSaleAmount,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: smallSaleInvoiceId,
- },
- });
-
- expect(response1.status).toEqual(200);
- expect(response1.data.sale?.amount).toEqual(smallSaleAmount);
-
// Test with a large sale amount
const largeSaleInvoiceId = `INV_${randomId()}`;
const largeSaleAmount = 20000; // $200.00
- const response2 = await http.post<TrackSaleResponse>({
- path: "/track/sale",
- body: {
- ...sale,
- amount: largeSaleAmount,
- customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
- invoiceId: largeSaleInvoiceId,
- },
- });
+ const [response1, response2] = await Promise.all([
+ http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: smallSaleAmount,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ invoiceId: smallSaleInvoiceId,
+ },
+ }),
+
+ http.post<TrackSaleResponse>({
+ path: "/track/sale",
+ body: {
+ ...sale,
+ amount: largeSaleAmount,
+ customerExternalId: E2E_CUSTOMER_EXTERNAL_ID_2,
+ invoiceId: largeSaleInvoiceId,
+ },
+ }),
+ ]);
+
+ expect(response1.status).toEqual(200);
+ expect(response1.data.sale?.amount).toEqual(smallSaleAmount);
expect(response2.status).toEqual(200);
expect(response2.data.sale?.amount).toEqual(largeSaleAmount);
- await verifyCommission({
- http,
- invoiceId: smallSaleInvoiceId,
- expectedAmount: response1.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.amountInCents,
- });
+ await Promise.all([
+ verifyCommission({
+ http,
+ invoiceId: smallSaleInvoiceId,
+ expectedAmount: response1.data.sale?.amount!,
+ expectedEarnings: E2E_SALE_REWARD.amountInCents,
+ }),
- await verifyCommission({
- http,
- invoiceId: largeSaleInvoiceId,
- expectedAmount: response2.data.sale?.amount!,
- expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
- });
+ verifyCommission({
+ http,
+ invoiceId: largeSaleInvoiceId,
+ expectedAmount: response2.data.sale?.amount!,
+ expectedEarnings: E2E_SALE_REWARD.modifiers[1].amountInCents!,
+ }),
+ ]);
});
});
diff --git a/apps/web/tests/utils/verify-commission.ts b/apps/web/tests/utils/verify-commission.ts
index 0e230e300d..dc33a9b273 100644
--- a/apps/web/tests/utils/verify-commission.ts
+++ b/apps/web/tests/utils/verify-commission.ts
@@ -10,6 +10,9 @@ interface VerifyCommissionProps {
expectedEarnings: number;
}
+const POLL_INTERVAL_MS = 5000; // 5 seconds
+const TIMEOUT_MS = 30000; // 30 seconds
+
export const verifyCommission = async ({
http,
customerExternalId,
@@ -19,10 +22,7 @@ export const verifyCommission = async ({
}: VerifyCommissionProps) => {
let customerId: string | undefined;
- // Pause for 3 seconds for data to be fully processed
- await new Promise((resolve) => setTimeout(resolve, 3000));
-
- // Optional: resolve customer ID if customerExternalId is given
+ // Resolve customer ID first if customerExternalId is given
if (customerExternalId) {
const { data: customers } = await http.get<Customer[]>({
path: "/customers",
@@ -43,27 +43,43 @@ export const verifyCommission = async ({
query.customerId = customerId;
}
- const { status, data: commissions } = await http.get<CommissionResponse[]>({
- path: "/commissions",
- query,
- });
+ // Poll for commission every 5 seconds, timeout after 30 seconds
+ const startTime = Date.now();
- expect(status).toEqual(200);
- expect(commissions).toHaveLength(1);
+ while (Date.now() - startTime < TIMEOUT_MS) {
+ const { status, data: commissions } = await http.get<CommissionResponse[]>({
+ path: "/commissions",
+ query,
+ });
- const commission = commissions[0];
+ if (status === 200 && commissions.length === 1) {
+ const commission = commissions[0];
- if (invoiceId) {
- expect(commission.invoiceId).toEqual(invoiceId);
- }
+ // Verify all expectations
+ if (invoiceId) {
+ expect(commission.invoiceId).toEqual(invoiceId);
+ }
- if (customerId) {
- expect(commission.customer?.id).toEqual(customerId);
- }
+ if (customerId) {
+ expect(commission.customer?.id).toEqual(customerId);
+ }
+
+ if (expectedAmount !== undefined) {
+ expect(commission.amount).toEqual(expectedAmount);
+ }
+
+ expect(commission.earnings).toEqual(expectedEarnings);
+
+ return;
+ }
- if (expectedAmount !== undefined) {
- expect(commission.amount).toEqual(expectedAmount);
+ // Wait before next poll
+ await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
}
- expect(commission.earnings).toEqual(expectedEarnings);
+ // Timeout reached - fail the test
+ throw new Error(
+ `Commission not found within ${TIMEOUT_MS / 1000} seconds. ` +
+ `Query: ${JSON.stringify(query)}`,
+ );
};