yamadapc / jsdoctest

Run jsdoc examples as doctests.
https://yamadapc.github.io/jsdoctest
MIT License
92 stars 9 forks source link

Support for async await? #31

Closed semmel closed 7 years ago

semmel commented 7 years ago

@jmatsushita How can I test ES7 async (i.e. Promise returning) functions?

/**
 * @example
 *   resolvesAPromise();
 *   // ~> 11
 */
async function resolvesAPromise() {
    return 11;
}

Under Node v.7.6.0 using the iilab's jsdoctest project fork I get

0 passing

Although async await is supported in Node I tried requiring these babel plugins, but no luck: syntax-async-functions and transform-regenerator.

Best regards Semmel

jmatsushita commented 7 years ago

Hi, good job discovering my fork :) it's not quite as robust as I would like and not tested with node 7 or async await. If you want to try and get to the bottom of this, I would look at the very brittle code that actually runs a Babel transform before compiling the doctest. Maybe if your .babelrc is not in scope then the transform will not pick it up.

On 24 May 2017 23:20, "Matthias Seemann" notifications@github.com wrote:

@jmatsushita https://github.com/jmatsushita How can I test ES7 async (i.e. Promise returning) functions?

/* @example resolvesAPromise(); // ~> 11 */async function resolvesAPromise() { return 11; }

Under Node v.7.6.0 using the iilab's jsdoctest project fork https://github.com/iilab/jsdoctest I get

0 passing

Although async await is supported in Node I tried requiring these babel plugins, but no luck: syntax-async-functions and transform-regenerator.

Best regards Semmel

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yamadapc/jsdoctest/issues/31, or mute the thread https://github.com/notifications/unsubscribe-auth/AAVvAdKGvY6GqoTW__O3k9DvjrOIarsXks5r9J8kgaJpZM4NlryZ .

yamadapc commented 7 years ago

async => should work on a mocha --require babel-register --require jsdoctest I'd love a failing test otherwise. We've tests testing bash scripts.

semmel commented 7 years ago

The error is caused by tj/dox v. 0.8 not supporting async functions. Fortunately @cjy37 has created a fork of dox v. 0.9 with a patch for async functions.

So I upgraded dox to v. 0.9 and "merged" @cjy37 patch by overwriting dox.js. Now everything works fine without requiring any Babel packages on the command line:

 mocha --include test/helpers/helper-promises.js --timeout 3000 --require .  test/files/returns-complex-es7-promise.js 

test/files/returns-complex-es7-promise resolvesAPromise() ✓ resolvesAPromise() (2006ms)

This being my ES7 source file:

/**
 * @example
 *   resolvesAPromise();
 *   // ~> 10
 */
async function resolvesAPromise() {
    await new Promise( resolve => setTimeout(resolve, 2000) );
    return 10;
}

Hopefully both @jmatsushita's and @cjy37 forks will sometime eventually be merged back with their parent projects so that I can upgrade my tooling better in the future.

yamadapc commented 7 years ago

Hm... I see... The recommended way would be to use babel, maybe even to preserve your code base compatible with versions of Node that don't yet support async/await.

RE dox fork; if there's some PR it'll likely be merged soon, otherwise I wouldn't mind depending on a patched version temporarily.

I'll add tests to async syntax with babel soon. Closing as invalid though.