@joystick.js/test

Unit Testing Functions

How to unit test functions in your Joystick app using the test.load() method.

Unit testing in your app should be reserved for testing individual, standalone functions (i.e., "units" of code). To unit test a function, the test.load() method can be utilized:

/tests/lib/add.test.js

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

test.that('add function adds two numbers together', async (assert = {}) => {
  const add = await test.load('lib/add.js', { default: true });
  assert.is(add(5, 5), 10);
});

Above, we've utilized the test.load() method to dynamically import a function we want to unit test in our app at /lib/add.js. When we call test.load(), a dynamic import of that path is performed. Because we expect /lib/add.js to export the add() function as a default export, we pass { default: true } as the second argument to test.load() to ensure that we're handed back the default export and not an object like { default: [Function add] }.

API

Definition

test.load(path: string, options: object) => Promise;

Parameters

path string required
The path relative to the root of your project that you want to load dynamically.
options object
Options for test.load().