13

Do any JavaScript test frameworks provide a rough equivalent to Python's doctest?

function add(a, b) {
  /**
  Returns the sum of `a` and `b`:

    > add(1, 3)
    4

  Add coerces types to numeric values where possible:

    > add('51' + 3)
    54
  */
  return (a - 0) + (b - 0);
}
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • Can you add more detail? At first glance this looked identical to http://stackoverflow.com/questions/4180224/what-is-the-standard-way-of-adding-documentation-to-a-javascript-function. – John Kugelman Nov 14 '10 at 23:18
  • This question refers to doctests, that question refers to docstrings. They're fundamentally different, although they both occur within comments. – Tim McNamara Nov 14 '10 at 23:23
  • "How to run (Python) doctests in JavaScript?" makes me think you want to run Python code in JavaScript. I think you could remove the "(Python)" from your question title. – Denilson Sá Maia Nov 15 '10 at 12:26
  • @Denilson Sá thanks for the comment. I changed the title in response to @John Kugelman's response. I wanted to clarify the distinctiveness of the question. – Tim McNamara Nov 15 '10 at 18:39
  • How about: How to run "Python-like" doctests in JavaScript? – Denilson Sá Maia Nov 15 '10 at 19:25
  • Has anyone tried https://dev.to/supabase/use-comments-to-unit-test-your-code-4igk ? – Simon B. May 05 '20 at 02:37

2 Answers2

12

I can't get the point of Ian Bicking's package, doctestjs. He just provides us a different way of writing normal external tests, not real doctests.

I use a lot python doctests, they are quite important for me, I don't know what doctestjs could be useful for, but I found some true doctests implemented with this project: https://github.com/davidchambers/doctest .

Even if that is not the most trendy doctest project for javascript, I strongly prefer it.


Update: after more that one year since this answer, i had the opportunity to contribute to the project from David Chambers, it is really well written. I also used it at work in a complex application using require.js, and for this we added support for AMD modules. I think he added support for Common JS modules as well. Thus i can only confirm my opinion.

danza
  • 11,511
  • 8
  • 40
  • 47
2

Don't know what I was smoking yesterday. Soory.

doctestjs (doctestjs on github) is the correct link and it seems to be under active development too.

Although it works a little different from the python doctest in that with doctestjs you declare the tests in a HTML file so they can be run.

But I guess it should be possible to declare the test inline in your code. And then do a preprocessing step in your build process to extract the tests and automagically create a test-html from them

e.g. when using ant I imagine an applying copy file, replaceregexp, concat. e.g. copy the js file, replace everything which isn't a comment and all comments which don't look liks doctestjs tests. then concat htmlhead+tests+headfooter done.

jitter
  • 53,475
  • 11
  • 111
  • 124