Search code examples
node.jsmocha.jskue

Testing Node.js application that uses Kue


I would like to test an application that uses Kue so that job queue is empty before each test and cleared after each test. Queue should be fully functional and I need to be able to check status of jobs that are already in the queue.

I tried mock-kue and it worked well until I had to get jobs from the queue and analyze them. I couldn't get it to return jobs by job ID.

Situations that I need to be able to test:

  1. Something happens and there should be a job of a given type in the queue,
  2. Something happens and produces a job. Something else happens and that job gets removed and replaced with another job (rescheduling or existing job).

Seams straightforward, but I have hard time wrapping my head around the problem. All pointers are welcome.


Solution

  • In my experience it's more straightforward to simply have redis running on localhost wherever you want to run your tests rather than dealing with a mocked version of kue.

    First, to make sure kue is empty before each test it could be as simple as flushing redis, eg:

    var kue = require('kue');
    var queue = kue.createQueue();
    
    queue.client.flushdb(function(err) {});
    

    For #1, kue has a rangeByType() method that should solve your problem:

    var getJobs = function(type, state, cb) {
       kue.Job.rangeByType(type, state, 0, -1, 'asc', cb);    
    }
    // After something happens
    getJobs('myJobType', 'active', function(err, jobs) {});
    

    For #2, you can use the same method and simply keep track of the job id to know that it has been replaced:

    var jobId;
    getJobs('myJobType', 'active', function(err, jobs) {
        assert.lengthOf(jobs, 1);
        jobId = jobs[0].id;
    });
    
    // After the thing happens
    getJobs('myJobType', 'active' function(err, jobs) {
        assert.lengthOf(jobs, 1);
        assert.notEqual(jobId, jobs[0].id);
    });
    

    And if you ever need to query a job by ID you can do it like so:

    kue.Job.get(jobId, function(err, job) {});