Giter VIP home page Giter VIP logo

kue-mock's Introduction

Kue Mock

Build Status

A small stubbing/mocking library for testing code that deals with kue.

The library doesn't hack on any kue's internals (replacing/overriding methods etc.). Instead, it creates the original queue instance with a separate redis namespace, then, when stubbing, it creates job process handlers on the fly, putting its own implementation that gives you the ability to control the job processing behaviour.

Installation

$ npm install kue-mock --save-dev

Usage

const kue = require('kue');
const KueMock = require('kue-mock');
const $queue = new KueMock(kue);

describe('functionality that deals with kue', () => {
  let $stub;

  beforeEach(() => {
    return $queue.clean(); // for test case isolation
  });

  beforeEach(() => {
    $stub = $queue.stub('job type');
  });

  afterEach(() => {
    $stub.restore(); // for test case isolation
  });

  // your tests goes here (see examples below)
});

API

KueMock

new KueMock(kue [, options]) (constructor)

Creates a kue's JobQueue instance internally specifying a custom jobs namespace for environmental isolation.

Important Note: kue's .createQueue() is designed to return a singleton object, so you're required to create a KueMock instance before including any code that calls kue.createQueue.

You can also pass your custom redis options as a second argument if you need.

#clean()

Cleanup all the enqueued/completed/failed/stuck jobs. It's highly recommended to call this function inside the beforeEach block to make your test cases isolated from each other.

This method supports both Promise and callback styles, so you can do:

beforeEach(function (done) {
  $queue.clean(done); // providing `done` as a callback
});

// or in ES6 Promises style
beforeEach(() => $queue.clean()); // returning a `Promise`
#stub(type [, impl])

Stubs a job process returning a JobStub object for controlling it.

@return JobStub

JobStub

#process

This is actually a sinon.stub() that is set up as a default implementation of the job process. It allows you to get all benefits sinon.js' stubbing/mocking features in setting up the job behaviour. Or you can even replace it with your own implementation on the fly.

Here are a few usage examples:

let $stub = $queue.stub('job type');

$stub.yields(null, { foo: 'bar' }); // completes the job with results
$stub.yields(new Error('Oops!')); // fails the job providing a reason

// assertions and expectations
$stub.process.should.be.called(); // using `should-sinon`
expect($stub).to.have.been.called; // using `sinon-chai`

// you can also provide your custom process implementation
let $stub = $queue.stub('job type', (job, done) => {
  done(null, { foo: 'bar' });
});

// or simply replace the #process property
$stub.process = function (job, done) {
  // your own process handling...
  done(null, { foo: 'bar' });
};
@return sinon.stub()
#restore

Unregister the job process stub. Make sure that you've called this after your stubbing test cases to isolate them from each other.

TODO: $stub.process setup sugar

JobStub#completes()
JobStub#completesWith(result)
JobStub#fails()
JobStub#failsWith(reason)

Examples

const {expect} = require('chai');

const kue = require('kue');
const KueMock = require('kue-mock');
const $queue = new KueMock(kue);

const {yourJobRunnerFunction} = require('./your-lib-file');

describe('functionality that deals with kue', () => {
  before(() => $queue.clean());
  afterEach(() => $queue.clean());

  it('enqueues a job providing some correct data', () => {
    let jobData;

    const $stub = $queue.stub('your job type', (job, done) => {
      jobData = job.data;
      done();
    });

    return yourJobRunnerFunction()
      .then(() => {
        expect(jobData).to.be.an('object')
          .that.is.eql({ foo: 'bar' });
      })
      .finally(() => {
         $stub.restore(); // unregister the job stub to isolate test cases
      });
  });

  describe('when the job is completed', () => {
    let $stub;

    beforeEach('stub job process result', () => {
      $stub = $queue.stub('your job type')
        .yields(null, { baz: 'qux' });
    });

    afterEach(() => $stub.restore());

    it('correctly handles the result', () => {
      return yourJobRunnerFunction()
        .then((result) => {
          expect(result).to.eql({ baz: 'qux' });
        });
    });

    // ...
  });

  describe('when the job is failed', () => {
    let $stub;

    beforeEach('stub job process failure', () => {
      $stub = $queue.stub('your job type')
        .yields(new Error('Oops!'));
    });

    afterEach(() => $stub.restore());

    it('correctly handles the job result', () => {
      return yourJobRunnerFunction()
        .catch((err) => {
          expect(err).to.be.an('error')
            .with.property('message', 'Oops!');
        });
    });

    // ...
  });
});

Contributing

Your contributions are very welcome!

When contributing, follow the simple rules:

  • Don't violate DRY principles.
  • Boy Scout Rule needs to have been applied.
  • Your code should look like all the other code – this project should look like it was written by one man, always.
  • If you want to propose something – just create an issue and describe your question with as much description as you can.
  • If you think you have some general improvement, consider creating a pull request with it.
  • If you add new code, it should be covered by tests. No tests - no code.
  • If you find a bug (or at least you think it is a bug), create an issue with the library version and test case that we can run and see what are you talking about, or at least full steps by which we can reproduce it.

Running tests

$ make test

License

MIT © 2016 Dan Kerimdzhanov

kue-mock's People

Contributors

abd2561024 avatar kerimdzhanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

abd2561024

kue-mock's Issues

Test job enqueue

I have a post-hook middleware that enqueues some job for processing. I don't need any complete/failure handlers, the function calls back just after job is enqueued (on Job#save callback).

How can I test such cases?

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.