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

muggle-assert

v1.1.3

Published

A simplification of the node assert library for use with muggle

Downloads

47

Readme

muggle-assert

Greenkeeper badge Travis badge standard badge npm

A simplification of node's assert for use with muggle

Goals

  • Predictable and simple behavior
  • Simple and readable source code
  • Intuitive and small API
  • Encourage writing robust and readable tests
  • Fully tested

Installation

$ npm install muggle-assert

Usage

Failing assertions throw an error. Passing assertions do nothing.

const assert = require('muggle-assert')

assert(2 < 5, '2 should be less than 5')

assert.equal(2 + 2, 4)

assert.throws(() => { throw new Error('penguin') })

await assert.rejects(Promise.reject(new Error('penguin')))

API

AssertionError

new assert.AssertionError(args)

When an assertion fails, it throws an AssertionError

function outerFunction () {
  const assertError = new assert.AssertionError({
    message: 'should be a penguin',
    expected: 'penguin',
    actual: 'polar bear',
    operator: 'animal',
    stackStartFn: outerFunction
  })

  assert.equal(assertError.name, 'AssertionError')
  assert.equal(assertError.message, 'should be a penguin')
  assert.equal(assertError.expected, 'penguin')
  assert.equal(assertError.actual, 'polar bear')
  assert.equal(assertError.operator, 'animal')

  // strips stackStartFn and above from the stack trace
  assert(!assertError.stack.includes('outerFunction'))
}

Assert

assert(expression, description)

Assert that an expression is truthy. Throws an AssertionError if expression is falsy.

The AssertionError has no useful information by default. I recommended always including a description using the word "should" to make the error output readable and useful.

const tux = new Penguin('Tux')
assert(tux instanceof Penguin, 'tux should be a Penguin instance')

try {
  assert(5 > 100, '5 should be greater than 100')
} catch (assertError) {
  assert.equal(assertError.message, '5 should be greater than 100')
  assert.equal(assertError.operator, 'true')
}

Equal

assert.equal(actual, expected, description = 'should be equal')

Assert that the two values are equivalent.

muggle-deep-equal is used to determine equality. It uses === to compare values, and recursively compares the values of objects and arrays. Its behavior is detailed in its readme.

The AssertionError provides actual and expected properties that usually give a good idea of why the assertion failed. It's still often a good idea to add a custom description using "should", especially to differentiate multiple assert.equal() within the same muggle test.

assert.equal('penguin', 'penguin')

assert.equal([1, 2, 3], [1, 2, 3])

assert.equal({key: 'value'}, {key: 'value'})

try {
  assert.equal(1, true, '1 should equal true')
} catch (assertError) {
  assert.equal(assertError.message, '1 should equal true')
  assert.equal(assertError.actual, 1)
  assert.equal(assertError.expected, true)
  assert.equal(assertError.operator, 'deepEqual')
}

Throws

assert.throws(fn, [expectedError], description = 'should throw error')

Assert that a function will throw an error. If fn() finishes execution without throwing an error, then the assertion fails.

expectedError, if present, is the error that should be thrown from the function. If an error is thrown but it doesn't equal expectedError, then the assertion fails.

muggle-deep-equal is used to compare the thrown error to expectedError. This means the error is compared as expected using toString().

assert.throws(() => { throw new Error() })

assert.throws(
  () => { throw new Error('penguin') },
  new Error('penguin'),
  'penguin error should be thrown'
)

// expectedError is optional
assert.throws(() => { throw new Error('penguin') }, 'penguin error should be thrown')

try {
  assert.throws(
    () => { throw new Error('penguin') },
    new TypeError('penguin'),
    'should throw penguin TypeError'
  )
} catch (assertError) {
  assert.equal(assertError.message, 'should throw penguin TypeError')
  assert.equal(assertError.actual, new Error('penguin'))
  assert.equal(assertError.expected, new TypeError('penguin'))
  assert.equal(assertError.operator, 'throws')
}

Rejects

async assert.rejects(promise, [expectedError], description = 'promise should reject')

Assert that a promise is rejected or will reject.

If promise resolves, then the assertion fails. assert.rejects() is an async function, so it returns a rejected promise with an AssertionError as the reason instead of throwing on failure.

expectedError, if present, is the error that the promise should reject with. If the promise rejects with an error that doesn't equal expectedError, then the assertion fails.

muggle-deep-equal is used to compare the rejection reason to expectedError. This means errors are compared as expected using toString().

await assert.rejects(Promise.reject(new Error()))

await assert.rejects(
  Promise.reject(new Error('penguin')),
  new Error('penguin'),
  'should reject with penguin error'
)

// expectedError is optional
await assert.rejects(Promise.reject(new Error('penguin')), 'should reject with penguin error')

async function delayReject() {
  await sleep(100)
  throw new Error()
}

// async functions return promises
await assert.rejects(delayReject())

try {
  await assert.rejects(
    Promise.reject(new Error()),
    new Error('penguin'),
    'should reject with penguin error'
  )
} catch (assertError) {
  assert.equal(assertError.message, 'should reject with penguin error')
  assert.equal(assertError.actual, new Error())
  assert.equal(assertError.expected, new Error('penguin'))
  assert.equal(assertError.operator, 'rejects')
}