Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

chai-builder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chai-builder

First class expressive expectations

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

First Class Expectations

Build Status

A chai plugin to separate expectations from the the values they are tested on.

var expect = require('chai').expect;
var should = require('chai-buider/use');

var expectation = should.equal(4);
expectation.test(4);
expect(4).to.equal(4);

This example shows the correspondence beetween the expect syntax and should builder syntax. This extends to all of chai’s language properties—so in general the following are equivalent.

should.a.b.c.test(target);
expect(target).a.b.c

should.a.b.f(x, y).test(target);
expect(target).a.b.f(x,y)

Expecation also have a label property that describes the expectation

should.be.defined.and.equal(4).label
// -> 'should be defined and equal 4'

Use Cases

Building custom expecations.

var shouldHaveAddress =
  should.have.property('address')
        .as.an('object')
        .and.with.key('name')
var shouldHaveExtendedAddress = 
  shouldHaveAddress.with.key('street').and.key('city')

shouldHaveAddress.test({address: {name: 'Willy'}})
shouldHaveExtendedAddress.test({address: {
  name: 'Willy',
  street: 'Chocolate Lane',
  city: 'Tiny Town'
}})

Running labeled tests.

function test(subject, expecations) {
  expectations.forEach(function(expectation)) {
    consol.log('test: ' + subject + expectation.label);
    expectation.test(subject);
  }
}

The plugin integrates nicely with mocha-when-then.


shouldHaveAddress = should.have.property('address')
shouldHaveAddress.label = 'should have address'

Given 'person', {address: {name: 'Willy'}}
Then 'person', shouldHaveAddress.with.key('name')

When -> @person.address.street = 'Chocolate Lane'
And  -> @person.address.city = 'Tiny Town'
Then 'person', shouldHaveAdress.with.key('street')
And  'person', shouldHaveAdress.with.key('city')

This creates the following test lables

person should have address with key name
person should have address with key street
person should have address with key city

Keywords

FAQs

Package last updated on 23 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc