Convert Figma logo to code with AI

cucumber logocucumber-js

Cucumber for JavaScript

5,035
1,085
5,035
50

Top Related Projects

Run cucumber/gherkin-syntaxed specs with Cypress

Quick Overview

Cucumber-js is the JavaScript implementation of Cucumber, a popular Behavior-Driven Development (BDD) tool. It allows developers to write automated tests in plain language that can be understood by both technical and non-technical team members, bridging the gap between business requirements and software functionality.

Pros

  • Encourages collaboration between developers, testers, and business stakeholders
  • Supports writing tests in multiple languages using Gherkin syntax
  • Integrates well with various JavaScript testing frameworks and tools
  • Provides a clear and readable format for documenting software behavior

Cons

  • Learning curve for teams new to BDD and Gherkin syntax
  • Can lead to verbose test suites if not properly managed
  • May require additional setup and configuration compared to traditional unit testing
  • Performance can be slower compared to lower-level testing frameworks

Code Examples

  1. Defining a simple step definition:
const { Given, When, Then } = require('@cucumber/cucumber');

Given('I have {int} cucumbers', function (count) {
  this.cucumberCount = count;
});

When('I eat {int} cucumbers', function (eaten) {
  this.cucumberCount -= eaten;
});

Then('I should have {int} cucumbers', function (expected) {
  assert.equal(this.cucumberCount, expected);
});
  1. Using data tables in step definitions:
const { Given } = require('@cucumber/cucumber');

Given('the following users exist:', function (dataTable) {
  const users = dataTable.hashes();
  users.forEach(user => {
    // Add user to the system
    addUser(user.name, user.email);
  });
});
  1. Implementing hooks:
const { Before, After } = require('@cucumber/cucumber');

Before(function () {
  // This hook runs before each scenario
  console.log('Starting scenario');
});

After(function () {
  // This hook runs after each scenario
  console.log('Scenario completed');
});

Getting Started

  1. Install Cucumber-js:

    npm install @cucumber/cucumber
    
  2. Create a feature file (e.g., features/example.feature):

    Feature: Example feature
      Scenario: Adding numbers
        Given I have entered 5 into the calculator
        And I have entered 7 into the calculator
        When I press add
        Then the result should be 12 on the screen
    
  3. Implement step definitions (e.g., features/step_definitions/calculator_steps.js):

    const { Given, When, Then } = require('@cucumber/cucumber');
    const assert = require('assert');
    
    Given('I have entered {int} into the calculator', function (number) {
      this.numbers = this.numbers || [];
      this.numbers.push(number);
    });
    
    When('I press add', function () {
      this.result = this.numbers.reduce((a, b) => a + b, 0);
    });
    
    Then('the result should be {int} on the screen', function (expected) {
      assert.equal(this.result, expected);
    });
    
  4. Run Cucumber-js:

    npx cucumber-js
    

Competitor Comparisons

Run cucumber/gherkin-syntaxed specs with Cypress

Pros of cypress-cucumber-preprocessor

  • Seamless integration with Cypress, leveraging its powerful testing capabilities
  • Supports parallel execution of scenarios, improving test suite performance
  • Allows for easy debugging of Cucumber tests within the Cypress Test Runner

Cons of cypress-cucumber-preprocessor

  • Limited to Cypress ecosystem, less flexible for non-Cypress projects
  • May require additional setup and configuration compared to standalone Cucumber.js
  • Smaller community and ecosystem compared to Cucumber.js

Code Comparison

cypress-cucumber-preprocessor:

Given('I am on the homepage', () => {
  cy.visit('/');
});

When('I click the login button', () => {
  cy.get('#login-button').click();
});

Cucumber.js:

Given('I am on the homepage', async function () {
  await this.driver.get('/');
});

When('I click the login button', async function () {
  const loginButton = await this.driver.findElement(By.css('#login-button'));
  await loginButton.click();
});

Summary

cypress-cucumber-preprocessor is an excellent choice for teams already using Cypress, offering tight integration and performance benefits. However, Cucumber.js provides more flexibility and a larger ecosystem for projects not tied to Cypress. The choice between the two depends on the specific project requirements and existing testing infrastructure.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README


Cucumber

Automated tests in plain language, for Node.js

#StandWithUkraine npm build coverage backers sponsors

Cucumber is a tool for running automated tests written in plain language. Because they're written in plain language, they can be read by anyone on your team. Because they can be read by anyone, you can use them to help improve communication, collaboration and trust on your team.

This is the JavaScript implementation of Cucumber. It runs on maintained versions of Node.js. You can quickly try it via CodeSandbox, or read on to get started locally in a couple of minutes.

Looking to contribute? Read our code of conduct first, then check the contributing guide to get up and running.

Install

Cucumber is available on npm:

npm install @cucumber/cucumber

Get Started

Let's take this example of something to test:

First, write your main code in src/index.js:

class Greeter {
  sayHello() {
    return 'hello'
  }
}

module.exports = {
  Greeter
}

Then, write your feature in features/greeting.feature:

Feature: Greeting

  Scenario: Say hello
    When the greeter says hello
    Then I should have heard "hello"

Next, implement your steps in features/support/steps.js:

const assert = require('assert')
const { When, Then } = require('@cucumber/cucumber')
const { Greeter } = require('../../src')

When('the greeter says hello', function () {
  this.whatIHeard = new Greeter().sayHello()
});

Then('I should have heard {string}', function (expectedResponse) {
  assert.equal(this.whatIHeard, expectedResponse)
});

Finally, run Cucumber:

npx cucumber-js

And see the output:

Terminal output showing a successful test run with 1 scenario and 2 steps, all passing

If you learn best by example, we have a repo with several example projects, that might help you get going.

Documentation

The following documentation is for main, which might contain some unreleased features. See documentation for older versions if you need it.

Support

Support is available from the community if you need it.

NPM DownloadsLast 30 Days