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

moch-steps

v0.2.0

Published

Given, When, Then interface for mocha

Downloads

3

Readme

Given, When, Then

Write composable and expressive tests with a promise-based Given-When-Then DSL for [mocha][].

To get started, install the package and run your tests with the mocha-steps ui

A test with the steps ui looks like this.

describe 'given when then', ->

Given setup
When  doSomething
Then  check

To run it, install the the package with npm install --save-dev mocha-steps and run

mocha --ui mocha-steps

The above example is roughly equivalent to

describe 'bdd', ->

beforeEach setup

it '', ->
  doSomething()
  if check() == false
    throw new Error()

In addition to executing a step, Then also serves as a very simple assertion replacement. It will throw an error if the return value of the given function is false. To not prevent you from using you on expectations it will not throw on other falsey values like null or undefined.

By passing a string to the DSL methods you can assign variables, or for Then, pass variables to step.

Given 'a number', -> return 0 When 'more', 1 When 'evenMore', 2 Then 'more', (more)-> more > @number && @evenMore > more

As you can see, the labels will be stripped of any leading 'a'. The same holds for 'an' and 'the'.

You can also pass constant values to the steps instead of functions.

Structuring

We now explain in more detail how the step DSL maps to the default BDD DSL.

The Given function behaves like beforeEach. The step is executed before all tests in the current suite and all sub-suites.

Then 'number', (it)-> it > 0 Given 'number', 5

describe 'actual value', -> Then 'number', (it) it == 5

Each Then creates a test case. The runner for this test case includes all When steps tha precede it, up to a previous Then.

describe 'when then', ->

When computeName
When computeAge
Then checkName
Then checkAge

When computeStars
Then checkStars

describe 'bdd', ->

it '', ->
  computeName()
  computeAge()
  checkName()

it '', ->
  computeName()
  computeAge()
  checkAge()

it '', ->
  computeStars()
  checkStars()

Promises

All functions passed to the DSL may return promises. The next step is only executed when the promise is fullfilled.

Given 'zero', -> makePromise(0) When 'more', makePromise(1) Then -> @more > @zero

Test labels

Since each Then creates a test case it has to provide a label for the test.

If you pass a single function to Then, the interface will inspect the code of that function and extract the expression that is returned last. This works well for simple tests like

'then this.counter == 5'

Then -> @counter == 5

You can set the label explicitly by passing a string to Then.

'then the counter is 5'

Then 'the counter is 5', -> @counter == 5

Finally, if a given function has a specLabel property, it is used to contruct the label.

shouldEqual5 = (it)-> it == 5 shouldEqual5.specLabel = 'should equal 5'

'then the counter should equal 5'

Then 'the counter', shouldEqual5

Multiple data

Not yet implemented.

GivenAny 'name' ['alice', 'adam', 'avery'] Then 'name', (n)-> n[0] == 'a'

GivenAny 'number', -> (i) -> i < 5 && i Then 'name', (n)-> n < 5 && n >= 0