npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cucumber-fp

v0.0.6

Published

Cucumber.js with functional programming-style step definitions

Downloads

12

Readme

Cucumber.js FP step definitions

build

This little library brings functional programming style step definitions to Cucumber.js. We highly recommend using it with TypeScript as it enforces read-only constraints on the context and all its nested members in your functional step definitions.

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 }))

Mutations of context are forbidden

interface MyContext { a: string[] }
const initialContext: MyContext = { a: ['a', 'b'] }
const { When } = withContext(initialContext)

When('a step', (ctx) => {
  ctx.a.push('c')
  //    ^--- TypeScript compiler will fail here,
  //         `ctx` is deeply read-only. The following
  //          would work instead:
  ctx = { ...ctx, a: [...ctx.a, 'c']}
  return ctx
})

The type of ctx passed to your step definitions is always DeepReadonly<C> (where C is the type of your context, in the example above, MyContext). That means all mutation operations are forbidden.

Theses constraints have no effects if you're not writing your step definitions in TypeScript, which we highly recommend.

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:

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)))

When working with callbacks, tap() is not needed, you can simply omit the context when calling back:

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

Then('c should exist', (ctx, cb) => {
  assert(ctx.c)
  cb()
})