Auth0 Home Blog Docs

How to write unit tests for rules


I have multiple rules in my tenant and now I want to write unit tests for my rules just like Now the issue is in the docs ( it’s written that we can use module for unit test our rules but I think the explanation is old and it’s not updated because in the sample project ( we need to add webtask token and sandbox url and now we can’t find webtask token in Auth0 dashboard ( and also where can I get this sandbox url? Any help?


Hey there @jah!

You’re right the readme file in the sample repo seems outdated. I’ve submitted an issue so that it can be updated. Using your Auth0 sandbox for creating custom webtasks is a deprecated feature. You can, however, create a free account at and from that page you should be able to follow the instructions for creating your test harness as a webtask there (webtask token + sandbox url).

Let me know if that helps!


Thanks for your response. Can you give the instructions page link on because when I created new account on i was just redirected to the dashboard and I couldn’t find any instructions page there


Sure @jah!

This workshop content will give you a quick tour guide on webtasks and how to use them:

Hope it helps!


I assume you are looking to write unit test, if yes here is what we did. See if it helps you.

  1. We treated a rule like a private nodejs function. (Since it is not exported as per the syntax).
    Make sure you name the function
function functionName(user, context, callback) {
return callback(null, user, context);
  1. Use mocha (Or any other test famework that suits you)
  2. Use rewire to get the code to be tested.
const fileName=rewire('file which has the rule to be tested');
  1. Defined all the global variables used.(These globals need to be defined before setting them via rewire)
global.auth0= {};
  1. Use rewire to set the globals
fileName.__set__('auth0', {})
fileName.__set__('configuration', {})
  1. Get the function to be tested and test it like how you would test a node js function (For a given set of inputs what are the outputs).
const functionName= fileName.__get__('functionName');

Hey @roshan.lobo for sharing this one too!


Hi @konrad.sopala,

Not sure if its there in your roadmap or there are any technical limitations, but it would be better if the rules syntax was updated to

module.exports = function (user, context, cb) {

Similar to how it is right now for the DB migration scripts and hooks, the rewire step can be eliminated and tests can be written more easily.


1 Like

Thanks a lot @roshan.lobo!

I’ll share it with our product team. If at any point in the future, you’d like to provide any product feedback or feature requests go ahead to:

I’ll report this one for you! Thanks a lot!

closed #10

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.