From 3c4e71ae5efc60de20ae7dc27a09ca60e7ab7095 Mon Sep 17 00:00:00 2001 From: Simon Schrottner Date: Sun, 27 Oct 2024 14:27:42 +0100 Subject: [PATCH] fixup: reduce code duplications Signed-off-by: Simon Schrottner --- libs/providers/flagd-web/src/e2e/constants.ts | 14 +++----------- .../flagd-web/src/e2e/step-definitions/flag.ts | 6 +++--- .../flagd-web/src/e2e/tests/provider.spec.ts | 3 ++- libs/providers/flagd-web/src/e2e/tsconfig.lib.json | 4 +++- libs/providers/flagd/src/e2e/constants.ts | 10 ++-------- .../flagd/src/e2e/step-definitions/reconnect.ts | 6 ++++-- .../src/e2e/tests/in-process-reconnect.spec.ts | 13 ++++++++++--- .../flagd/src/e2e/tests/in-process.spec.ts | 3 +-- .../flagd/src/e2e/tests/rpc-reconnect.spec.ts | 6 +++--- libs/providers/flagd/src/e2e/tests/rpc.spec.ts | 3 +-- libs/shared/flagd-core/src/e2e/index.ts | 8 ++++++++ libs/shared/flagd-core/src/index.ts | 1 + libs/shared/flagd-core/tsconfig.lib.json | 2 +- libs/shared/flagd-core/tsconfig.spec.json | 2 +- 14 files changed, 43 insertions(+), 38 deletions(-) create mode 100644 libs/shared/flagd-core/src/e2e/index.ts diff --git a/libs/providers/flagd-web/src/e2e/constants.ts b/libs/providers/flagd-web/src/e2e/constants.ts index ed8a7da71..d51d19f0e 100644 --- a/libs/providers/flagd-web/src/e2e/constants.ts +++ b/libs/providers/flagd-web/src/e2e/constants.ts @@ -1,13 +1,5 @@ -export const FLAGD_NAME = 'flagd-web'; -export const E2E_CLIENT_NAME = 'e2e'; - -export const IMAGE_VERSION = 'v0.5.13'; +import { getGherkinTestPath } from '@openfeature/flagd-core'; -export function getGherkinTestPath(file: string, modulePath = 'test-harness/gherkin/'): string { - // TODO: find a way to resolve this in a generic manner - currently this works, because of the file structure - return `/../../../../../shared/flagd-core/${modulePath}${file}`; -} +export const FLAGD_NAME = 'flagd-web'; -export const GHERKIN_EVALUATION_FEATURE = getGherkinTestPath( - 'flagd.feature' -); +export const GHERKIN_EVALUATION_FEATURE = getGherkinTestPath('flagd.feature'); diff --git a/libs/providers/flagd-web/src/e2e/step-definitions/flag.ts b/libs/providers/flagd-web/src/e2e/step-definitions/flag.ts index 2390365e4..da2bc9e2e 100644 --- a/libs/providers/flagd-web/src/e2e/step-definitions/flag.ts +++ b/libs/providers/flagd-web/src/e2e/step-definitions/flag.ts @@ -8,7 +8,7 @@ import { ProviderEvents, StandardResolutionReasons, } from '@openfeature/web-sdk'; -import { E2E_CLIENT_NAME } from '../constants'; +import { E2E_CLIENT_NAME } from '@openfeature/flagd-core'; export const flagStepDefinitions: StepDefinitions = ({ given, and, when, then }) => { let flagKey: string; @@ -286,8 +286,8 @@ export const flagStepDefinitions: StepDefinitions = ({ given, and, when, then }) await new Promise((resolve) => setTimeout(resolve, 3000)); }); - then('the PROVIDER_CONFIGURATION_CHANGED handler must run', () => { - expect(ran).toBeTruthy(); + then('the PROVIDER_CONFIGURATION_CHANGED handler must run', async () => { + expect(await ran).toBeTruthy(); }); and(/^the event details must indicate "(.*)" was altered$/, (flagName) => { diff --git a/libs/providers/flagd-web/src/e2e/tests/provider.spec.ts b/libs/providers/flagd-web/src/e2e/tests/provider.spec.ts index 21f800869..4a3d2c2ad 100644 --- a/libs/providers/flagd-web/src/e2e/tests/provider.spec.ts +++ b/libs/providers/flagd-web/src/e2e/tests/provider.spec.ts @@ -3,8 +3,9 @@ import { OpenFeature } from '@openfeature/web-sdk'; import { GenericContainer, StartedTestContainer } from 'testcontainers'; import { FlagdWebProvider } from '../../lib/flagd-web-provider'; import { autoBindSteps, loadFeature } from 'jest-cucumber'; -import { E2E_CLIENT_NAME, FLAGD_NAME, GHERKIN_EVALUATION_FEATURE, IMAGE_VERSION } from '../constants'; +import { FLAGD_NAME, GHERKIN_EVALUATION_FEATURE } from '../constants'; import { flagStepDefinitions } from '../step-definitions'; +import { E2E_CLIENT_NAME, IMAGE_VERSION } from '@openfeature/flagd-core'; // register the flagd provider before the tests. async function setup() { diff --git a/libs/providers/flagd-web/src/e2e/tsconfig.lib.json b/libs/providers/flagd-web/src/e2e/tsconfig.lib.json index ab38a5fb9..1211629d6 100644 --- a/libs/providers/flagd-web/src/e2e/tsconfig.lib.json +++ b/libs/providers/flagd-web/src/e2e/tsconfig.lib.json @@ -1,10 +1,12 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "lib": ["ES2015", "DOM"], + "lib": ["ES2022", "DOM"], "outDir": "../../../dist/out-tsc", "declaration": true, "types": ["jest"], "allowSyntheticDefaultImports": true, + "allowJs" :true, + "resolveJsonModule": true } } diff --git a/libs/providers/flagd/src/e2e/constants.ts b/libs/providers/flagd/src/e2e/constants.ts index 0707dfcc9..b21aa50b2 100644 --- a/libs/providers/flagd/src/e2e/constants.ts +++ b/libs/providers/flagd/src/e2e/constants.ts @@ -1,15 +1,9 @@ +import { getGherkinTestPath } from "@openfeature/flagd-core"; + export const FLAGD_NAME = 'flagd Provider'; -export const E2E_CLIENT_NAME = 'e2e'; export const UNSTABLE_CLIENT_NAME = 'unstable'; export const UNAVAILABLE_CLIENT_NAME = 'unavailable'; -export const IMAGE_VERSION = 'v0.5.6'; - -export function getGherkinTestPath(file: string, modulePath = 'test-harness/gherkin/'): string { - // TODO: find a way to resolve this in a generic manner - currently this works, because of the file structure - return `/../../../../../shared/flagd-core/${modulePath}${file}`; -} - export const GHERKIN_FLAGD_FEATURE = getGherkinTestPath('flagd.feature'); export const GHERKIN_FLAGD_JSON_EVALUATOR_FEATURE = getGherkinTestPath('flagd-json-evaluator.feature'); export const GHERKIN_FLAGD_RECONNECT_FEATURE = getGherkinTestPath('flagd-reconnect.feature'); diff --git a/libs/providers/flagd/src/e2e/step-definitions/reconnect.ts b/libs/providers/flagd/src/e2e/step-definitions/reconnect.ts index e27db6398..69e06c35a 100644 --- a/libs/providers/flagd/src/e2e/step-definitions/reconnect.ts +++ b/libs/providers/flagd/src/e2e/step-definitions/reconnect.ts @@ -12,6 +12,8 @@ export const reconnectStepDefinitions: StepDefinitions = ({ given, and, when, th let readyRunCount = 0; let errorHandlerRun = 0; + const retryDelayMs = 1000; + beforeAll((done) => { client.addHandler(ProviderEvents.Ready, () => { done(); @@ -32,11 +34,11 @@ export const reconnectStepDefinitions: StepDefinitions = ({ given, and, when, th expect(readyRunCount).toEqual(1); }); and("the PROVIDER_ERROR handler must run when the provider's connection is lost", async () => { - await new Promise((resolve) => setTimeout(resolve, 10000)); + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); expect(errorRunCount).toBeGreaterThan(0); }); and('when the connection is reestablished the PROVIDER_READY handler must run again', async () => { - await new Promise((resolve) => setTimeout(resolve, 10000)); + await new Promise((resolve) => setTimeout(resolve, retryDelayMs)); expect(readyRunCount).toBeGreaterThan(1); }); when('a flagd provider is set and initialization is awaited', () => { diff --git a/libs/providers/flagd/src/e2e/tests/in-process-reconnect.spec.ts b/libs/providers/flagd/src/e2e/tests/in-process-reconnect.spec.ts index 7751a4fbc..fcf19b918 100644 --- a/libs/providers/flagd/src/e2e/tests/in-process-reconnect.spec.ts +++ b/libs/providers/flagd/src/e2e/tests/in-process-reconnect.spec.ts @@ -4,12 +4,12 @@ import { FlagdProvider } from '../../lib/flagd-provider'; import { GenericContainer, StartedTestContainer } from 'testcontainers'; import { autoBindSteps, loadFeature } from 'jest-cucumber'; import { - UNSTABLE_CLIENT_NAME, - UNAVAILABLE_CLIENT_NAME, FLAGD_NAME, GHERKIN_FLAGD_RECONNECT_FEATURE, - IMAGE_VERSION, + UNAVAILABLE_CLIENT_NAME, + UNSTABLE_CLIENT_NAME, } from '../constants'; +import { IMAGE_VERSION } from '@openfeature/flagd-core'; import { reconnectStepDefinitions } from '../step-definitions'; // register the flagd provider before the tests. @@ -52,6 +52,13 @@ async function setup() { } jest.setTimeout(30000); +/** + * This describe block and retry settings are calibrated to gRPC's retry time + * and our testing container's restart cadence. + */ +const retryTimes = 240; +jest.retryTimes(retryTimes); + describe('in process', () => { let containers: StartedTestContainer[] = []; beforeAll(async () => { diff --git a/libs/providers/flagd/src/e2e/tests/in-process.spec.ts b/libs/providers/flagd/src/e2e/tests/in-process.spec.ts index bab16ff50..4ed01470f 100644 --- a/libs/providers/flagd/src/e2e/tests/in-process.spec.ts +++ b/libs/providers/flagd/src/e2e/tests/in-process.spec.ts @@ -4,14 +4,13 @@ import { FlagdProvider } from '../../lib/flagd-provider'; import { GenericContainer, StartedTestContainer } from 'testcontainers'; import { autoBindSteps, loadFeature } from 'jest-cucumber'; import { - E2E_CLIENT_NAME, FLAGD_NAME, GHERKIN_EVALUATION_FEATURE, GHERKIN_FLAGD_FEATURE, GHERKIN_FLAGD_JSON_EVALUATOR_FEATURE, - IMAGE_VERSION, } from '../constants'; import { flagStepDefinitions } from '../step-definitions'; +import { E2E_CLIENT_NAME, IMAGE_VERSION } from '@openfeature/flagd-core'; // register the flagd provider before the tests. async function setup() { diff --git a/libs/providers/flagd/src/e2e/tests/rpc-reconnect.spec.ts b/libs/providers/flagd/src/e2e/tests/rpc-reconnect.spec.ts index 159642a7e..dbf27fc4e 100644 --- a/libs/providers/flagd/src/e2e/tests/rpc-reconnect.spec.ts +++ b/libs/providers/flagd/src/e2e/tests/rpc-reconnect.spec.ts @@ -4,13 +4,13 @@ import { FlagdProvider } from '../../lib/flagd-provider'; import { GenericContainer, StartedTestContainer } from 'testcontainers'; import { autoBindSteps, loadFeature } from 'jest-cucumber'; import { - UNSTABLE_CLIENT_NAME, - UNAVAILABLE_CLIENT_NAME, FLAGD_NAME, GHERKIN_FLAGD_RECONNECT_FEATURE, - IMAGE_VERSION, + UNAVAILABLE_CLIENT_NAME, + UNSTABLE_CLIENT_NAME, } from '../constants'; import { reconnectStepDefinitions } from '../step-definitions'; +import { IMAGE_VERSION } from '@openfeature/flagd-core'; // register the flagd provider before the tests. async function setup() { diff --git a/libs/providers/flagd/src/e2e/tests/rpc.spec.ts b/libs/providers/flagd/src/e2e/tests/rpc.spec.ts index 89ceb82a3..6068f286e 100644 --- a/libs/providers/flagd/src/e2e/tests/rpc.spec.ts +++ b/libs/providers/flagd/src/e2e/tests/rpc.spec.ts @@ -4,13 +4,12 @@ import { FlagdProvider } from '../../lib/flagd-provider'; import { GenericContainer, StartedTestContainer } from 'testcontainers'; import { autoBindSteps, loadFeature } from 'jest-cucumber'; import { - E2E_CLIENT_NAME, FLAGD_NAME, GHERKIN_EVALUATION_FEATURE, GHERKIN_FLAGD_FEATURE, GHERKIN_FLAGD_JSON_EVALUATOR_FEATURE, - IMAGE_VERSION, } from '../constants'; +import { E2E_CLIENT_NAME, IMAGE_VERSION } from '@openfeature/flagd-core'; import { flagStepDefinitions } from '../step-definitions'; // register the flagd provider before the tests. diff --git a/libs/shared/flagd-core/src/e2e/index.ts b/libs/shared/flagd-core/src/e2e/index.ts new file mode 100644 index 000000000..003cb1559 --- /dev/null +++ b/libs/shared/flagd-core/src/e2e/index.ts @@ -0,0 +1,8 @@ +export const E2E_CLIENT_NAME = 'e2e'; + +export const IMAGE_VERSION = 'v0.5.13'; + +export function getGherkinTestPath(file: string, modulePath = 'test-harness/gherkin/'): string { + // TODO: find a way to resolve this in a generic manner - currently this works, because of the file structure + return `/../../../../../shared/flagd-core/${modulePath}${file}`; +} diff --git a/libs/shared/flagd-core/src/index.ts b/libs/shared/flagd-core/src/index.ts index 71e12e9e7..7d97e85da 100644 --- a/libs/shared/flagd-core/src/index.ts +++ b/libs/shared/flagd-core/src/index.ts @@ -1,3 +1,4 @@ export * from './lib/flagd-core'; export * from './lib/feature-flag'; export * from './lib/storage'; +export * from './e2e'; diff --git a/libs/shared/flagd-core/tsconfig.lib.json b/libs/shared/flagd-core/tsconfig.lib.json index 36e88999e..075a857b0 100644 --- a/libs/shared/flagd-core/tsconfig.lib.json +++ b/libs/shared/flagd-core/tsconfig.lib.json @@ -6,5 +6,5 @@ "types": [] }, "include": ["src/**/*.ts"], - "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"] + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts", "src/e2e"] } diff --git a/libs/shared/flagd-core/tsconfig.spec.json b/libs/shared/flagd-core/tsconfig.spec.json index b2ee74a6b..0dceea5a8 100644 --- a/libs/shared/flagd-core/tsconfig.spec.json +++ b/libs/shared/flagd-core/tsconfig.spec.json @@ -5,5 +5,5 @@ "module": "commonjs", "types": ["jest", "node"] }, - "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"] + "include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts", "src/e2e"] }