How to best unit test custom code?

What’s the best approach to unit test custom actions, custom hooks and custom middleware with botpress server?

Since custom actions, hooks and middleware are not regular javascript modules we need to write modules and then special adapters ( real actions and hooks) to import the modules. This is quite an overhead, especially when the codebase grows… Moreover, the adapters can’t be testet at all.

What’s the intended testing strategy for custom actions, custom hooks and custom middlewares?

2 Likes

unit tests with mocha

Hi @Soufyanbreda, how exactly would you test custom actions with mocha? Can you provide me a project setup that shows how to test a custom action in an automated manner?

Let’s say you want to test this action which, in the end, has to be in data/global/actions/ at bot-start.

/**
 * Get number of conversations
 * @title Get number of conversations
 * @category Storage
 * @author Botpress, Inc.
 * @param {string} output - The state variable to output the count to
 */
const getNumberOfConversations = async output => {
  const userId = event.target;
  const botId = event.botId;
  const key = bp.kvs.getUserStorageKey(userId, 'numberOfConversations');
  const value = await bp.kvs.getStorageWithExpiry(botId, key);
  temp[output] = value;
};

return getNumberOfConversations();

Anyone an idea?

Bump! I’d also like to know how to write unit tests for bot code, what kind of other tests would you recommend for an automated dev-ops pipeline?

There is also a nlu testing module in experimental state, can we start using it soon?

2 Likes

Hey guys,

As @crixx said, the best way to unit test custom actions and hooks would be to develop actions in proper javascript modules and require the module in the actual custom actions. The modules can be unit tested with any javascript framework you’ld like (like for instance jest.)

As for the hooks, if you want to make them really robust, I suggest you switch to middlewares instead. Middlewares act just like hooks, but can be coded in Typescript instead of Javascript which is way less error prown. To write middlewares, you’ll need to create a custom module, which you can learn about in our documentation.

As for the nlu-testing module mentionned by @JustusNBB, it is only an internal tool. We use it to detect regressions in our own nlu and tbh, we are at the moment trying to replace it by another external tool.

There used to be a module in botpress called testing. If I remember well, this module could register conversation scenarios and make sure the conversations take place as expected. I haven’t checked it for a while and I feel like it might need some love.

I know we’ve talked about developping better tools to help you guys use botpress in a more robust way and I’m pretty sure it’s on our road map for 2021, but I can’t make any promise on when such features will be available.

If you guys have any other questions, features requests or bug report, feel free to post again on this forum, or post an issue on our github. We’ll do our best to answer it.

Thanks again for your contribution to the forum,

François

2 Likes