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

expect-to

v1.0.0

Published

Assertion library based on pure functions

Downloads

26

Readme

expect-to

expect-to is an assertion library based on pure functions. It is very easy to extend with new assertions, it can be used in most JavaScript testing frameworks and it works in both Node.js and browsers.

Why another assertion library?

Installation

$ npm install --save-dev expect-to

Basic usage

expect(foo).to(be('test'))
expect(foo).to(not(be('testing')))
expect(bar).to(be(false))
expect(bar).to(not(be(undefined)))
expect(obj).to(deepEqual({ foo: 'bar' }))

Or, e.g. within the Mocha BDD interface:

import expect, { not, be } from 'expect-to'

describe('my test', function() {

  it('handles equality checks', function() {
    expect('foo').to(be('foo'))
  })

  it('handles `not` for all assertions', function() {
    expect('foo').to(not(be('bar')))
  })

})

Assertions available

  • expect-to-core contains core assertions, such as be, deepEqual, match and throwError. Included by default.
  • expect-to-promises contains the powerful eventually for promises.

Created other assertions on top of expect-to? Let me know!

Creating your own assertions

As an example of how to create an assertion, this is how be is implemented:

function be(expected) {
  return function({ actual, assert }) {
    return assert(actual === expected,
      ['Expected %j to be %j', actual, expected],
      ['Expected %j not to be %j', actual, expected])
  }
}

And this is how you use it:

expect('foo').to(be('foo'))
expect('foo').to(not(be('bar')))

// And just to use the same variable names
// as in the implementation of be:
expect(actual).to(be(expected))

You can also write the assertion using arrow functions:

const be = expected => ({ actual, assert }) =>
  assert(actual === expected,
    ['Expected %j to be %j', actual, expected],
    ['Expected %j not to be %j', actual, expected])

And if you want to create an assertion like beUndefined, it would only be:

const beUndefined = ({ actual, assert }) =>
  assert(actual === undefined,
    ['Expected %j to be undefined', actual],
    ['Expected %j to not be undefined', actual])

And you can use it like this:

expect(undefined).to(beUndefined)
expect('testing').to(not(beUndefined))

Also, what's up with those error messages?

We rely on util.format() under the hood, so check out those docs for an explanation of %j and the other flags.

Note: You can also return a string if you want. This string will not be formatted in any way.

Why a new assertion library?

There are many assertion libraries out there, but I haven't found one that matches what I want in an assertion library. This is what I look for:

First, I want my assertions to feel like JavaScript, so I've never been a huge fan of this style of chaining assertions:

someVar.should.be.a('string')
should(null).not.be.ok()
should.not.exist(err)
expect(foo).to.be.a('string')
expect(obj).to.have.property('key')
  .with.length(3)
expect(window).not.to.be.an(Image)

An assertion library must also have great error messages, which excludes basic asserts:

assert(user.name == 'kim')
assert(typeof user.age == 'number')

(Yep, I know Power Assert solves that problem.)

It must also be extremely simple to extend. I want assertions for React, Sinon.js and others, but they shouldn't be built into the core itself — the core should be framework agnostic. To me it's important that extensions are first class members, not something that is tacked onto a prototype or added to an instance. This exludes things like:

beforeEach(function () {
  jasmine.addMatchers({
    toBeDivisibleByTwo: function () {
      return {
        compare: function (actual, expected) {
          return {
            pass: (actual % 2) === 0
          }
        }
      }
    }
  })
})

and:

chai.use(somePlugin)

On that note, I also don't want to mix the test library with the assertion library. This also excludes things like:

test('equal test', function (t) {
    t.plan(1)
    t.equal('test', 'test')
})

It must also end up calling a function, not assert on property access. What happens here?

foo.should.be.false

// or maybe a test with a small typo
bar.should.be.undefied

The latter doesn't fail no matter what bar is because it always returns undefined. And on that note, what about:

var bar = undefined
bar.should.not.be.ok

This doesn't work as you cannot add properties to undefined and null.

expect-to is my attempt at building an assertion library that solves all of these problems.