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.
What snippets are
Section titled “What snippets are”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).
Authoring a snippet
Section titled “Authoring a snippet”- Open the Snippets page (Snippets in the sidebar).
- Click New Snippet.
- Give it a name of up to 64 characters using letters, numbers, hyphens, and underscores (for example,
login-helperorsign_request). The name is how you require it in scripts. - Write JavaScript that exports a function or value via
module.exports. A snippet can hold up to 100000 characters. - Click Save Snippet.
The Free plan includes up to 5 snippets; paid plans include up to 100.
Example snippet named login:
// Snippet: loginconst 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.
Using a snippet in a script
Section titled “Using a snippet in a script”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 checkThe 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.
Notes and limitations
Section titled “Notes and limitations”- Literal path only. Only the form
require('./snippets/<name>')is resolved. Dynamic paths such asrequire('./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,requestorresponseif your script passes it in.
Related guides
Section titled “Related guides”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.