Coder Social home page Coder Social logo

jest-bdd's Introduction

Jest BDD

Behavior-driven development wrapper for Jest

Installation

npm install jest-bdd

Usage

In-line

const { unit, given, when, then } = require('jest-bdd')

unit(
  given('two numbers', () => {
    scope.num1 = 3
    scope.num2 = 8
  },
    then('num1 is 3', () => { // check the setup
      expect(num1).toBe(3)
    }),
    when('the numbers are added', () => { // perform the action to test
      scope.sum = num1 + num2
    },
      then('the sum is 11', () => { // check the result of the action
        expect(sum).toBe(11)
      })
    )
  )
)

Output:
  Given: two numbers
    ✓ Then: num1 is 3
    When: the numbers are added
      ✓ Then: the sum is 11

Rules

Rules allow reusable clauses to be created.

const { Rules, Given, when, Then, unit } = require('jest-bdd')

const rules = RuleGroup()

Given(rules.two_numbers, () => {
  scope.num1 = 3
  scope.num2 = 8
})

When(rules.the_numbers_are_added, () => {
  scope.sum = num1 + num2
})

Then(rules.num1_is_3, () => {
  expect(num1).toBe(3)
})

Then(rules.the_sum_is_11, () => {
  expect(sum).toBe(11)
})

useRules(rules, () => unit(
  given.two_numbers(
    then.num1_is_3,
    when.the_numbers_are_added(
      then.the_sum_is_11
    )
  )
))

Output:
  Given: two numbers
    ✓ Then: num1 is 3
    When: the numbers are added
      ✓ Then: the sum is 11

Tests

npm test

jest-bdd's People

Contributors

konnorandrews avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

ssp6 lcborn4

jest-bdd's Issues

Fail then when when fails

If a when fails, I feel like the then following it should simply not run, i.e. when is the precondition, if that isn't satisfied, then the code simply should not execute. Any tips on how to achieve this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.