Skip to content

Commit

Permalink
feat(autBindSteps): add the ability to load a single feature via auto…
Browse files Browse the repository at this point in the history
…BindStep
  • Loading branch information
pplancq committed Mar 26, 2024
1 parent c268e52 commit 942d31f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 11 additions & 0 deletions docs/AutomaticStepBinding.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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]);
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/automatic-step-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 942d31f

Please sign in to comment.