diff --git a/test-e2e/spec/expect-with-retry.js b/test-e2e/spec/catch-and-retry.js similarity index 64% rename from test-e2e/spec/expect-with-retry.js rename to test-e2e/spec/catch-and-retry.js index 8c5122e..26b29bc 100644 --- a/test-e2e/spec/expect-with-retry.js +++ b/test-e2e/spec/catch-and-retry.js @@ -1,4 +1,4 @@ -export default async (callback, { maxRetries = 10, delay = 250, backOff = 2 } = {}) => { +export default async (callback, { maxRetries = 10, delay = 250, backOff = 4 } = {}) => { for (let i = 1; i <= maxRetries; i++) { try { await callback() @@ -7,8 +7,9 @@ export default async (callback, { maxRetries = 10, delay = 250, backOff = 2 } = throw e } - console.log(`Caught error ${e}. Will retry ${maxRetries - i} more times.`) - await new Promise((resolve) => setTimeout(resolve, delay * i * backOff)) + const time = delay * i * backOff + console.log(`Caught error ${e}. Will retry ${maxRetries - i} more times after ${time}ms.`) + await new Promise((resolve) => setTimeout(resolve, time)) } } } \ No newline at end of file diff --git a/test-e2e/spec/hydration.spec.js b/test-e2e/spec/hydration.spec.js index ebc5862..516bd47 100644 --- a/test-e2e/spec/hydration.spec.js +++ b/test-e2e/spec/hydration.spec.js @@ -4,7 +4,7 @@ import { expect, test } from '@playwright/test' import { randomUUID } from 'crypto' import { Api, SubscriptionHooks, SubscriptionHydration, SubscriptionInfo, subscriptionStorage } from '../../lib/index.js' import mongodbClient from '../../test/spec/mongodb-client.js' -import expectWithRetry from './expect-with-retry.js' +import catchAndRetry from './catch-and-retry.js' const client = mongodbClient(27017) const storage = subscriptionStorage({ client }) @@ -26,20 +26,9 @@ test.beforeAll(async () => { test.beforeEach(async () => { apiClientId = randomUUID() - console.log('Creating placeholder for', apiClientId) await subscriptions.addSubscriptionPlaceholder([apiClientId]) }) -test.afterAll(async () => { - const subscriptions = await api.listSubscriptions() - for (let i = 0; i < subscriptions.length; i++) { - const subscription = subscriptions[i] - await api.cancelSubscription(subscription) - } - - return storage.close() -}) - async function createNewSubscription(page, apiClientId) { setTimeout(async () => { const nextYear = new Date().getFullYear() + 1 @@ -110,7 +99,7 @@ test('hydrates the initial payment too', async ({ page }) => { }) subscription = await storage.get([apiClientId]) - await expectWithRetry(async () => { + await catchAndRetry(async () => { // .. expect sub to be not active anymore after we reset all status and payments let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription) expect(sub['52450']).toBeFalsy() @@ -173,7 +162,7 @@ test('provides enough data for a hydrated status to look like a real one', async } }) - await expectWithRetry(async () => { + await catchAndRetry(async () => { subscription = await storage.get([apiClientId]) // .. expect sub to be not active anymore after we reset all status and payments let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription) @@ -217,7 +206,7 @@ test('provides enough data for a hydrated payment to look like a real one', asyn // .. now hydrate status again .. await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], { subscription_id: subscriptionId }, 'checkoutId') - await expectWithRetry(async () => { + await catchAndRetry(async () => { // .. and expect subscription to be active again const subInfo = await subscriptionInfo.getSubscriptionInfo([apiClientId]) const { payments_trail: paymentsTrail } = subInfo['52450'] @@ -267,7 +256,7 @@ test('throws if subscription was created for another client', async ({ page }) = } }) - await expectWithRetry(async () => { + await catchAndRetry(async () => { subscription = await storage.get([apiClientId]) // .. expect sub to be not active anymore after we reset all status and payments let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription) @@ -296,7 +285,7 @@ test('does not hydrate if status created was already received', async ({ page }) let subscription = await storage.get([apiClientId]) const subscriptionId = subscription.status[0].subscription_id - await expectWithRetry(async () => { + await catchAndRetry(async () => { // .. and check subscription is active to make sure setup was correct let sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription) expect(sub['52450']).toBeTruthy() @@ -305,7 +294,7 @@ test('does not hydrate if status created was already received', async ({ page }) // .. now hydrate status again .. await subscriptionHydration.hydrateSubscriptionCreated([apiClientId], { subscription_id: subscriptionId }, 'checkoutId') - await expectWithRetry(async () => { + await catchAndRetry(async () => { // .. and expect subscription to be active again subscription = await storage.get([apiClientId]) expect(subscription.status).toHaveLength(1) @@ -332,7 +321,7 @@ test('hydrate a deleted subscription', async ({ page }) => { await subscriptionHydration.hydrateSubscriptionCancelled([apiClientId], '52450') - await expectWithRetry(async () => { + await catchAndRetry(async () => { subscription = await storage.get([apiClientId]) const sub = await subscriptionInfo.getAllSubscriptionsStatus(subscription, new Date(new Date().getTime() + 1000 * 3600 * 24 * 35)) expect(sub['52450']).toBeFalsy() diff --git a/test-e2e/spec/subscription.spec.js b/test-e2e/spec/subscription.spec.js index 529d455..dddddc6 100644 --- a/test-e2e/spec/subscription.spec.js +++ b/test-e2e/spec/subscription.spec.js @@ -5,7 +5,7 @@ import { expect as chaiExpect } from 'chai' import { randomUUID } from 'crypto' import { Api, SubscriptionHooks, SubscriptionInfo, subscriptionStorage } from '../../lib/index.js' import mongodbClient from '../../test/spec/mongodb-client.js' -import retry from './expect-with-retry.js' +import catchAndRetry from './catch-and-retry.js' const client = mongodbClient(27017) const storage = subscriptionStorage({ client }) @@ -25,17 +25,6 @@ test.beforeEach(async () => { await subscriptions.addSubscriptionPlaceholder([apiClientId]) }) -test.afterAll(async () => { - const subscriptions = await api.listSubscriptions() - for (let i = 0; i < subscriptions.length; i++) { - const subscription = subscriptions[i] - await api.cancelSubscription(subscription) - } - - return storage.close() -}) - - /** * * @param {import('@playwright/test').Page} page @@ -121,7 +110,7 @@ test('test cancel via subscription info and subscription object', async ({ page const success = await subscriptionInfo.cancelSubscription(subscription, '52450') expect(success).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // ... verify subscription still active today ... subscription = await storage.get([apiClientId]) expect(subscription.status).toHaveLength(2) @@ -154,7 +143,7 @@ test('test cancel via subscription info and client id array', async ({ page }) = const success = await subscriptionInfo.cancelSubscription([apiClientId], '52450') expect(success).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // ... verify subscription still active today ... subscription = await storage.get([apiClientId]) expect(subscription.status).toHaveLength(2) @@ -187,7 +176,7 @@ test('test cancel via api', async ({ page }) => { const success = await api.cancelSubscription(subscription.status[0]) expect(success).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // ... verify subscription still active today ... subscription = await storage.get([apiClientId]) expect(subscription.status).toHaveLength(2) @@ -220,7 +209,7 @@ test('test update via subscription info and subscription object', async ({ page const updated = await subscriptionInfo.updateSubscription(subscription, '52450', '52451') expect(updated).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // .. check new status and payments added ... subscription = await storage.get([apiClientId]) expect(subscription).not.toBeFalsy() @@ -250,7 +239,7 @@ test('test update via subscription info and api client id', async ({ page }) => const updated = await subscriptionInfo.updateSubscription([apiClientId], '52450', '52451') expect(updated).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // .. check new status and payments added ... subscription = await storage.get([apiClientId]) expect(subscription).not.toBeFalsy() @@ -279,7 +268,7 @@ test('test update via api', async ({ page }) => { // update subscription plan via api ... await api.updateSubscriptionPlan(subscription.status[0], '52451') - await retry(async () => { + await catchAndRetry(async () => { // .. check new status and payments added ... subscription = await storage.get([apiClientId]) expect(subscription).not.toBeFalsy() @@ -309,7 +298,7 @@ test('test refund via api', async ({ page }) => { const refunded = await api.refundFullPayment(subscription.payments[0]) expect(refunded).toBeTruthy() - await retry(async () => { + await catchAndRetry(async () => { // .. check no new status and payments added ... // .. because refunds need to be reviewed by paddle time // .. it's their money we're playing with @@ -324,7 +313,7 @@ test('test refund via api', async ({ page }) => { expect(sub['52450']).toBeTruthy() }) -test('test create, update, and cancel via ui', async ({ page }) => { +test('test create, update payment, and cancel via ui', async ({ page }) => { // create new subscription and ... await createNewSubscription(page, apiClientId) @@ -339,7 +328,7 @@ test('test create, update, and cancel via ui', async ({ page }) => { // update payment method ... await updatePaymentMethod(page, subscription) - await retry(async () => { + await catchAndRetry(async () => { // .. check no new status or payments added ... subscription = await storage.get([apiClientId]) expect(subscription).not.toBeFalsy() @@ -354,7 +343,7 @@ test('test create, update, and cancel via ui', async ({ page }) => { // cancel subscription ... await cancelSubscription(page, subscription) - await retry(async () => { + await catchAndRetry(async () => { // ... verify subscription still active today ... subscription = await storage.get([apiClientId]) expect(subscription.status).toHaveLength(2) @@ -373,11 +362,11 @@ test('test create, update, and cancel via ui', async ({ page }) => { async function getAndValidateSubscription() { let subscription - await retry(async () => { + await catchAndRetry(async () => { subscription = await storage.get([apiClientId]) expect(subscription).not.toBeFalsy() expect(subscription.status).toHaveLength(1) expect(subscription.payments).toHaveLength(1) - }) + }, { maxRetries: 15 }) return subscription }