Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run (Python-like) doctests in JavaScript?

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);
}
like image 699
Tim McNamara Avatar asked Nov 14 '10 23:11

Tim McNamara


1 Answers

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.

like image 90
danza Avatar answered Sep 19 '22 21:09

danza