TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Best libraries for converting JavaScript comments to both docs and tests?

3 pointsby tfbalmost 10 years ago
Basically, I&#x27;d like to automate docs and tests as much as possible from comments like this (or something similar):<p><pre><code> &#x2F;** * Adds `b` to `a`. * * @param a {Number} * @param b {Number} * @return {Number} * @api public *&#x2F; function add (a, b) { return a + b; } </code></pre> Obviously there are libraries that can do what I&#x27;m asking, but I&#x27;m having trouble finding the best one(s), so I figured I&#x27;d just ask here. :)

2 comments

trcollinsonalmost 10 years ago
How exactly would you like to be able to turn these into tests? I can see how this would turn into some sort of rudimentary documentation. However, the testing is a bit harder. Looking at your example, from the comments we know that your method accepts two numbers and returns a number. So what should the test framework do? @param a = 5, @param b = -23482739, @return = 9038745. Wait, no that&#x27;s not right, the test failed. I guess you could run some rudimentary tests like &quot;Does @param a only accept numbers?&quot; or &quot;Does @return only return a number?&quot; or even &quot;Is the add method in the @api public?&quot; but I am not sure I see the value of that.<p>Now what you might want is something like:<p><pre><code> &#x2F;** * Adds `b` to `a`. * * @param a {Number} * @param b {Number} * @return {Number} * @api public * @test https:&#x2F;&#x2F;github.com&#x2F;blah&#x2F;something&#x2F;blob&#x2F;master&#x2F;test&#x2F;add_test.js#L53 * *&#x2F; </code></pre> Then the documentation could link out to the specific test or tests for this method. Though if you are going to go this route you will probably want some sort of automated code analysis to find the test locations and link them in. Creating rudimentary documentation from these comments should be trivial.
评论 #10011221 未加载
tfbalmost 10 years ago
Forgot to add: Compatibility with ES6&#x2F;ES7 is preferred.<p>Edit: Found <a href="https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;dox" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tj&#x2F;dox</a>, but surely there&#x27;s a way to convert these comments (with maybe a little extra info) to tests?