@joystick.js/test

Testing Queues

How to test jobs in queues using the test.queues.job() method from @joystick.js/test.

To test that specific jobs defined in a queue are working as expected, you can utilize the test.queues.job() method exported from @joystick.js/test:

/tests/queues/tasks.js

import test from '@joystick.js/test';

test.that('queue runs the tasks_report job', async (assert = {}) => {
  await test.queues.job('tasks_report', {
    queue: 'tasks',
    payload: {
      week: 'january_4',
      user_id: 'abc123',
    },
  });

  const report = await process.databases.mongodb.collection('task_reports').findOne({
    user_id: 'abc123',
    week: 'january_4',
  })

  assert.is(!!report, true);
});

Above, using the test.queues.job() method, we make a call directly to the tasks_report job in our tasks queue. To verify our call succeeded, we check that the expected side-effect of a report being added to the task_reports collection in our database exists for the user_id on the specified week.

The idea here is that when testing a job, we're not testing the queue functionality itself (i.e., that the job runs at a specific time). Instead, we're testing that the work being done at whatever time the job runs, runs as expected and creates the intended result.

API

test.queues.job()

test.queues.job(job_name: string, options: object) => Promise;

Parameters

job_name string required
The name of the job you're trying to test.
options object required
The options for the job to run.