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

bddly

v1.1.5

Published

A nice Behaviour Driven Development testing framework for Jest

Downloads

451

Readme

Bddly is a lightweight Behaviour Driven Development testing wrapper for Jest. Unlike other BDD frameworks, such as Cucumber, it doesn't require the use of non-code textual spec files. All your tests and specs are written in code, which makes for a more fluid workflow, and lets the compiler help you just like when writing the rest of your code.

Bddly generates nice html files with the output of your tests, which can be the living documentation of the behaviour of your system.

Example output

The code that generated this test output is pretty readable, and all in TypeScript:

spec('Order retrieval', async () => {
    given.anOrderExistsInTheDatabaseWithName('My order');
    await when.aRequestIsMadeForOrderByIndex(state.orderIndex);
    then.theResponseTextIs('My order');
    and.theResponseCodeIs(200);
});

Bddly was inspired by YatSpec, which is a BDD testing framework for Java.

Bddly is written in TypeScript, and is designed to work with TypeScript projects. Vanilla JavaScript projects may need some fiddling to get this to work!

Quick Start

Setup
npm i bddly --save-dev

In your tsconfig.json file, add "resolveJsonModule": true so that Bddly can read your project name from package.json.

There is an example Nest.js project using Bddly that you might find instructive here. It has a slightly cleaner structure than the following example.

Test file

Bddly will look for test files under the bddly-tests folder. Your file name should end with -spec.ts in order for it to be recognised.

Start by creating a standard Jest test file, then add the following import:

import { Step, spec, specFinished, suiteStart, suiteFinished } from '../bddly';

Your import path might be different, depending on where your test file is.

Bddly uses decorators to wrap your BDD test steps, so that it can capture information to generate reports. For this to work, we need a bit of boilerplate. Firstly, we need to create classes to hold the implementations of your BDD steps, so go ahead and create the following:

class Given {}

class When {}

class Then {}

class And {}

We're going to initialise these classes within the Jest beforeAll function, so at the top level let's declare them first:

let given: Given;
let when: When;
let then: Then;
let and: And;

Then in the beforeAll function:

given = new Given();
when = new When();
then = new Then();
and = new And();

suiteStart();

And for the final piece of boilerplate, add calls to the Jest afterEach and afterAll methods:

    afterEach(() => {
        specFinished(expect.getState().currentTestName);
    });

    afterAll(() => {
        suiteFinished(expect.getState().currentTestName, __filename);
    });
A first test

Now that's all set up, let's write our first test. We use the Bddly spec function to define our test, which takes a test name, and an anonymous function with the test steps.

Here's the whole call to Jest describe:

describe('Bddly test:', () => {
    let given: Given;
    let when: When;
    let then: Then;
    let and: And;

    beforeAll(async () => {
        given = new Given();
        when = new When();
        then = new Then();
        and = new And();

        suiteStart();
    });

    afterEach(() => {
        specFinished(expect.getState().currentTestName);
    });

    afterAll(async () => {
        suiteFinished(expect.getState().currentTestName, __filename);
    });

    describe('Example tests:', () => {
        spec('Doubles a number', () => {
            given.aNumber(2);
            when.theNumberDoublerIsCalled();
            then.theStoreShouldBe(4);
        });
    });
});

Of note, each describe description ends in a colon. Bddly uses this to know which of the describes are part of the path to the test, and which is the test name.

The last thing to do is to write the implementations of the BDD steps, within the Given, When and Then classes. We'll also create a variable to hold the number:

let store: number;

class Given {
    @Step
    aNumber(x: number) {
        store = x;
    }
}

class When {
    @Step
    theNumberDoublerIsCalled() {
        store = store * 2;
    }
}

class Then {
    @Step
    theStoreShouldBe(x: number) {
        expect(store).toBe(x);
    }
}

Just remember to annotate each of these methods with the @Step decorator, so that Bddly knows to pay attention to them!

And that's it. Now just...

Run the test

Add a script to your package.json:

"test:e2e": "jest -i --config ./test/jest-e2e.json"

In this example, test-e2e.json is the jest config file. Your's might be different.

Then in the terminal run npm run test:e2e.

You should see the result of your test in the terminal (hopefully it passes!), and there should be a new top level folder in your project called bdd-reports. Open index.html in your browser to see a contents list of your Bddly tests.

Next steps

Real world apps have dependencies you need to include in your test. For instance, a database. You can add constructors to the Given, When, Then and And classes to inject dependencies you might want to use.

Here's an example of a Given class that needs to use a PrismaService to set up data in a database:

class Given {
    constructor(private prisma: PrismaService) {}

    @Step
    aProductIsInTheDatabaseWithName(name: string) {
        return this.prisma.product.create({
            data: { name: name },
        });
    }
}

and here's a When class that triggers a call to a test version of a Nest.js app:

class When {
    constructor(private app: INestApplication) {}

    @Step
    aRequestIsMadeTo(endpoint: string) {
        return request(this.app.getHttpServer())
            .get(endpoint)
            .then((resp) => {
                responseStatus = resp.status;
                responseBody = resp.body;
            });
    }
}

Note that we're assigning the response status and body to global variables. This is to capture the values so that we can assert on them in the Then class, like:

class Then {
    @Step
    theResponseStatusShouldBe(status: number) {
        expect(responseStatus).toBe(status);
    }
}

This is what the setup might look like for this:

let given: Given;
let when: When;
let then: Then;
let and: And;

let responseStatus: number;
let responseBody: any;

describe('Nest app:', () => {
    let app: INestApplication;
    let prisma: PrismaService;

    beforeAll(async () => {
        const moduleFixture: TestingModule = await Test.createTestingModule({
            imports: [AppModule],
        }).compile();

        app = moduleFixture.createNestApplication();
        prisma = app.get<PrismaService>(PrismaService);

        const { httpAdapter } = app.get(HttpAdapterHost);
        app.useGlobalFilters(new PrismaClientExceptionFilter(httpAdapter));

        given = new Given(prisma);
        when = new When(app);
        then = new Then();
        and = new And();

        suiteStart();

        await app.init();
    });

    beforeEach(async () => {
        await prisma.product.deleteMany();
    });

    afterEach(() => {
        specFinished(expect.getState().currentTestName);
    });

    afterAll(async () => {
        suiteFinished(expect.getState().currentTestName, __filename);
        await prisma.$disconnect();
        await app.close();
    });

Interesting Givens and Reports

Often it's useful to display additional data in the test reports. For instance, if you're priming your database with some data as part of a Given step, you might want to call out something about the data, such as an entity name, or number of rows inserted. In this case, use Bddly's interestingGiven function, which takes a name for the interesting fact, and the fact itself.

You can do the same for responses, using the report function.

Interesting givens and reports will be displayed in the Bddly test output, and if the value of any interesting givens are in any of the reports, then they will be highlighted.

Here's an example using our number doubler test from before:

let store: number;

class Given {
    @Step
    aNumber(x: number) {
        interestingGiven('The stored number', x);
        store = x;
    }
}

class When {
    @Step
    theNumberDoublerIsCalled() {
        store = store * 2;
    }
}

class Then {
    @Step
    theStoreShouldBe(x: number) {
        report('The number after doubling', store);
        expect(store).toBe(x);
    }
}