Coder Social home page Coder Social logo

bdd-expand's Introduction

Lets say we want to make unit tests for simple add function

export const add = (a, b) => a + b;

Full BDD style requires quite verbose syntax to get it done

describe('Add', () => {
    it('Adds two digits', () => {
        expect((add(1, 5))).to.eq(6);
    });

    it('Works with negative numbers', () => {
        expect((add(-1, 5))).to.eq(4);
    });

    it('Works for larger numbers', () => {
        expect((add(1000000, 5))).to.eq(1000005);
    });
})

It is handy when we have to do extra mocking and setup, but in this case it might be overkill. All the above can be reduced to which function is being tested and map of example inputs to expected outputs. This tool enables to write short, concise test definition

import { bddExpand } from 'bdd-expand';

// Style 1
bddExpand(add, [
    [[1, 5], 6],
    [[-1, 5], 4],
    [[1000000, 5], 1000005],
]);

// Style 2
bddExpand(add, [
    {
        title: 'Adds two digits',
        args: [1, 5],
        expected: 6,
    },
    {
        title: 'Works with negative numbers',
        args: [-1, 5],
        expected: 4,
    },
    {
        title: 'Works for larger numbers',
        args: [1000000, 5],
        expected: 1000005,
    }
]);

You can run the spec in either mocha or jest test runner to get identical output to the classical way ๐Ÿ˜‰

Minor features

  • types support โœจ
  • works for mocha/chai - uses deep.equal comparison
  • works for jest - uses toEqual
  • in cypress suite name will be the file basename

bdd-expand's People

Contributors

smuszel avatar

Watchers

James Cloos avatar

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.