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

pickle-jar

v1.4.3

Published

Framework for writing Gherkin features and running them using Jest

Downloads

20

Readme

pickle-jar

Framework for writing Gherkin features and running them using Jest

Overview

pickle-jar is an alternative to jest-cucumber and and Cucumber.js that runs on top of jest. pickle-jar allows defining test scnearios using Gherkin language and translates them into define/it blocks that run using jest. Compared to jest-cucumber, the output is more explicit and all steps are reperesented by jest describe blocks.

The framework allows defining steps using regular expression matchers. Match groups are automatically passed as parameters to the step function.

Why pickle-jar?

jest-cucumber is a good framework, but it has several limitations:

  • the test steps are not clearly represented (only the scenario)
  • scenario descriptions are not clear, making it hard to figure out the set of parameters that leads to a scenario failure

Getting started

Install pickle-jar:

npm install pickle-jar --dev

The project dependencies must be explicitly installed (they are defined as peer dependencies):

npm install antlr4 antlr4ts glob --dev

Create a first feature file

Create a folder named <rootDir>/test/features. This folder will be the base folder for all feature files. To keep things tidy, feature files can be also grouped in subfolders.

Define a first feature file, called MyFeature.feature inside the <rootDir>/test/features directory:

Feature: Logging in

Scenario: Entering a correct password
    Given I have previously created a password
    When I enter my password correctly
    Then I should be granted access

In order to run the features and their steps, a test runner entry point must be defined:

The test runner entry point

Create a file named runner.ts in the <rootDir>/test directory:

import {workerData} from "worker_threads";
import {StepDefinition, testRunner} from "../src";

interface World {
    password: string | undefined;
    grantedAccess: boolean;
}

const stepDefinitions: StepDefinition<World>[] = [{
    match: /^Given I have previously created a password$/, step: (world) => {
        world.password = "my password";
    }
}, {
    match: /^When I enter my password correctly$/, step: (world) => {
        world.grantedAccess = world.password === 'my password';
    }
}, {
    match: /^I should be granted access$/, step: (world) => {
        expect(world.grantedAccess).toBeTruthy();
    }
}];

const world: World = {
    password: undefined,
    grantedAccess: false
}
testRunner<World>(`${__dirname}/features/**/*.feature`, stepDefinitions, world);

The test runner file defines the World object structure. This object is passed to each step and allows sharing values from one step to another.

Step definitions

Steps from the feature file are matched using regular expressions. The test runner receives an array of steps. Each step has a regular expression and a callback:

{
    match: /^Given I have previously created a password$/, 
    step: (world) => {
        world.password = "my password";
    }
}

The test runner checks the step using the regular expressions. If zero or more than one regular expression match the feature step, an error will be thrown. Any regular expression capture groups or doc string in the step definition will be passed to the step callback as parameters.

For example, the feature and steps define above can be rewritten in a more general way:

MyFeature.feature:

Feature: Logging in
  Scenario: Test passwords with parameters
    Given I have previously created the 'my password' password
    When I enter correctly the password
    """
    my password
    """
    Then I should be granted access

runner.ts steps:

import {workerData} from "worker_threads";
import {StepDefinition, testRunner} from "../src";

interface World {
    password: string | undefined;
    grantedAccess: boolean;
}

const stepDefinitions: StepDefinition<World>[] = [{
    match: /^Given I have previously the '([^']+)' password$/, step: (world, password) => {
        world.password = password;
    }
}, {
    match: /^I enter correctly the password$/, step: (world, expectedPassword) => {
        world.grantedAccess = world.password === expectedPassword;
    }
}, {
    match: /^I should be granted access$/, step: (world) => {
        expect(world.grantedAccess).toBeTruthy();
    }
}];

const world: World = {
    password: undefined,
    grantedAccess: false
}
testRunner<World>(`${__dirname}/features/**/*.feature`, stepDefinitions, world);

Running scenario outlines

Scenario outline steps are defined in the same way as normal scenario steps. The scenario outline is expanded into a test run for each row of the examples.

Tag filtering

Tags can be used for filtering entire scenarios or scenario steps. Feature files can use tags using the @tag. The test runner can accept an optional predicate callback that can filter scenarios or steps based on the tags such as:

const tagFilter = (tags: string[])=> {
    return tags.indexOf("@ui") === -1;
}

There are several builtin tags which are handled differently than custom tags:

  • @skip - when used, the step and sub-steps are completely skipped (they are handled as Jest describe.only or it.only calls)
  • @only - when used, only the steps and sub-steps are executed (they are handled as Jest describe.only or it.only calls)
  • @todo - when used, the step is marked as a TODO and no step code is executed (they are handled as Jest it.todo calls)
  • @fail - when used, the step is expected to fail (thye are handled as Jest it.failed calls)

Jest configuration

Create a jest.config.js file in the project root (or update the existing one) to match the runner.ts file:

module.exports = {
    testMatch: [
        '<rootDir>/test/runner.ts'
    ],
    transform: {
        '^.+\\.[tj]s$': ['ts-jest', {
            tsconfig: '<rootDir>/tsconfig.spec.json',
        }]
    },
    moduleFileExtensions: ['ts', 'js', 'html'],
    coverageDirectory: '../coverage/',
};