Skip to main content

Auth0 Actions Unit Test

The Actions NPM (@auth0/actions) package enables the use of TypeScript development on external projects, allowing developers to follow best practices and improve their unit testing based on the TypeScript definitions.

How it works

Follow the installation and import guidelines described at: How Actions NPM works. To unit test an Action, you must mock the event and api objects that your Action function receives. You can create these mocks using the TypeScript definitions included in auth0/actions, which ensures your tests accurately reflect the production environment. Testing frameworks like Jest are ideal for managing mocking and functionality. The Unit Tests can be run in a local environment, version control, or CI/CD process, improving the overall quality assurance and validations before impacting changes on Auth0 Tenants.

Examples

The following examples provide validation for a series of scenarios by mocking the necessary objects.
The examples use Jest (https://www.npmjs.com/package/jest), but any testing library can be used.

Configuration

In your package.json, define any development dependencies to have IntelliSense help when writing your Action.

Pre-user registration access control and user metadata setup

The following example Action checks if the user email has a forbidden email domain, and calls api.access.deny() when matching. Otherwise, it checks if the full name has being provided through Custom Prompts additional fields and proceeds to set the full name at the user profile user_metadata, otherwise sends a validation error to Universal Login.
The Unit Test performs some validations to maximize code coverage, by mocking event and api objects.

Custom email provider and HTTP request

The following example Action attempts to send a message via HTTP request to an external service, handling the potential request error to drop the notification. It uses secrets for the external service URL and authorization API Key.
The Unit Test performs some validations to maximize code coverage, by mocking event and api objects, in addition to the fetch function.
To learn more about @auth0/actions, visit: https://www.npmjs.com/package/@auth0/actions. To learn more about writing Actions, read Write Your First Action.