@joystick.js/test

Testing Routes

How to test API routes in your Joystick app using test.routes HTTP methods.

Depending on the nature of your app, it can be helpful to author tests for individual routes (e.g., if you're offering a public API and want to test responses). To facilitate this, @joystick.js/test includes the test.routes object, containing methods for each of Joystick's supported HTTP methods.

  • test.routes.get() performs an HTTP GET request.
  • test.routes.delete() performs an HTTP DELETE request.
  • test.routes.patch() performs an HTTP PATCH request.
  • test.routes.post() performs an HTTP POST request.
  • test.routes.put() performs an HTTP PUT request.

/tests/index.server.js

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

test.that('the /api/books route returns books', async (assert = {}) => {
  const response = await test.routes.get('/api/books');
  assert.is(response?.body?.books?.length === 25, true);
});

Above, we create a mock test leveraging the test.routes.get() method, performing an HTTP GET request to the /api/books endpoint in our route. We assume that this endpoint will return us an array of 25 books and write our assertion to verify this.

Passing a Body

To pass a body with your request (assuming you're performing an HTTP request other than a GET), utilize the body field on the options object passed to your test.routes method:

/tests/index.server.js

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

test.that('the /api/books route returns books', async (assert = {}) => {
  const response = await test.routes.post('/api/books', { 
    body: {
      title: 'Tough and Competent',
      author: 'Gene Kranz',
    },
  });

  const existing_book = await process.databases.mongodb.collection('books').findOne({
    title: 'Tough and Competent',
  });

  assert.is(existing_book && existing_book?.author === 'Gene Kranz', true);
});

Passing Headers

If you need to test a route that relies on custom HTTP headers, you can pass a headers object in the options object passed to your test.routes method:

/tests/index.server.js

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

test.that('the /api/books route returns books', async (assert = {}) => {
  const response = await test.routes.post('/api/books', {
    headers: {
      'x-api-key': 'abc1234567890',
    },
    body: {
      title: 'Tough and Competent',
      author: 'Gene Kranz',
    },
  });

  const existing_book = await process.databases.mongodb.collection('books').findOne({
    title: 'Tough and Competent',
  });

  assert.is(existing_book && existing_book?.author === 'Gene Kranz', true);
});

Passing Query Params

If you need to test a route that relies on query params, you can pass a query object in the options object passed to your test.routes method:

/tests/index.server.js

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

test.that('the /api/books route returns books', async (assert = {}) => {
  const response = await test.routes.get('/api/books', {
    query: {
      limit: 10,
    }
  });

  assert.is(response?.body?.books?.length === 10, true);
});

Adding a User

If the route you’re testing calls for it, a user object can be passed along with the request being made:

/tests/index.server.js

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

test.that('the /api/books route returns books', async (assert = {}) => {
  const user = await test.accounts.signup({
    email_address: 'example@test.com',
    password: 'password',
  });

  const response = await test.routes.get('/api/books', {
    user
  });

  assert.is(response?.body?.books?.length === 25, true);
});

API

test.routes.get()

test.routes.get(route: string, options: object) => Promise;

Parameters

route string required
The route to perform an HTTP GET request to.
options object
The options for the HTTP GET request.

test.routes.delete()

test.routes.delete(route: string, options: object) => Promise;

Parameters

route string required
The route to perform an HTTP DELETE request to.
options object
The options for the HTTP DELETE request.

test.routes.patch()

test.routes.patch(route: string, options: object) => Promise;

Parameters

route string required
The route to perform an HTTP PATCH request to.
options object
The options for the HTTP PATCH request.

test.routes.post()

test.routes.post(route: string, options: object) => Promise;

Parameters

route string required
The route to perform an HTTP POST request to.
options object
The options for the HTTP POST request.

test.routes.put()

test.routes.put(route: string, options: object) => Promise;

Parameters

route string required
The route to perform an HTTP PUT request to.
options object
The options for the HTTP PUT request.