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

@ltipton/parse-herkin

v0.0.1

Published

Parse gherkin features and map them to step definitions

Downloads

1

Readme

Parse-Herkin

  • Parse gherkin features text and step definition javascript text
  • Allows mapping feature steps to registered step definition and calls its method

Outline

Install

  • With NPM - npm install @ltipton/parse-herkin
  • With Yarn - yarn add @ltipton/parse-herkin

Use

// With esm imports
import { ParseHerkin } from 'parse-herkin'

// With cjs require
const { ParseHerkin } = require('parse-herkin')

// ParseHerkin is a class, so you should create a new instance of it before using
const PH = new ParseHerkin()

API

ParseHerkin

Description

  • <Class> - Manages features, steps, and definitions
  • <Arguments> - Accepts two arguments
    • (Optional) <Object> - World object passed to all step definition calls
    • (Optional) <Object> - Steps object to register steps on initialization

ParseHerkin.parse

Description

  • <Object> - Containing methods for parsing features and definitions

ParseHerkin.parse.feature

Description

  • <Function> - Parses the text content of a feature file (.feature)
  • <Arguments> - Accepts a single argument
    • (REQUIRED) <String> - Feature file text content
  • Returns an object adhering to the feature model spec

Example

// In Node.js
const fs = require('fs') 
const featureContent = fs.readFileSync('path/to/feature/file.feature')

// Returns an array of feature models parsed from the feature content
const featureModels = PH.parse.feature(featureContent)

ParseHerkin.parse.definition

Description

  • <Function> - Parses the text content of a step definition file (.js)
  • <Arguments> - Accepts a single argument
    • (REQUIRED) <String> - Definition text content ( Valid javascript code as text )
  • Returns an object adhering to the definition model spec

Example

// In Node.js
const fs = require('fs') 
const definitionContent = fs.readFileSync('path/to/step/definition.js')

// Returns an array of definition models parsed from the definition content
const definitionModel = PH.parse.definition(definitionContent)

ParseHerkin.Given

Description

  • <Function> - Register method for Given step definitions
  • <Arguments> - Accepts two arguments
    • (REQUIRED) <String> - Match expression string or regex
    • (REQUIRED) <Function> - Method called when a step definition matches a feature step

Example

import { ParseHerkin } from 'parse-herkin'
const PH = new ParseHerkin()

// Register a step definition with expression syntax
PH.Given(`Given match with {expression} syntax`, (expression) => { /* Assertion code */ })

// Register a step definition with regex syntax
PH.Given(/Given match with (\S+) syntax/, (expression) => { /* Assertion code */ })

ParseHerkin.When

Description

  • <Function> - Register method for When step definitions
  • <Arguments> - Accepts two arguments
    • (REQUIRED) <String> - Match expression string or regex
    • (REQUIRED) <Function> - Method called when a step definition matches a feature step

Example

import { ParseHerkin } from 'parse-herkin'
const PH = new ParseHerkin()

// Register a step definition with expression syntax
PH.When(`When match with {expression} syntax`, (expression) => { /* Assertion code */ })

// Register a step definition with regex syntax
PH.When(/When match with (\S+) syntax/, (expression) => { /* Assertion code */ })

ParseHerkin.Then

Description

  • <Function> - Register method for Then step definitions
  • <Arguments> - Accepts two arguments
    • (REQUIRED) <String> - Match expression string or regex
    • (REQUIRED) <Function> - Method called when a step definition matches a feature step

Example

import { ParseHerkin } from 'parse-herkin'
const PH = new ParseHerkin()
// Register a step definition with expression syntax
PH.Then(`Then match with {expression} syntax`, (expression) => { /* Assertion code */ })
// Register a step definition with regex syntax
PH.Then(/Then match with (\S+) syntax/, (expression) => { /* Assertion code */ })

ParseHerkin.registerSteps

Description

  • <Function> - Helper to register multiple step definitions at one time
  • (REQUIRED) <Arguments> - Accepts a single <Object>, matching the example below

Example

import { ParseHerkin } from 'parse-herkin'
const PH = new ParseHerkin()
// Accepts an object with properties of the definition types 
PH.registerSteps({
  given: {
    // Register a given step definition with expression syntax
    `I am on {page}`: (page) => { /* ... */ },
    // Register a given step definition with regex syntax
    `/I goto (\S+)$/`: (page) => { /* ... */ },
  },
  // All other properties follow the same format as defined above
  then: { /* ... */ },
  when: { /* ... */ },
})

ParseHerkin.run

Description

  • <Function> - Runs tests using the following steps
    • Parses the passed in feature text into a feature model
      • Bypassed if passed in argument a feature model
    • Matches the parsed feature text with registered step definitions
      • Uses the step definitions match property with the feature step text
    • Calls the methods of the matching step definitions
      • Passes in dynamic arguments of the feature step when method is called
  • <Arguments> - Accepts a single argument
    • (REQUIRED) <String|Object> - Feature text content or parsed feature matching feature model

Model Specs

Feature Model

{
  feature: <String>, /* Name of the feature */
  perspective: <String>, /* Meta information about the feature */
  desire: <String>, /* Meta information about the feature */
  reason: <String>, /* Meta information about the feature */
  tags: <Array> [ /* Array of defined tags for the feature */
    <String>
  ],
  scenarios: <Array> [ /* Array of defined Scenario Models */
    <Scenario>, /* Scenario Model */
  ]
}

Scenario Model

{
  scenario: <String>, /* Name of the scenario */
  uuid: <String>, /* Id of scenario created at the time it was parsed */
  tags: <Array> [ /* Array of defined tags for the scenario */
  given: <Array> [ /* Array of defined Given Step Models */
    <Step> /* Step Model */
  ],
  when: <Array> [ /* Array of defined When Step Models */
    <Step> /* Step Model */
  ],
  then: <Array> [ /* Array of defined Then Step Models */
    <Step> /* Step Model */
  ]
}

Step Model

{
  step: <String>, /* Text content of the step used to match with definitions */
  type: <String>, /* Gherkin definition type ( Given, When, Then, And, But ) */
  uuid: <String>, /* Id of step created at the time it was parsed */
}

Definition Model

{
  type: <String>, /* Gherkin definition type ( Given, When, Then, And, But ) */
  name: <String>, /* Cleaned and formatted string of the match property */
  match: <String>, /* Step matching string to match feature steps with definition methods */
  variant: <String>, /* Syntax used for defining the match property ( regex || expression ) */
  content: <String>, /* Text content of step definition ( Valid javascript code ) */
  method: <Function>, /* Method called when the step definition matches a feature step */
}