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

mocha-cakes-2

v3.3.0

Published

A Mocha BDD integration

Downloads

23,681

Readme

Mocha Cakes 2

Mocha Cakes is a Gherkin/Cucumber syntax integration for the Mocha testing framework.

Build Status Sponsored

Installation

NPM:

npm install --save-dev mocha-cakes-2

Usage

Enable the mocha-cakes-2 integration

To enable the Mocha integration you need to specify mocha-cakes-2 in the ui option.

CLI

Either use the command line argument:

mocha --ui mocha-cakes-2 path/to/my/tests

Or set it in your mocha.opts file:

--ui mocha-cakes-2

API

Either pass it in the options as you construct Mocha:

var mocha = new Mocha({
  ui: 'mocha-cakes-2'
});

Or set it after you've constructed Mocha:

var mocha = new Mocha();
mocha.ui('mocha-cakes-2')

Test structure

require('chai').should();

Feature('Some feature', () => {

  Scenario('Some Scenario', () => {

    let number = 2;

    Given('a number', () => {
      number.should.exist;
    });
    And('that number is 2', () => {
      number.should.equal(2);
    });

    When('adding 40', () => {
      number += 40;
    });

    Then('the number should be 42', () => {
      number.should.equal(42);
    });
  });
});

The result will look something like this:

The common Mocha functions (describe, it, before, after, etc) are also available and can be used together with Mocha Cakes.

Upgrading from version 1.x

Replace the require('mocha-cakes-2') statement(s) with the --ui mocha-cakes-2 option as described above.

TypeScript

The TypeScript definitions are bundled together with mocha-cakes-2. To use mocha directly with TypeScript you need types for mocha and ts-node.

npm install --save-dev typescript ts-node @types/mocha

You should have a tsconfig.json in the root of your project like so

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node"
  }
}

Now you can run it like so:

mocha -r ts-node/register --ui mocha-cakes-2  ...

Your tests should look like this:

import 'mocha-cakes-2';

Feature('Some feature', () => {

  Scenario('Some Scenario', () => {

    let number = 2;

    Given('a number', () => {

    });
    And('that number is 2', () => {

    });

    When('adding 40', () => {

    });

    Then('the number should be 42', () => {

    });
  });
});

API

The Mocha Cakes integration adds the following functions to the global scope:

  • Feature | feature
    • Scenario | scenario
      • Given | given
      • When | when
      • Then | then
      • And | and
      • But | but

.skip

Skips a test clause. Works on all test functions.

Feature('Some feature', () => {

  Scenario.skip('Skipped scenario', () => {
    // ...
  });

  Scenario('Ordinary', () => {
    // ...
  });
});

.only

Only run the specified test clause. Works on all test functions.

Feature('Some feature', () => {

  Scenario('First scenario', () => {
    // ...
  });

  Scenario('Second scenario', () => {
    // ...
  });

  Scenario.only('Only I will run!', () => {
    // ...
  });

  // ...
});

beforeEachScenario and afterEachScenario

Executes the provided function only once for each of the scenarios under the current scope.

Feature('Some feature', () => {

  beforeEachScenario( () => {
    someSetup();
  });

  afterEachScenario( () => {
    doCleanup();
  });

  Scenario('First scenario', () => {
    // ...
  });

  Scenario('Second scenario', () => {
    // ...
  });

  // ...
});

beforeEachFeature and afterEachFeature

Executes the provided function only once for each of the features under the current scope.

beforeEachFeature( () => {
  someSetup();
});

afterEachFeature( () => {
  doCleanup();
});

Feature('Some feature', () => {
  // ...
});

Feature('Another feature', () => {
  // ...
});

// ...

Development

Testing the CLI and API interfaces

If you use Mocha directly to run the tests you can set the MOCHA_INTERFACE environment variable to either cli or api to choose which Mocha interface to run the tests with: MOCHA_INTERFACE=api mocha test/feature/tests.js.

MOCHA_INTERFACE will default to cli if no value is set.

When you run npm run test:cli or npm run test:api (or npm test to run them both), MOCHA_INTERFACE is set automatically to the appropriate value.

Acknowledgements

Mocha Cakes 2 is heavily influenced by quangv's mocha-cakes.