From bdfea368e6de134a352f86a5bcc68307027e5ea0 Mon Sep 17 00:00:00 2001 From: Lars Neumeier Date: Sun, 19 Jan 2025 03:53:57 +0100 Subject: [PATCH] fix: #9666 Performance --- .../mock-helper/mock-helper.flush-test-bed.ts | 2 +- tests/mock-helper-flush-test-bed/test.spec.ts | 36 ++++++++++++------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/libs/ng-mocks/src/lib/mock-helper/mock-helper.flush-test-bed.ts b/libs/ng-mocks/src/lib/mock-helper/mock-helper.flush-test-bed.ts index 876f5e1e1d..6a49b67ee1 100644 --- a/libs/ng-mocks/src/lib/mock-helper/mock-helper.flush-test-bed.ts +++ b/libs/ng-mocks/src/lib/mock-helper/mock-helper.flush-test-bed.ts @@ -2,7 +2,7 @@ import { getTestBed } from '@angular/core/testing'; export default (): void => { const testBed: any = getTestBed(); - if (testBed.tearDownTestingModule != null) { + if (testBed.shouldTearDownTestingModule != null && testBed.shouldTearDownTestingModule()) { testBed.tearDownTestingModule(); } testBed._instantiated = false; diff --git a/tests/mock-helper-flush-test-bed/test.spec.ts b/tests/mock-helper-flush-test-bed/test.spec.ts index 98d04b002f..40c3d4a624 100644 --- a/tests/mock-helper-flush-test-bed/test.spec.ts +++ b/tests/mock-helper-flush-test-bed/test.spec.ts @@ -5,17 +5,29 @@ import { ngMocks } from 'ng-mocks'; describe('mock-helper-flush-test-bed', () => { it('should execute tearDownTestingModule', () => { const testBed: any = getTestBed(); - if (testBed.tearDownTestingModule == null) { - testBed._instantiated = true; - ngMocks.flushTestBed(); - expect(testBed._instantiated).toBeFalse(); - } else { - const spy = spyOn( - testBed, - 'tearDownTestingModule', - ).and.callThrough(); - ngMocks.flushTestBed(); - expect(spy).toHaveBeenCalledOnceWith(); - } + spyOn(testBed, 'shouldTearDownTestingModule').and.returnValue( + true, + ); + + const spy = spyOn( + testBed, + 'tearDownTestingModule', + ).and.callThrough(); + ngMocks.flushTestBed(); + expect(spy).toHaveBeenCalledOnceWith(); + }); + + it('should not execute tearDownTestingModule', () => { + const testBed: any = getTestBed(); + spyOn(testBed, 'shouldTearDownTestingModule').and.returnValue( + false, + ); + + const spy = spyOn( + testBed, + 'tearDownTestingModule', + ).and.callThrough(); + ngMocks.flushTestBed(); + expect(spy).not.toHaveBeenCalled(); }); });