Skip to content

Reusable snippets

When several checks share the same login flow or helper logic, copying that code into every script creates a maintenance problem. Reusable snippets let you write the logic once, give it a name, and require it from any setup script, teardown script, browser script, or multistep script in your team.

Snippets are team-scoped JavaScript modules stored in the app. Every member of your team can use them. When LoadFocus runs a check, it finds every require('./snippets/<name>') in your scripts, ships those snippets with the run, and the sandbox resolves them. A snippet can use the same npm modules a script can (for example axios, got and crypto).

  1. Open the Snippets page (Snippets in the sidebar).
  2. Click New Snippet.
  3. Give it a name of up to 64 characters using letters, numbers, hyphens, and underscores (for example, login-helper or sign_request). The name is how you require it in scripts.
  4. Write JavaScript that exports a function or value via module.exports. A snippet can hold up to 100000 characters.
  5. Click Save Snippet.

The Free plan includes up to 5 snippets; paid plans include up to 100.

Example snippet named login:

// Snippet: login
const axios = require('axios');
module.exports = async function login(ctx, username, password) {
const res = await axios.post('https://api.example.com/auth', { username, password });
ctx.vars.token = res.data.token;
};

This snippet fetches a token and stores it in ctx.vars.token so the check can read it. The credentials are passed in as arguments, because a ctx.secrets reference written only inside a snippet is not resolved for the run.

Require the snippet by name using the path ./snippets/<name>. Your script text is already the body of the setup function, so write plain statements and do not add module.exports. For example, in a browser check setup script:

const login = require('./snippets/login');
await login(ctx, 'ci-user', ctx.secrets.PASSWORD);
// ctx.vars.token is now available to the check

The same require works in teardown scripts, browser scripts, and multistep scripts. API check scripts receive request (setup) or request and response (teardown) instead of ctx, so pass those to your snippet function.

  • Literal path only. Only the form require('./snippets/<name>') is resolved. Dynamic paths such as require('./snippets/' + name) are not supported.
  • No snippet-to-snippet resolution (v1). A snippet that requires another snippet is not auto-resolved. Flatten shared logic into a single snippet if needed.
  • Missing snippets fail fast. If a required snippet does not exist, the check run fails immediately before it is dispatched, with an error identifying the missing name.
  • Shared sandbox. Snippets run in the same sandbox as the calling script and can use the same npm modules. A snippet only sees ctx, request or response if your script passes it in.

See Setup and teardown scripts for how setup and teardown scripts work and what you can do with them, and Snippets for the short version of this guide.