How to unit test rules as part of CI/CD pipeline

Do we have any method to unit test just rules, as we have deprecated webtasks.

The docs used to point to this module auth0-rules-testharness - npm with this sample (GitHub - tawawa/auth0-rules-testharness-sample: Seed project for auth0-rules-testharness to help users get started immediately), but both rely on a webtask token.

This question comes from a Professional Services customer

There are two ways:

  1. The NPM package ( auth0-rules-testharness - npm) is quite simple and can be modified to run a rule locally and not in the webtask sandbox.
    The auth0-rules-local-testharness library provides an easy way to deploy, execute, and test the output of Auth0 Rules using a local sandbox environment without modifying the actual rule.

  2. You can wrap the rule in a simple anonymous function which immediately executes. This function will return the rule as CommonJS module if the code is invoked from module system.
    This enables you to load the rule in your tests using standard require(‘debug_rule’) statement and you can use packages like nock to mock http requests etc. However, this requires you to modify the rule.

debug_rule.js

(()=>{
  function rule(user, context, callback) {
    request.post({
      url: 'https://requestbin.com/zumtg7zu',
      json: {
        user: user,
        context: context,
      },
      timeout: 15000
    }, function(err, response, body){
      if (err) return callback(new Error(err));
      return callback(null, user, context);
    });
  }
   
  if (module) {
    module.exports = rule;
  }
   
  return rule;
   
})()
1 Like

Thanks a lot @sumana.malkapuram for sharing it with the rest of community!

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