AthennaIO/IoC

[Bug]: Error when lifetime is TRANSIENT

jlenon7 opened this issue · 0 comments

Version

1.3.1

Steps to reproduce

Register a dependency with TRANSIENT lifetime and try to mock it using __mock method.

Expected behavior

I expect that TRANSIENT lifetime dependencies could be mocked.

Actual behavior

An error is thrown because transient method does not exist in Ioc class.

Additional context

Implementation to fix these scenarios in Facade.__mock method:

const dictionary = {
  SCOPED: 'scoped', // This method should be implemented in Ioc class.
  TRANSIENT: 'bind',
  SINGLETON: 'singleton'
}

const registration = Facade.container.list()[this.alias]
const lifetime = dictionary[registration.lifetime] ? dictionary[registration.lifetime] : 'bind'

Facade.depsMocksRestoreMap.set(`${this.alias}::${method}`, this.Provider.prototype[method])

this.Provider.prototype[method] = returnValue

Facade.container[lifetime](this.alias, this.Provider, true)

Environment

System:
    OS: Linux 5.14 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
    Memory: 6.19 GB / 15.03 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.18.0 - ~/.nvm/versions/node/v16.18.0/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v16.18.0/bin/npm