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 rules/test at master · auth0/rules · GitHub Now the issue is in the docs (Run Pre-Deployment Tests) it’s written that we can use auth0-rules-testharness - npm module for unit test our rules but I think the explanation is old and it’s not updated because in the sample project (GitHub - tawawa/auth0-rules-testharness-sample: Seed project for auth0-rules-testharness to help users get started immediately) we need to add webtask token and sandbox url and now we can’t find webtask token in Auth0 dashboard (https://manage.auth0.com/#/account/webtasks) and also where can I get this sandbox url? Any help?

1 Like

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 https://webtask.io 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!

Hi,
Thanks for your response. Can you give the instructions page link on webtask.io because when I created new account on webtask.io. 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!

1 Like

Hi,
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)
    EX:
global.auth0= {};
global.configuration={};
  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');
7 Likes

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.

Cheers,
Roshan

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!

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