Giter Club home page Giter Club logo

coworkers-test's Introduction

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 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.
        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.

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

coworkers-test's People

Contributors

melalj avatar tjmehta avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.