Skip to content

Commit

Permalink
feat(autoBindSteps): add a context for sharing data between stages
Browse files Browse the repository at this point in the history
  • Loading branch information
pplancq committed Mar 28, 2024
1 parent a3954ef commit fb316ca
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { autoBindSteps, loadFeature, StepDefinitionsWithContext } from '../../../../../src';
import { VendingMachine } from '../../../src/vending-machine';

type Context = {
vendingMachine: VendingMachine;
};

export const vendingMachineSteps: StepDefinitionsWithContext<Context> = ({ given, and, when, then, context }) => {
given(/^the vending machine has "(.*)" in stock$/, (itemName: string) => {
context.vendingMachine = new VendingMachine();
context.vendingMachine.stockItem(itemName, 1);
});

and('I have inserted the correct amount of money', () => {
context.vendingMachine.insertMoney(0.5);
});

when(/^I purchase "(.*)"$/, (itemName: string) => {
context.vendingMachine.dispenseItem(itemName);
});

then(/^my "(.*)" should be dispensed$/, (itemName: string) => {
const inventoryAmount = context.vendingMachine.items[itemName];
expect(inventoryAmount).toBe(0);
});
};

const features = loadFeature('./examples/typescript/specs/features/auto-binding/snack-vending-machine.feature');

autoBindSteps(features, [vendingMachineSteps]);
14 changes: 12 additions & 2 deletions src/automatic-step-binding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ParsedFeature } from './models';
import { matchSteps } from './validation/step-definition-validation';
import { StepsDefinitionCallbackFunction, createDefineFeature, IJestLike } from './feature-definition-creation';
import {
createDefineFeature,
IJestLike,
type StepsDefinitionCallbackFunctionWithContext,
} from './feature-definition-creation';
import { generateStepCode } from './code-generation/step-generation';

const globalSteps: Array<{ stepMatcher: string | RegExp; stepFunction: () => unknown }> = [];
Expand All @@ -12,12 +16,17 @@ const registerStep = (stepMatcher: string | RegExp, stepFunction: () => unknown)
export const createAutoBindSteps = (jestLike: IJestLike) => {
const defineFeature = createDefineFeature(jestLike);

return (parsedFeatures: ParsedFeature | ParsedFeature[], stepDefinitions: StepsDefinitionCallbackFunction[]) => {
return <C extends NonNullable<unknown> = NonNullable<unknown>>(
parsedFeatures: ParsedFeature | ParsedFeature[],
stepDefinitions: StepsDefinitionCallbackFunctionWithContext<C>[],
) => {
let features = parsedFeatures;
if (!Array.isArray(features)) {
features = [features];
}

const context = {} as C;

stepDefinitions.forEach(stepDefinitionCallback => {
stepDefinitionCallback({
defineStep: registerStep,
Expand All @@ -29,6 +38,7 @@ export const createAutoBindSteps = (jestLike: IJestLike) => {
pending: () => {
// Nothing to do
},
context,
});
});

Expand Down
8 changes: 8 additions & 0 deletions src/feature-definition-creation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type StepsDefinitionCallbackOptions = {
pending: () => void;
};

export type StepsDefinitionCallbackOptionsWithContext<C extends NonNullable<unknown> = NonNullable<unknown>> =
StepsDefinitionCallbackOptions & {
context: C;
};

export interface IJestLike {
describe: jest.Describe;
test: jest.It;
Expand All @@ -44,6 +49,9 @@ export type DefineScenarioFunctionWithAliases = DefineScenarioFunction & {
};

export type StepsDefinitionCallbackFunction = (options: StepsDefinitionCallbackOptions) => void;
export type StepsDefinitionCallbackFunctionWithContext<C extends NonNullable<unknown> = NonNullable<unknown>> = (
options: StepsDefinitionCallbackOptionsWithContext<C>,
) => void;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type DefineStepFunction = (stepMatcher: string | RegExp, stepDefinitionCallback: (...args: any[]) => any) => any;
export type DefineFeatureFunction = (
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export {
generateCodeFromFeature,
generateCodeWithSeparateFunctionsFromFeature,
} from './code-generation/generate-code-by-line-number';
export { StepsDefinitionCallbackFunction as StepDefinitions, IJestLike } from './feature-definition-creation';
export {
StepsDefinitionCallbackFunction as StepDefinitions,
StepsDefinitionCallbackFunctionWithContext as StepDefinitionsWithContext,
IJestLike,
} from './feature-definition-creation';

const jestLike: IJestLike = {
describe,
Expand Down

0 comments on commit fb316ca

Please sign in to comment.