Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Performance issue #9666

Open
lneumeier-visma opened this issue Aug 5, 2024 · 0 comments
Open

Bug: Performance issue #9666

lneumeier-visma opened this issue Aug 5, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@lneumeier-visma
Copy link

Description of the bug

ngMocks.flushTestBed() function prevents the cleanup-code in angular's TestBed.tearDownTestingModule function. As a result, unit test execution is much slower, because of duplicated, not cleaned up shared host styles (and maybe other objects). (3000 tests, 3-4 minutes instead of 1-1,5 minutes)

An example of the bug

ngMocks.flushTestBed() called from https://github.com/help-me-mom/ng-mocks/blob/master/libs/ng-mocks/src/lib/common/ng-mocks-global-overrides.ts#L56 sets the _testModuleRef to null
(https://github.com/help-me-mom/ng-mocks/blob/master/libs/ng-mocks/src/lib/mock-helper/mock-helper.flush-test-bed.ts#L7)

This prevents the cleanup of the test module.
https://github.com/angular/angular/blob/main/packages/core/testing/src/test_bed.ts#L735

  // If the module ref has already been destroyed, we won't be able to get a test renderer.
    if (this._testModuleRef === null) {
      return;
    }
    // Resolve the renderer ahead of time, because we want to remove the root elements as the very
    // last step, but the injector will be destroyed as a part of the module ref destruction.
    const testRenderer = this.inject(TestComponentRenderer);
    try {
      this._testModuleRef.destroy();
    } catch (e) {
      if (this.shouldRethrowTeardownErrors()) {
        throw e;
      } else {
        console.error('Error during cleanup of a testing module', {
          component: this._testModuleRef.instance,
          stacktrace: e,
        });
      }
    } finally {
      testRenderer.removeAllRootElements?.();
    }

Link:

Expected vs actual behavior

Workaround tests.ts:
Add

afterEach(() => { getTestBed().inject(ɵSharedStylesHost).ngOnDestroy(); });

angular/angular#31834 (comment)

Possible fix in ng-mocks (mock-helper.flush-test-bed.ts)

export default (): void => {
  const testBed: any = getTestBed();
  testBed.tearDownTestingModule(); // run cleanup
  testBed._instantiated = false;
  testBed._moduleFactory = undefined;
  testBed._testModuleRef = null;
};
@lneumeier-visma lneumeier-visma added the bug Something isn't working label Aug 5, 2024
@lneumeier-visma lneumeier-visma changed the title Bug: Performance issues Bug: Performance issue Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant