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

coworkers-test

v0.3.2

Published

Coworkers queue testing made easy

Downloads

21

Readme

coworkers-test

Helper to easily test coworkers message-handling middlewares as a unit

Installation

npm install --save coworkers-test

Usage

test.js:

const coworkersTest = require('coworkers-test')
const sinon = require('sinon')

const app = require('app.js')

// BDD style is shown but not required.
//   Replace the BDD style in this example w/ you test runner's api
describe('int-queue', function () {
  it('should ack if the message is an int', function (done) {
    coworkersTest(app)
      .send('foo-queue', '20')
      .expectAck() // check out the api section below for all available methods
      .expect(done)
  })
  it('should nack if the message is not an int, promise api', function (done) {
    coworkersTest(app)
      .send('foo-queue', 'abc')
      .expectNack() // check out the api section below for all available methods
      .expect()
      .then(function () {
        done()
      })
      .catch(done)
  })
  // failed test
  it('purposeful failed test to show coworker-test usage', function (done) {
    coworkersTest(app)
      .send('foo-queue', 'abc')
      .expectAck() // note: this is an ACK
      .expect(function (err, context) {
        console.error(err) // [ Error: expected "ack" but got "nack" ]
        // you can make assertions on context if you want
        // it is always passed even in the error case.
        // methods on the context are sinon stubs and can be asserted using sinon.
        sinon.assert.calledOnce(context.publish)
        sinon.assert.calledOnce(context.publisherChannel.publish)
        done(err)
      })
  })
})

app.js:


const app = module.exports = require('coworkers')()

app.queue('int-queue', function () {
  const int = parseInt(this.message)
  if (isNaN(int)) {
    throw new Error(`${this.message} is not a number`)
  }
  this.ack = true
})

app.on('error', function (err, context) {
  console.error(err.stack)
  const requeue = false
  context.consumerChannel.nack(context.message, requeue)
})

API

send(queueName, content, [props], [fields])

Use this to mock-send a message to your coworkers app content is the message content (it will cast numbers, strings, objects, and arrays to buffers for you) props are optional, and set as message properties to add to your message. fields are optional, message fields (like headers) to add to your message. Defaults:

// default message.properties
{
  headers: {}
}
// default message.fields
{
  'consumerTag': 'amq.ctag-izmxkEleoW2XjvKP3mNNUA',
  'deliveryTag': deliveryTag++, // auto-incremented id starting at 1
  'redelivered': false,
  'exchange': '',
  'routingKey': queueName
}

expect([cb])

Allows you to handle any assertion errors (expectAck, etc) and make custom assertions of your own on context Callback recieves err (assertion err) and context. context will always be passed even in the case of an error. If expect is not passed a callback it will return a Promise. Context methods are sinon stubs and can be asserted using sinon's assertion api.

expectAck([expectedOpts])

Expect that queue's consumer acks the message, will pass it's error to expect callback. expectedOpts allows you to verify that ack was invoked w/ the appropriate expected opts

expectNack([expectedOpts])

Expect that queue's consumer nacks the message, will pass it's error to expect callback. expectedOpts allows you to verify that nack was invoked w/ the appropriate expected opts

expectAckAll([expectedOpts])

Expect that queue's consuemr ackAlls the message, will pass it's error to expect callback. expectedOpts allows you to verify that ackAll was invoked w/ the appropriate expected opts

expectNackAll([expectedOpts])

Expect that queue's consumer nackAlls the message, will pass it's error to expect callback. expectedOpts allows you to verify that nackAll was invoked w/ the appropriate expected opts

expectReject([expectedOpts])

Expect that queue's consuemr rejects the message, will pass it's error to expect callback. expectedOpts allows you to verify that reject was invoked w/ the appropriate expected opts

License

MIT