From dc524b773b9a68e5199a2be5fbc2fbb058b53c97 Mon Sep 17 00:00:00 2001 From: Martijn Date: Thu, 24 Oct 2024 12:50:06 +0200 Subject: [PATCH] feat(invoices): moved waitFor util to test dir --- packages/test/src/test-helpers.ts | 26 ++++++++++++++++ .../vendure-plugin-invoices/test/e2e.spec.ts | 2 +- .../test/test-helpers.ts | 31 +------------------ 3 files changed, 28 insertions(+), 31 deletions(-) create mode 100644 packages/test/src/test-helpers.ts diff --git a/packages/test/src/test-helpers.ts b/packages/test/src/test-helpers.ts new file mode 100644 index 00000000..73ba4fb7 --- /dev/null +++ b/packages/test/src/test-helpers.ts @@ -0,0 +1,26 @@ +/** + * This helper keeps polling a condition function until it returns a value or times out. + * Use this instead of arbitrary `await new Promise()` calls. + * + * @example + * // This invoice will be defined, or an error is thrown after timeout + * const invoice = await waitFor(() => getInvoice(order.id)); + */ +export async function waitFor( + conditionFn: () => Promise | T | undefined, + interval = 100, + timeout = 10000 +) { + const startTime = Date.now(); + let result: T | undefined; + let elapsedTime = 0; + while (elapsedTime < timeout) { + result = await conditionFn(); + if (result) { + return result; + } + elapsedTime = Date.now() - startTime; + await new Promise((resolve) => setTimeout(resolve, interval)); + } + throw new Error(`'waitFor()' Failed to resolve a value after ${timeout}ms`); +} diff --git a/packages/vendure-plugin-invoices/test/e2e.spec.ts b/packages/vendure-plugin-invoices/test/e2e.spec.ts index 81bac8ef..775656b9 100644 --- a/packages/vendure-plugin-invoices/test/e2e.spec.ts +++ b/packages/vendure-plugin-invoices/test/e2e.spec.ts @@ -43,7 +43,7 @@ import { } from '../src/ui/queries.graphql'; import { MockAccountingStrategy } from './mock-accounting-strategy'; import gql from 'graphql-tag'; -import { waitFor } from '../../vendure-plugin-shipping-extensions/test/test-helpers'; +import { waitFor } from '../../test/src/test-helpers'; let server: TestServer; let adminClient: SimpleGraphQLClient; diff --git a/packages/vendure-plugin-shipping-extensions/test/test-helpers.ts b/packages/vendure-plugin-shipping-extensions/test/test-helpers.ts index 30136a05..fae0cff3 100644 --- a/packages/vendure-plugin-shipping-extensions/test/test-helpers.ts +++ b/packages/vendure-plugin-shipping-extensions/test/test-helpers.ts @@ -1,41 +1,12 @@ import { defaultShippingEligibilityChecker, Promotion } from '@vendure/core'; import { SimpleGraphQLClient } from '@vendure/testing'; -import { distanceBasedShippingCalculator } from '../src/config/distance-based-shipping-calculator'; import { gql } from 'graphql-tag'; -import { ConfigArgInput } from '../../test/src/generated/shop-graphql'; import { CreatePromotion, CreatePromotionMutation, CreatePromotionMutationVariables, - LanguageCode, } from '../../test/src/generated/admin-graphql'; - -/** - * This helper keeps polling a condition function until it returns a value or times out. - * Use this instead of arbitrary `await new Promise()` calls. - * - * @example - * // This invoice will be defined, or an error is thrown after timeout - * const invoice = await waitFor(() => getInvoice(order.id)); - */ -export async function waitFor( - conditionFn: () => Promise | T | undefined, - interval = 100, - timeout = 10000 -) { - const startTime = Date.now(); - let result: T | undefined; - let elapsedTime = 0; - while (elapsedTime < timeout) { - result = await conditionFn(); - if (result) { - return result; - } - elapsedTime = Date.now() - startTime; - await new Promise((resolve) => setTimeout(resolve, interval)); - } - throw new Error(`'waitFor()' Failed to resolve a value after ${timeout}ms`); -} +import { distanceBasedShippingCalculator } from '../src/config/distance-based-shipping-calculator'; const CREATE_SHIPPING_METHOD = gql` mutation CreateShippingMethod($input: CreateShippingMethodInput!) {