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

it-with-examples

v0.0.5

Published

Jasmine/Jest "it" function, with the addition of examples

Downloads

2

Readme

It with examples

######Specification-by-example style specs for Jasmine and Jest

It-with-examples is a small extension to Jest and Jasmine in order to allow examples to be passed into the specs. The overall goal is to improve the BDD experience with JavaScript unit testing frameworks, allowing multiple examples to be specified for a single spec. Each example will be ran as a separate spec, passing in the example as variables in the spec.

Installation

#For Yarn
yarn add it-with-examples --dev
#For NPM
npm install it-with-examples --save-dev

Use

To use It-with-examples within your specs, you'll need to specify it as a helper function:

jasmine.json

{
  ...
  "helpers": [
    "../node_modules/it-with-examples/index.js"
  ]
}

If you're using the Karma test runner karma.config.js

    files: [
        {pattern: "node_modules/it-with-examples/*.js", served: true, included: true}
    ]

It With Examples

The main function within it-with-examples is the eit function. eit stands for example-it, and provides an additional parameter to the standard Jasmine/Jest it function.

eit(specName, specFunction, examples, timeout = Jasmine default timeout)

The spec name and spec function parameters feature some slight modifications to the standard Jasmine/Jest it parameters.

  • The spec name allows the use of placeholder in order to differentiate between the examples
  • The spec function requires a data parameter, which will relate to the example that is to be run by the test
  • The examples parameter is an array of values. Each value in the array will be run as a single Jasmine/Jest test.
  • The timeout parameter is optional, and defaults to the Jasmine timeout default. If you are not using Jasmine then it will default to 5 seconds.

Example:

eit("should show a warning message when the user submits the form without a {field} value", data => {
    renderFormWidget();
    
    setFieldAsEmpty(data.field);
    submitForm();
    
    expect(formMessages()).toContain(data.expectedMessage);
}, [
  {field: "email", expectedMessage: "Missing email"},
  {field: "username", expectedMessage: "Missing username"},
  {field: "password", expectedMessage: "Missing password"} 
]);

This will produce each example as a separate test, substituting any placeholders in the spec name. The placeholders relate to the keys in the example objects. Running the above function will produce the following specs:

>should show a warning message when the user submits the form without a email field
>should show a warning message when the user submits the form without a username field
>should show a warning message when the user submits the form without a password field

Alternatively, the list of examples can simply be an array of primitive values, e.g.

eit("should show a warning message when the user submits the form without a {} value", field => {
    renderFormWidget();
    
    setFieldAsEmpty(field);
    submitForm();
    
    expect(form).toHaveWarning();
}, [
  "email",
  "username",
  "password"
]);

For primitives, the placeholder is simply {}.

Asynchronous Tests

With Jasmine and Jest your spec function can take an optional parameter, done, which allows the spec to wait until any asynchronous events have finished. It-with-examples retains this functionality by using a second, optional parameter on the spec function.

eit("should show a warning message when the user submits the form without a {} value", (field, done) => {
    submitForm();
    onFormSubmit(() => {
        expect(successNotice()).toBePresent();
        done();
    });
}, [
  "email",
  "username",
  "password"
]);

Flags

Jasmine and Jest feature the ability to exclude (xit) and focus (fit) specs. A "flag" parameter can be passed into each of the examples in order to specify whether they will be excluded or focused.

//Flags
'f' - focused
'x' - excuded

Any examples without a flag, or with an invalid flag, will be defaulted to using the standard it-style specs*.

Example:

eit("should show a warning message when the user submits the form without a {field} value", data => {
    ...
}, [
  {field: "email", expectedMessage: "Missing email", flag: 'f'},
  {field: "username", expectedMessage: "Missing username", flag: 'f'},
  {field: "password", expectedMessage: "Missing password", flag: 'x'} 
]);

Release Versions

0.0.1 Initial release 0.0.4 Resolving issues with NPM repository link