Skip to content

Debug and test function injection

While developing an extension that injects serverless functions, you can test it locally to check if it works as expected.

You can only test functions after you inject them into a site

Since functions are created within your extension, the only way to test them is by adding your extension and injecting the functions into a test site first. Once you run that test site, you can invoke functions while the site runs, as documented below. There is no way to test them in isolation.

Test your extension

To test your extension, you need to prepare a test site and then run netlify dev or netlify build to inject the functions.

Prepare a test site

  1. Have a test site repository and your extension repository on your local machine.
  2. Create a test site on Netlify.
  3. Install the Netlify CLI with npm install netlify-cli -g.
  4. Make sure your test site is a git repository by running git init in the root of your test site.
  5. Log in to your Netlify account with netlify login.
  6. Link your local test site to the test site you created on Netlify by running netlify link.
  7. Create a netlify.toml file in the root of your test site with the following configuration. Make sure to update the name to match your extension’s slug and the path to specify the location of the extension on your local machine.
netlify.toml
# Use your extension’s slug as the name
[[integrations]]
name = "my-extension"
[integrations.dev]
# Set the "path" as the path from your test site to your extension’s build folder.
path = "../path/to/extension/repository"
# The following forces the extension to be used when building the test site.
force_run_in_build = true

Test injection and invocations locally

Now that your test site is ready, you can test your extension and the functions locally. To do so, run the following command for your test site in your terminal:

netlify dev

Netlify will inject the functions from your extension into the site during the onPreDev build event and then start a development environment that executes functions on local requests.

You can then invoke and debug the functions on your test site by making a request to the appropriate site path for each function. Learn more about testing and debugging serverless functions locally.

To invoke background functions, make a POST request to the functions path once your site is running.

Scheduled functions won’t run on a schedule locally but you can invoke them immediately for testing. Learn more about developing and debugging scheduled functions.

If you only want to test the injection step, you can run the following in your terminal:

netlify build --context=dev

This runs a build of your test site. Netlify will inject the functions from your extension into the site during the onPreBuild build event.

When you make changes to your extension’s functions, you can check the results in your terminal by re-running the build command. Any console.log statements will show in the terminal.

Note that if your extension includes custom build event handlers that run onPreBuild or onPreDev, Netlify injects the functions before your build event handlers run.

Got it!

Your feedback helps us improve our docs.