Cool. We already did our setup earlier, so let’s go ahead and run the test!

npx markdown-doctest

The output here is a bit misleading on account of some combination of A) the JavaScript I told Copilot to write for our snippet and B) the expected usage for markdown-doctest. 😏 But that’s OK, as we can still get a meaningful result. As I’ve highlighted below, the test reported that it encountered an HTTP response code of 404 (not found) when trying to do a fetch from the endpoint at https://jsonplaceholder.typicode.com/todoz/1.

Screenshot of the terminal showing the expected 404 (not found) error
  • Wait a minute, what’s a TODOz list? 😁 Let’s go back to our readthedocs/docs/source/js.md file and fix the code snippet.
  • Change the URL on line 7 from https://jsonplaceholder.typicode.com/todoz/1 to https://jsonplaceholder.typicode.com/todos/1 and save your changes.
  • Your js.md file should now look like this:
Screenshot showing the corrected version of the `js.md` file
  • Now run the test again
npx markdown-doctest
Screenshot of the terminal showing that all tests passed.

Look, no 404! Essentially, we just verified that after fixing a typo in one of our code snippets, the test suite is now able to reach the correct endpoint without errors. And because we caught it before the docs were released, we didn’t have to hear about the developers who called Support asking why their TODOz apps weren’t working. 😂

Great job! 👍

What now?

Since you’ve now tested a UI and API using different tools, you can officially say you have Docs as Tests experience in those areas 😎 And seriously, give yourself credit for all of the work it took to get here. Last I checked, the widget that indicates “reading time” for this post was at 20 minutes, but I suspect it took you at least 2-3 times make it through all of that setup and testing (probably more for the less technical folks 🫡).

But obviously it doesn’t end here. For my part, I’m going to move on to making a video that covers this and the last post. I know some people vibe with learning from videos more than reading, and I also have a large video-shaped hole in my portfolio 🫤

Code Coverage

After that, there is a topic that I think deserves some attention: code coverage. You’ve probably noticed that I have a strong preference for running tests that are auto-detected using tools like Doc Detective. Aside from the fact that these tests have high return on investment, they also scale with your docs automatically.

However, if code coverage of documentation-based tests is taken into account, it becomes easier for us to have confidence that we are “covering” all of our products with these tests. And, crucially, this can help us prevent our hand-crafted tests from becoming out of sync with our docs.

That’s it for now. Follow me on LinkedIn if you’d like to be notified when I publish a video or another blog post.

Cheers.

Leave a comment