Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(flagd): rework e2e tests to new format #1129

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libs/providers/flagd/src/e2e/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export const FLAGD_NAME = 'flagd';
export const UNSTABLE_CLIENT_NAME = 'unstable';
export const UNAVAILABLE_CLIENT_NAME = 'unavailable';

export const GHERKIN_FLAGD = getGherkinTestPath('*.feature');
export const GHERKIN_CONFIG = getGherkinTestPath('config.feature');
export const GHERKIN_FLAGD_FEATURE = getGherkinTestPath('flagd.feature');
export const GHERKIN_FLAGD_TESTING = getGherkinTestPath('testing.feature');
export const GHERKIN_FLAGD_JSON_EVALUATOR_FEATURE = getGherkinTestPath('flagd-json-evaluator.feature');
export const GHERKIN_FLAGD_RECONNECT_FEATURE = getGherkinTestPath('flagd-reconnect.feature');
export const GHERKIN_EVALUATION_FEATURE = getGherkinTestPath(
Expand Down
46 changes: 46 additions & 0 deletions libs/providers/flagd/src/e2e/step-definitions/configSteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { StepsDefinitionCallbackOptions } from 'jest-cucumber/dist/src/feature-definition-creation';
import { State, Steps } from './state';
import { CacheOption, getConfig, ResolverType } from '../../lib/configuration';
import { mapValueToType } from "./utils";

export const configSteps: Steps = (state: State) => {
function mapName(name: string): string {
switch (name) {
case 'resolver':
return 'resolverType';
default:
return name;
}
}

return ({ given, when, then }: StepsDefinitionCallbackOptions) => {
beforeEach(() => {
state.options = {};
});
given(/^an option "(.*)" of type "(.*)" with value "(.*)"$/, (name: string, type: string, value: string) => {
state.options[mapName(name)] = mapValueToType(value, type);
});
given(/^an environment variable "(.*)" with value "(.*)"$/, (name, value) => {
process.env[name] = value;
});
when('a config was initialized', () => {
state.config = getConfig(state.options);
});

then(
/^the option "(.*)" of type "(.*)" should have the value "(.*)"$/,
(name: string, type: string, value: string) => {
const expected = mapValueToType(value, type);
const propertyName = mapName(name);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const configElement = state.config[propertyName];
expect(configElement).toBe(expected);
},
);

then('we should have an error', () => {

});
};
};
32 changes: 32 additions & 0 deletions libs/providers/flagd/src/e2e/step-definitions/contextSteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { State, Steps } from './state';
import { mapValueToType } from './utils';

export const contextSteps: Steps =
(state: State) =>
({ given, when, then }) => {
beforeEach(() => (state.context = undefined));
given(
/^a context containing a key "(.*)", with type "(.*)" and with value "(.*)"$/,
(key: string, type: string, value: string) => {
if (state.context == undefined) {
state.context = {};
}
state.context[key] = mapValueToType(value, type);
},
);
given(
/^a context containing a nested property with outer key "(.*)" and inner key "(.*)", with value "(.*)"$/,
(outer, inner, value) => {
if (state.context == undefined) {
state.context = {};
}
state.context[outer] = { [inner]: value };
},
);
given(/^a context containing a targeting key with value "(.*)"$/, (key) => {
if (state.context == undefined) {
state.context = {};
}
state.context.targetingKey = key;
});
};
58 changes: 58 additions & 0 deletions libs/providers/flagd/src/e2e/step-definitions/eventSteps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { State, Steps } from './state';
import { ConfigChangeEvent, ServerProviderEvents } from "@openfeature/core";
import { waitFor } from './utils';

export const eventSteps: Steps =
(state: State) =>
({ given, when, then }) => {
beforeEach(() => {
state.events = [];
});

function map(eventType: string): ServerProviderEvents {
switch (eventType) {
case 'error':
return ServerProviderEvents.Error;
case 'ready':
return ServerProviderEvents.Ready;
case 'stale':
return ServerProviderEvents.Stale;
case 'change':
return ServerProviderEvents.ConfigurationChanged;

default:
throw new Error('unknown eventtype');
}
}

given(/a (.*) event handler/, async (type: string) => {
state.client?.addHandler(map(type), (details) => {
state.events.push({ type, details });
});
});

then(/^the (.*) event handler should have been executed$/, async (type: string) => {
await waitFor(() => expect(state.events.find((value) => value.type == type)).toBeDefined(), { timeout: 20000 });
expect(state.events.find((value) => value.type == type)).toBeDefined();
state.events = [];
});

then(/^the (.*) event handler should have been executed within (\d+)ms$/, async (type: string, ms: number) => {
await waitFor(() => expect(state.events.find((value) => value.type == type)).toBeDefined(), { timeout: ms });
const actual = state.events.find((value) => value.type == type);
expect(actual).toBeDefined();
state.events = [];
console.error('here bin cih');
});

when(/^a (.*) event was fired$/, async (type: string) => {
await waitFor(() => expect(state.events.find((value) => value.type == type)), { timeout: 2000 });
expect(state.events.find((value) => value.type == type)).toBeDefined();
state.events = [];
});

then('the flag should be part of the event payload', async () => {
await waitFor(() => expect(state.events.find((value) => value.type == 'change')), { timeout: 2000 });
state.events = [];
});
};
Loading
Loading