Codeceptjs: Add an async init hook

Created on 29 Jul 2016  路  4Comments  路  Source: codeceptjs/CodeceptJS

It would we good to have an asynchronous called init hook.

I tried to implement a helper for the selenium-standalone package. Ideally it should use the _init hook of a helper. So my code looked like this:

const selenium = require('selenium-standalone');

'use strict';

class SeleniumHelper extends Helper {

  _init() {
    return selenium.install({logger: function(message) {
      console.log(message);
    }}, function() {
      selenium.start(() => {console.log('blub')});
    });
  }
}

module.exports = SeleniumHelper;

The problem is here, that the _init hook is called synchronously in the lib/listener/helpers.js

event.dispatcher.on(event.all.before, function () {
  runHelpersHook('_init');
});

This means, that the init will not wait for the selenium helper to be correctly initialized, so further steps will fails as selenium wasn't started. Also the beforeSuite step is not helping as it is also called synchronously. Only the before hook would work.

By changing the above dispatch event to

event.dispatcher.on(event.all.before, function () {
  runAsyncHelpersHook('_init');
});

The init would wait for the initialization of the selenium standalone to complete. But as there may be code in the wild relying on synchronous initialization, you may introduce a new hook which is called asynchronously.

enhancement need investigation

Most helpful comment

I also need this!

All 4 comments

Somehow it's not working anymore now. So I'm still testing a little bit more to get it correctly working.

Somehow it's not working anymore now. So I'm still testing a little bit more to get it correctly working.

Yes, it failed for me now. The reason is that recorder, a singleton that records promise chains runs only inside a test. So init can't be async because recorder is not started at that point. However, I will try to think on how it can be implemented in different way

I also need this!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dsypien picture dsypien  路  4Comments

javigomez picture javigomez  路  3Comments

himanshuqdabra picture himanshuqdabra  路  3Comments

neil-s picture neil-s  路  3Comments

foobarth picture foobarth  路  4Comments