Skip to content

Dependency injection and class inheritance doesn't work in Angular 8 library test #31337

Closed

Description

@ip7e

🐞 bug report

Affected Package

The issue is caused by package @angular/core@^8

Is this a regression?

Yes, the previous version in which this bug was not present was: @angular/core@7

Description

The problem occurs when:

  1. The base class has a constructor with an injected service. (Injector from the example)
@Injectable()
export class BaseService {
  // "Injector" is just for the sake of example. Any injectable would have the same problem.
  constructor(public injector: Injector) {
  }
}
  1. Child class has no constructor (i.e., it relies on base class constructor) AND at least one property with the default value.
@Injectable()
export class MyLibService extends BaseService {

  public foo = 'bar';

}
  1. (dont ask me how I have found this, but:) In angular.json [lib name].test.options.codeCoverage should be true.

PROBLEM:

injector is undefined in the instance of MyLibService.


🚩 Removing property from the child class (2) AND/OR opting out codeCoverage config in angular.json (3) doesn't brake test.

As a result, I can't have a class inheritance and code coverage at the same time.


I could put a constructor in the child class and call super by passing all required arguments manually, but it's the last option since the real-life scenario is much more complicated than the example provided here.

🔬 Minimal Reproduction

git repo: https://github.com/ddramone/ng-8-library-inheritance-di-test-bug

reproduce steps:

  1. Install angular 8 cli
  2. Clone git clone https://github.com/ddramone/ng-8-library-inheritance-di-test-bug.git
  3. npm i
  4. run Test ng test my-lib

Expected behavior

Test should pass = Injector service should be available in the service instance.

Activity

  1. Airblader commented on Jun 28, 2019

    @Airblader
    Contributor

    Related to microsoft/TypeScript#6979 I think.

  2. added this to the needsTriage milestone on Jun 28, 2019
  3. alxhub commented on Jul 2, 2019

    @alxhub
    Member

    Possibly related to #31333? @ddramone can you try again with the latest Angular/CLI?

  4. added
    regressionIndicates than the issue relates to something that worked in a previous version
    on Jul 2, 2019
  5. modified the milestones: needsTriage, Backlog on Jul 2, 2019
  6. ip7e commented on Jul 3, 2019

    @ip7e
    Author

    I've tried the latest cli (8.1.0), didn't work. (updated package versions in the repo as well)

  7. richardlnnr commented on Aug 21, 2019

    @richardlnnr

    I've had the same problem

  8. andreyjamer commented on Sep 9, 2019

    @andreyjamer

    I got the same problem.
    It appears only if tests are running with code coverage: ng test --code-coverage
    The usual ng test or any type of build (JIT or AOT) gives no errors.

    For now the problem can be solved by injecting services on child level and passing them to parent component.

  9. petrce commented on Oct 18, 2019

    @petrce

    tested on angular 8.2.11 with cli 8.3.12 and it is still not fixed, now I have to add tons of workarounds :(

  10. 22 remaining items

  11. added 2 commits that reference this issue on Jan 21, 2022
    ed67a07
    71d28ac
  12. angular-automatic-lock-bot commented on Feb 21, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  13. locked and limited conversation to collaborators on Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2The issue is important to a large percentage of users, with a workaroundarea: compilerIssues related to `ngc`, Angular's template compilerarea: coreIssues related to the framework runtimefreq1: lowregressionIndicates than the issue relates to something that worked in a previous versionstate: has PRtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions