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

assert-log

v0.2.2

Published

A simple way to unit-test callbacks

Downloads

15

Readme

assert-log

Any easy way to unit-test callbacks

If you need to unit-test some code that calls callbacks, you could use a mocking library like sinon.js, but there is a simpler technique. Just write your own callbacks, but have them send their output to a log. When the test is over, verify that the log matches your expectation. For example, suppose you want to unit-test this thing:

class ExampleCounter {
  on(event, callback) { /* ... */ }
  increment() { /* ... */ }
  decrement() { /* ... */ }
}

Here is what the test might look like:

import { makeAssertLog } from 'assert-log'

const log = makeAssertLog()

// Set up the callbacks:
const counter = new ExampleCounter()
counter.on('count', count => log('count is', count))
counter.on('error', error => log(error))

// Generate some events:
counter.increment()
log.assert('count is 1')

counter.decrement()
log.assert('count is 0')

counter.decrement()
log.assert('Error: Count cannot be negative')

The log function records a log entry. It accepts multiple arguments, which it concatenates together to make a single string.

The assert method verifies that that log contains the given strings in any order, and then clears the log.

Setup

Install:

yarn add --dev assert-log
# or:
npm install --save-dev assert-log

Import:

import { makeAssertLog } from 'assert-log'
// or:
const { makeAssertLog } = require('assert-log')

Advanced usage

If you need more control than assert provides, the log also has a read method. This method removes log entries and returns them. It accepts the maximum number of entries to return, but this is optional. It returns an array of strings. The returned array also has an assert method for convenience:

// Read and check the first entry:
log.read(1).assert('got 1')

// Read the remaining entries:
const entries = log.read()

// Use a separate assertion library to do fancy tests:
expect(entries[0]).to.not.match(/Error/)

The log also has waitFor method, which is like an asynchronous version of read. It waits for the requested number of entries to arrive, and then returns them in a promise. The returned promise also has an assert method for convenience:

setTimeout(() => log('timeout 1'), 10)
setTimeout(() => log('timeout 2'), 15)
await log.waitFor(2).assert('timeout 1', 'timeout 2')

The waitFor method will stop waiting after 1 second and just return whatever is available. If you want to tweak this timeout, just pass a timeout option to makeAssertLog (in milliseconds):

const log = makeAssertLog({ timeout: 100 })

The makeAssertLog function also takes a verbose boolean option, which copies the log entries to console.log. This can be helpful while debugging, but isn't something you would normally leave turned on:

const log = makeAssertLog({ verbose: true })

Pretty-printing

The log function converts its arguments to strings using a function called stringify. You can access this function yourself, which can be useful if you don't know the exact log contents ahead of time:

import { makeAssertLog, stringify } from 'assert-log'

const log = makeAssertLog()

// Logs something like 'Got event { type: "grid", location: [0.1, 0.2] }':
const event = {
  type: 'grid',
  location: [Math.random(), Math.random()]
}
log('Got event', event)

// Use `stringify` to help build the expected message:
log.assert(`Got event ${stringify(event)}`)