Skip to content

Commit

Permalink
fixup: reduce code duplications
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Schrottner <[email protected]>
  • Loading branch information
aepfli committed Oct 27, 2024
1 parent 5e5eac9 commit 1e5c631
Show file tree
Hide file tree
Showing 15 changed files with 46 additions and 44 deletions.
14 changes: 3 additions & 11 deletions libs/providers/flagd-web/src/e2e/constants.ts
Original file line number Diff line number Diff line change
@@ -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 `<rootdir>/../../../../../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');
6 changes: 3 additions & 3 deletions libs/providers/flagd-web/src/e2e/step-definitions/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 3 additions & 5 deletions libs/providers/flagd-web/src/e2e/tests/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -22,10 +23,7 @@ async function setup() {
tls: false,
maxRetries: -1,
});
await OpenFeature.setProviderAndWait(
E2E_CLIENT_NAME,
flagdWebProvider,
);
await OpenFeature.setProviderAndWait(E2E_CLIENT_NAME, flagdWebProvider);
assert(
OpenFeature.getProviderMetadata(E2E_CLIENT_NAME).name === FLAGD_NAME,
new Error(
Expand Down
4 changes: 3 additions & 1 deletion libs/providers/flagd-web/src/e2e/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -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
}
}
10 changes: 2 additions & 8 deletions libs/providers/flagd/src/e2e/constants.ts
Original file line number Diff line number Diff line change
@@ -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 `<rootdir>/../../../../../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');
Expand Down
6 changes: 4 additions & 2 deletions libs/providers/flagd/src/e2e/step-definitions/reconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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', () => {
Expand Down
13 changes: 10 additions & 3 deletions libs/providers/flagd/src/e2e/tests/in-process-reconnect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 () => {
Expand Down
3 changes: 1 addition & 2 deletions libs/providers/flagd/src/e2e/tests/in-process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions libs/providers/flagd/src/e2e/tests/rpc-reconnect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 1 addition & 2 deletions libs/providers/flagd/src/e2e/tests/rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/flagd-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions libs/shared/flagd-core/src/e2e/index.ts
Original file line number Diff line number Diff line change
@@ -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 `<rootdir>/../../../../../shared/flagd-core/${modulePath}${file}`;
}
1 change: 1 addition & 0 deletions libs/shared/flagd-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './lib/flagd-core';
export * from './lib/feature-flag';
export * from './lib/storage';
export * from './e2e';
2 changes: 1 addition & 1 deletion libs/shared/flagd-core/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
2 changes: 1 addition & 1 deletion libs/shared/flagd-core/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit 1e5c631

Please sign in to comment.