diff --git a/package-lock.json b/package-lock.json index 2e7aef4..4d2f5d3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,19 +1,19 @@ { "name": "@athenna/ioc", - "version": "4.7.0", + "version": "4.8.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/ioc", - "version": "4.7.0", + "version": "4.8.0", "license": "MIT", "dependencies": { "awilix": "^7.0.3" }, "devDependencies": { "@athenna/common": "^4.18.0", - "@athenna/test": "^4.12.0", + "@athenna/test": "^4.13.0", "@athenna/tsconfig": "^4.9.1", "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", @@ -123,16 +123,16 @@ } }, "node_modules/@athenna/test": { - "version": "4.12.0", - "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.12.0.tgz", - "integrity": "sha512-rBEOCVXomQtVR9568L7icSQHOE+PmkVbsDjb4Ac0icICR/Nji0O4l9O7DfizSpaAttH4bB9SLhVaFBsrBQNjAw==", + "version": "4.13.0", + "resolved": "https://registry.npmjs.org/@athenna/test/-/test-4.13.0.tgz", + "integrity": "sha512-9CNFkCtFU6UqdLFLO6RwGvP8YCo6EcZjpTop2+3WfOmBvvux70ILW5bY+jhJ46rDI85uniE4CwIGhjxAZiYutw==", "dev": true, "dependencies": { "@japa/assert": "^1.4.1", "@japa/run-failed-tests": "^1.1.0", "@japa/runner": "^2.2.2", "@japa/spec-reporter": "^1.3.3", - "@types/sinon": "^10.0.16", + "@types/sinon": "^10.0.17", "c8": "^8.0.1", "sinon": "^15.1.0" }, diff --git a/package.json b/package.json index 31d6eb6..d8f031b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/ioc", - "version": "4.7.0", + "version": "4.8.0", "description": "Global Ioc helper for Athenna ecosystem. Built on top of awilix.", "license": "MIT", "author": "João Lenon ", @@ -59,7 +59,7 @@ }, "devDependencies": { "@athenna/common": "^4.18.0", - "@athenna/test": "^4.12.0", + "@athenna/test": "^4.13.0", "@athenna/tsconfig": "^4.9.1", "@typescript-eslint/eslint-plugin": "^6.7.4", "@typescript-eslint/parser": "^6.7.4", diff --git a/src/facades/FacadeProxyHandler.ts b/src/facades/FacadeProxyHandler.ts index 53176b0..ab4ae9a 100644 --- a/src/facades/FacadeProxyHandler.ts +++ b/src/facades/FacadeProxyHandler.ts @@ -7,9 +7,14 @@ * file that was distributed with this source code. */ +import type { + SpyInstance, + MockBuilder, + StubInstance, + Mock as MockType +} from '@athenna/test' import { debug } from '#src/debug' import { Is, Module } from '@athenna/common' -import type { StubInstance, MockBuilder, Mock as MockType } from '@athenna/test' import { PROTECTED_FACADE_METHODS } from '#src/constants/ProtectedFacadeMethods' const athennaTest = await Module.safeImport('@athenna/test') @@ -28,11 +33,6 @@ export class FacadeProxyHandler { */ private provider: T = null - /** - * The stubbed service instance. - */ - private stubbed: StubInstance = null - /** * Creates a new instance of FacadeProxyHandler. */ @@ -74,14 +74,25 @@ export class FacadeProxyHandler { /** * Resolves a service instance of the - * facade and save it to be used instead of the - * original one. + * facade and save it to be used as stub. + * + * The stub will be used instead of resolving + * the service. */ public stub(): StubInstance { - this.provider = this.getProvider() - this.stubbed = athennaTest.Mock.stub(this.provider) + this.freeze() - return this.stubbed + return Mock.stub(this.provider) + } + + /** + * Resolve a service instance of the facade + * and save it to be + */ + public spy(): SpyInstance { + this.freeze() + + return Mock.spy(this.provider) } /** @@ -89,20 +100,22 @@ export class FacadeProxyHandler { * of the facade. */ public when(method: keyof T): MockBuilder { - this.provider = this.getProvider() - - const stub = Mock.when(this.provider, method) + this.freeze() - return stub + return Mock.when(this.provider, method) } /** - * Restore the facade to the original state - * by removing the stubbed instance. + * Restore the mocked facade to the original state. */ public restore(): void { - this.stubbed = null - this.provider = null + if (!this.provider) { + return + } + + Mock.restore(this.provider) + + this.unfreeze() } /** @@ -112,12 +125,6 @@ export class FacadeProxyHandler { * the same instance when a Facade method returns this. */ public set(_, key: string, value: any): boolean { - if (this.stubbed) { - this.stubbed[key] = value - - return true - } - if (this.provider) { this.provider[key] = value @@ -150,10 +157,6 @@ export class FacadeProxyHandler { return this[key].bind(this) } - if (this.stubbed) { - return this.stubbed[key] - } - if (this.provider) { return this.provider[key] }