Auth0 Home Blog Docs

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 https://www.npmjs.com/package/auth0-rules-testharness with this sample (https://github.com/tawawa/auth0-rules-testharness-sample), but both rely on a webtask token.

This question comes from a Professional Services customer

There are two ways:

  1. The NPM package ( https://www.npmjs.com/package/auth0-rules-testharness) 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.