Open DavidFaizulaev opened 6 years ago
Yup, we should revamp how the JS hooks work. I would like them to work similar to tests in Mocha - if you pass callback, it's called, otherwise a promise is returned.
Something like the following would be a start at supporting the current behaviour alongside returning promises (or using async function as the hook):
diff --git a/src/transaction-runner.js b/src/transaction-runner.js
index cbdf1cc..b391165 100644
--- a/src/transaction-runner.js
+++ b/src/transaction-runner.js
@@ -315,12 +315,18 @@ Interface of the hooks functions will be unified soon across all hook functions:
}
}
- runHook(hook, data, callback) {
+ async runHook(hook, data, callback) {
// Not sandboxed mode - hook is a function
if (typeof hook === 'function') {
if (hook.length === 1) {
// Sync api
- hook(data);
+ const promise = hook(data);
+
+ if (promise) {
+ // Promise or async/await API
+ await promise;
+ }
+
callback();
} else if (hook.length === 2) {
// Async api
This doesn't handle errors (rejected promises) properly to cause a failing test, but sharing as a start if anyone is tacking this issue.
Hello there. Is somebody working on promises support for hooks? Could you provide some tips to start working on it.
I'm writing a hooks.js file and would like to use the new option in node8 - async await. This requires me to pass only a single argument to BeforeEach, BeforeAll & AfterAll functions. This means that dredd will take as sync functions, which causes the tests to fail.
Is there a way to run dredd with async await?
Dredd version
This is the output I'm getting