How to fix Module couldn’t be connected error in Codeception

You are advanced enough to create your own Codeception modules, aren’t you? It sounds great. However, there is a high chance that you meet with such an unexpected and mysterious error like the following:

Codeception\Exception\ModuleException

Well, custom module couldn’t be connected, but why? And how to fix this error? The reason is trivial: Codeception does not have proper DI container for Modules, so the order of modules declaration does matter. In other words, if the module is used in another module, but declared (“connected” in terms of Codeception) after it in the *.yml config, then you will see the error. Solution: just move the problematic module up in the list (but not above the modules it uses itself, otherwise you will see similar error for another modules as well 🙂).

Another possible reason is calling getModule() with a module class name which is not indentical to what is mentioned in *.yml config file. For example, if you have the following in your code, and $this is a Codeception module:

instanciating Codeception Module by class name

Then you have to specify this module in your config without leading slash, because ::class does not have a leading slash, and class names must match:

api.suite.yml content — module is declared without leading slash

Same idea is applicable for built-in Codeception modules. Thus, if you want to use handy $moduleContainer->getModule(REST::class) style, you need to specify REST module like this in your api.suite.yml:

Specifiing REST module explicitly to be able to reference it easily

Thanks Luciano for the catch.

TL;DR

The problem usually has two possible reasons:

  • Modules’ declaration order in the codeception *.yml config. Just move the problematic module up, so it is declared before it is used in other modules.
  • Calling getModule with wrong class name — make sure those class names are identical to *.yml version.

Making the world a better place for someone else. https://github.com/ivastly/