View profile

Testing Webhooks Without Clicking in UIs

Testing Webhooks Without Clicking in UIs
By Mastering JS Weekly • Issue #76 • View online
Recently, we’ve been doing a lot of work with webhooks for Mongoose Pro and Mastering JS. We migrated Mastering JS’ eBooks from PayPal to Stripe, and we added webhooks for Mongoose GitHub comments and a basic Mongoose Pro GitHub app for pulling GitHub organization members.
Webhook development is notoriously painful and cumbersome. Too often, devs test webhooks by triggering the webhook the same way the end user will. Testing email webhooks by sending an actual email, testing GitHub commit webhooks by making an actual commit to GitHub, etc. I admit, I’ve done this a lot myself.

Testing a GitHub commit webhook. In my defense, this was a final test.
Testing a GitHub commit webhook. In my defense, this was a final test.
A Better Way: Copy Request and TDD
Instead of repeatedly triggering the webhook, just trigger the webhook HTTP request once and put the HTTP request body in a Mocha test case.
Here’s an example from the Mongoose Pro backend. The function being tested is responsible for pulling a GitHub organization’s members - membership in a GitHub org can be either public or private, and the easiest way to consistently provide access to private members is to ask the organization to install a GitHub app.
Instead of repeatedly installing and uninstalling the GitHub app, I just copied a portion of the request that the GitHub webhook sends and put it in a test case.
Part of the GitHub HTTP request. Hard code this in a test case
Part of the GitHub HTTP request. Hard code this in a test case
With this code in a test case, you don’t need to click through a UI to trigger the webhook. You can just run `npm test` to build out your initial webhook handler, and use automated testing to make sure your code doesn’t break unless the HTTP request structure changes.
We’ve got a bunch more webhooks to develop: GitHub sponsors, inbound Slack messages, and more. We’re very bullish on headless apps, and webhooks are the bread and butter of headless. We’re using this TDD pattern exclusively going forward. No more testing by clicking in UIs except as an initial experiment or as a final spot check.
Most Recent Tutorials
How to Compare an Array of Numbers in JavaScript - Mastering JS
How to Delete By Id in Mongoose - Mastering JS
How to Check if a Property is Undefined in JavaScript - Mastering JS
How to Reverse an Array in JavaScript - Mastering JS
How to Parse URL in JavaScript into Host, Path, Search, and Hash - Mastering JS
What We're Reading
Did you enjoy this issue?
Mastering JS Weekly

Pragmatic web development. No bloatware allowed!

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue