Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cucumber-fp

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cucumber-fp

Cucumber.js with functional programming-style step definitions

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

Cucumber.js FP step definitions

build

This little library brings functional programming style step definitions to Cucumber.js.

Install

npm install --save-dev cucumber-fp

Install Cucumber.js if you haven't done so already:

npm install --save-dev @cucumber/cucumber

Usage

Instead of using the regular step definition functions from Cucumber, call withContext to initialise a context and get FP-aware functions:

import { withContext } from 'cucumber-fp'

const { Given, When, Then } = withContext({ a: 0 })

Given('a step', (ctx) => ({ ...ctx, a: 1 }))
When('another step', (ctx) => ({ ...ctx, b: 2 }))
Then('a third step', (ctx) => ({ ...ctx, c: 3 }))

A context (ctx) is expected to be returned from every step and is passed to the next as the first parameter, followed by the regular step parameters inferred from the Cucumber expression or regular expression. The context is used to store and share state between steps. In other words, it replaces the usual World instance used in regular Cucumber steps.

Promises are supported:

Given('a step', async (ctx) => {
  await someAsyncFunction(ctx.someState)
  return { ...ctx, a: 1 }
})

And old-school callbacks are also supported:

const { withCallbacks: { Given, When, Then } } = withContext({ a: 0 })

Given('some step', (ctx, cb) => cb(null, { ...ctx, d: 9 }))

Steps that don't change context

Often, step definitions do not make any changes to the context. That's especially true for Then steps that usually only contain assertions. In such cases, you can use the tap function to avoid returning the original context:

import { withContext } from 'cucumber-fp'

const { Then, tap } = withContext({ a: 0 })

Then('c should exist', tap((ctx) => assert(ctx.c)))
Then('d should equal {int}', tap((ctx, expected) => assert.equal(ctx.d, expected)))

Note: tap() does not work with callbacks.

FAQs

Package last updated on 21 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc