doctest-js icon indicating copy to clipboard operation
doctest-js copied to clipboard

🙋‍♂️ Does this support promises

Open devnetkc opened this issue 2 years ago • 2 comments

@example LoadWidget() //=> Promise.resolve(true)

returns

  ● Doctests › doctest: LoadWidget()

    Expected value   {}
    Received:
      {}
    
    Message:
      expected {} to deeply equal {}

Am I doing something wrong or is this not supported??

devnetkc avatar Dec 04 '22 06:12 devnetkc

Hi @devnetkc -- this project seems abandoned, sadly. I have a number of PRs and issues reported, and myself use a fork in linus/doctest-js. I am currently trying to get doctest-js to use import("module").then() rather than require("module") in the eval call, which requires a Promise chain internally in doctest-js. It's currently a struggle, but if I get that one fixed, it should mean handling promises in functions under test as well.

linus avatar Dec 05 '22 13:12 linus

Hi again @devnetkc -- I have abandoned this effort and instead implemented a new module with a different approach. https://github.com/linus/testy uses jsdoc-api to parse the JSDoc, extracts any examples and executes them using vm.runInNewContext. It is very early days and I've only used it for this years' Advent of Code, but I would love early adopters, so please take it for a run!

For usage examples (it allows for some quite interesting uses, where you don't have to directly reference the exported function in the test, but can use any JS) see my Advent of Code solutions at https://github.com/linus/aoc/tree/main/2022/js.

In particular, here are a couple of creative tests: https://github.com/linus/aoc/blob/e7274aef0c7bb700f6039cfb6a3c674d51b8db45/2022/js/utils.js#L7-L16 https://github.com/linus/aoc/blob/f32bbe58c486e78d91c19ca36accb744ac5e7749/2022/js/day-10/lib.js#L36-L43

linus avatar Dec 11 '22 15:12 linus