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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@service-broker/test-utils

v1.1.2

Published

Unit testing utility

Downloads

24

Readme

test-utils

Unit testing utilities

Defining Tests

import { describe, expect } from '@service-broker/test-utils'

describe('test-suite', ({ beforeAll, afterAll, beforeEach, afterEach, test }) => {
  beforeEach(() => {
    //setup
  })
  afterEach(() => {
    //cleanup
  })
  test('test-1', () => {
    expect(actual, expectedValue)
    expect(await promise, expectedValue)
  })
})

When expectedValue is a:

  • Primitive: actual must be primitive and strictly equal
  • Set: actual must be a Set containing strictly the same elements
  • Map: actual must be a Map with the same keys mapped to expect-ed values
  • Array: actual must be an array with pair-wise expect-ed elements
  • Buffer: actual must be a Buffer with the exact same bytes
  • Object: actual must be an object with expect-ed property values

expectedValue can also be an Expectation object that defines custom assertions. For example:

import { Expectation } from '@service-broker/test-utils'

expect(actual, new Expectation('lessThan', 10, actual => {
  if (typeof actual != 'number') throw 'isNotNumber'
  if (actual >= 10) throw 'isNotLessThan10'
})

Since objects, arrays, and Maps are compared recursively using expect, Expectation is handy for nested expectations.

expect(request, {
  id: new Expectation('ofType', 'number', actual => {
    if (typeof actual != 'number') throw 'isNotNumber'
  }),
  ip: new Expectation('oneOf', ['::1', '127.0.0.1'], actual => {
    if (!['::1', '127.0.0.1'].includes(actual)) throw 'notLocalIp'
  })
})

It's useful to define common expectation 'helpers'. The following helpers are included with the library:

import { objectHaving, valueOfType, oneOf } from '@service-broker/test-utils'

expect(request, {
  id: valueOfType('number'),
  ip: oneOf(['::1', '127.0.0.1']),
  headers: objectHaving({
    'content-type': 'application/json',
    'content-length': new Expectation('lessThan', '1MB', actual => {
      assert(actual < 1024*1024, 'requestTooLarge')
    })
  })
})

Using expect inside describe-test results in nicely color-coded console outputs.

EXPECT {
  header: {
    from: Expectation { ofType: 'string' },
    ip: Expectation { oneOf: [ '::1', '127.0.0.1' ] },
    id: Expectation { ofType: 'string' },
    service: { name: 'sf1' },
    contentType: 'image/png',
    a: 2
  },
  payload: <Buffer 69 6d 61 67 65>
}
ACTUAL {
  header: {
    a: 2,
    service: { name: 's1' },
    ip: '127.0.0.1',
    contentType: 'image/png',
    from: 'xsjmxxcsjtq',
    id: 'xsjmxxcsjtq'
  },
  payload: <Buffer 69 6d 61 67 65>
}
Error: .header.service.name !equalExpected
    at Object.run (/Projects/service-broker/src/index.test.ts:94:5)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async Timeout.run (/Projects/service-broker/src/test-utils.ts:66:11)

Running Tests

Once you define your tests inside a file, run it like a regular Node script.

node --enable-source-maps ./dist/index.test.js [suite name] [test name]