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 theevent 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 yourpackage.json, define any development dependencies to have IntelliSense help when writing your Action.
- Javascript
- TypeScript
Pre-user registration access control and user metadata setup
The following example Action checks if the user email has a forbidden email domain, and callsapi.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.
- Javascript
- TypeScript
event and api objects.
- Javascript
- TypeScript
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.- Javascript
- TypeScript
- Javascript
- TypeScript
@auth0/actions, visit: https://www.npmjs.com/package/@auth0/actions.
To learn more about writing Actions, read Write Your First Action.