From 942d31f04e24b52a4f0ca921f791a820e950d27c Mon Sep 17 00:00:00 2001 From: pplancq Date: Sun, 24 Mar 2024 10:25:04 +0100 Subject: [PATCH] feat(autBindSteps): add the ability to load a single feature via autoBindStep --- docs/AutomaticStepBinding.md | 11 ++++++++ ...uto-step-binding-beverage-vending.steps.ts | 28 +++++++++++++++++++ .../auto-step-binding.steps.ts | 4 +-- src/automatic-step-binding.ts | 7 ++++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 examples/typescript/specs/step-definitions/auto-binding/auto-step-binding-beverage-vending.steps.ts rename examples/typescript/specs/step-definitions/{ => auto-binding}/auto-step-binding.steps.ts (91%) diff --git a/docs/AutomaticStepBinding.md b/docs/AutomaticStepBinding.md index e6b5ac6a..a5ab8ef4 100644 --- a/docs/AutomaticStepBinding.md +++ b/docs/AutomaticStepBinding.md @@ -103,6 +103,17 @@ If you are using `autoBindSteps` with just a subset of your feature files (as de ], ``` +It's also possible to load a single feature via `autoBinSteps`, so you can keep the test parallelization that jest offers. + +```javascript +import { loadFeature, autoBindSteps } from 'jest-cucumber'; + +import { vendingMachineSteps } from 'specs/step-definitions/vending-machine-steps'; + +const feature = loadFeature('specs/features/vending-machine.feature'); +autoBindSteps(feature, [ vendingMachineSteps ]); +``` + ## How it works `loadFeatures` and `autoBindSteps` are utility functions that automate what you would normally do per feature file by calling `loadFeature`, then `defineFeature`, and then defining one `test` per scenario containing inline step definitions. diff --git a/examples/typescript/specs/step-definitions/auto-binding/auto-step-binding-beverage-vending.steps.ts b/examples/typescript/specs/step-definitions/auto-binding/auto-step-binding-beverage-vending.steps.ts new file mode 100644 index 00000000..4ec3a599 --- /dev/null +++ b/examples/typescript/specs/step-definitions/auto-binding/auto-step-binding-beverage-vending.steps.ts @@ -0,0 +1,28 @@ +import { StepDefinitions, loadFeature, autoBindSteps } from '../../../../../src'; +import { VendingMachine } from '../../../src/vending-machine'; + +export const vendingMachineSteps: StepDefinitions = ({ given, and, when, then }) => { + let vendingMachine: VendingMachine; + + given(/^the vending machine has "(.*)" in stock$/, (itemName: string) => { + vendingMachine = new VendingMachine(); + vendingMachine.stockItem(itemName, 1); + }); + + and('I have inserted the correct amount of money', () => { + vendingMachine.insertMoney(0.5); + }); + + when(/^I purchase "(.*)"$/, (itemName: string) => { + vendingMachine.dispenseItem(itemName); + }); + + then(/^my "(.*)" should be dispensed$/, (itemName: string) => { + const inventoryAmount = vendingMachine.items[itemName]; + expect(inventoryAmount).toBe(0); + }); +}; + +const features = loadFeature('./examples/typescript/specs/features/auto-binding/beverage-vending-machine.feature'); + +autoBindSteps(features, [vendingMachineSteps]); diff --git a/examples/typescript/specs/step-definitions/auto-step-binding.steps.ts b/examples/typescript/specs/step-definitions/auto-binding/auto-step-binding.steps.ts similarity index 91% rename from examples/typescript/specs/step-definitions/auto-step-binding.steps.ts rename to examples/typescript/specs/step-definitions/auto-binding/auto-step-binding.steps.ts index 593e2b33..c842c9d1 100644 --- a/examples/typescript/specs/step-definitions/auto-step-binding.steps.ts +++ b/examples/typescript/specs/step-definitions/auto-binding/auto-step-binding.steps.ts @@ -1,5 +1,5 @@ -import { StepDefinitions, loadFeatures, autoBindSteps } from '../../../../src'; -import { VendingMachine } from '../../src/vending-machine'; +import { StepDefinitions, loadFeatures, autoBindSteps } from '../../../../../src'; +import { VendingMachine } from '../../../src/vending-machine'; export const vendingMachineSteps: StepDefinitions = ({ given, and, when, then }) => { let vendingMachine: VendingMachine; diff --git a/src/automatic-step-binding.ts b/src/automatic-step-binding.ts index 119b30aa..420124bf 100644 --- a/src/automatic-step-binding.ts +++ b/src/automatic-step-binding.ts @@ -12,7 +12,12 @@ const registerStep = (stepMatcher: string | RegExp, stepFunction: () => unknown) export const createAutoBindSteps = (jestLike: IJestLike) => { const defineFeature = createDefineFeature(jestLike); - return (features: ParsedFeature[], stepDefinitions: StepsDefinitionCallbackFunction[]) => { + return (parsedFeatures: ParsedFeature | ParsedFeature[], stepDefinitions: StepsDefinitionCallbackFunction[]) => { + let features = parsedFeatures; + if (!Array.isArray(features)) { + features = [features]; + } + stepDefinitions.forEach(stepDefinitionCallback => { stepDefinitionCallback({ defineStep: registerStep,