Auth0 Home Blog Docs

How to write unit tests for rules

auth0
rules
#1

I have multiple rules in my tenant and now I want to write unit tests for my rules just like https://github.com/auth0/rules/tree/master/test Now the issue is in the docs (https://auth0.com/docs/support/testing) it’s written that we can use https://www.npmjs.com/package/auth0-rules-testharness module for unit test our rules but I think the explanation is old and it’s not updated because in the sample project (https://github.com/tawawa/auth0-rules-testharness-sample) 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?

#3

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!

#4

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

#5

Sure @jah!

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

Hope it helps!

#6

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');
2 Likes
#7

Hey @roshan.lobo for sharing this one too!

#8

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
#9

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.