Reusable snippets
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
Snippets are team-scoped JavaScript modules stored in the app. Every member of your team can use them. When LoadFocus runs a check, it resolves the snippet by name and injects it into the script's sandbox, so the code runs with the same access to ctx, environment variables, and available npm modules as the script itself.
Authoring a snippet
- Open the Snippets page from the sidebar (listed under the settings group).
- Click New snippet.
- Give it a name 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.
Example snippet named login:
// Snippet: loginconst axios = require('axios');module.exports = async function login(ctx) {const res = await axios.post('https://api.example.com/auth', {username: ctx.env.USERNAME,password: ctx.env.SECRET,});ctx.vars.token = res.data.token;};
This snippet fetches a token and stores it in ctx.vars.token so the check can read it.
Using a snippet in a script
Require the snippet by name using the path ./snippets/<name>:
const login = require('./snippets/login');module.exports = async function setup(ctx) {await login(ctx);// ctx.vars.token is now available for the check to use};
The same pattern works in teardown scripts, browser scripts, and multistep scripts.
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 share access to
ctx, environment variables, secrets, and the available npm module set.
Related guides
See Setup and teardown scripts for how setup and teardown scripts work and what you can do with them.