Skip to content

Library doesn't share same ComponentFixture and TestBed classes as project #10980

@jnizet

Description

@jnizet

I'm trying to write a library to help writing unit tests, and I'm facing a really strange bug: the TestBed and ComponentFixture classes used by the library are different from the TestBed and ComponentFixture classes used in the tests of the external app using the library.

The two projects are separate: the app uses yarn link testlib to use the testlib library, since it's not published anywhere yet.

The bug does not happen when the lib and the app are in the same Angular project.

Versions

Node: 8.11.1
OS: darwin x64
Angular: 6.0.3
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.6.3
@angular-devkit/build-angular     0.6.3
@angular-devkit/build-optimizer   0.6.3
@angular-devkit/core              0.6.3
@angular-devkit/schematics        0.6.3
@ngtools/webpack                  6.0.3
@schematics/angular               0.6.3
@schematics/update                0.6.3
rxjs                              6.2.0
typescript                        2.7.2
webpack                           4.8.3

Repro steps

  • configure the CLI to use yarn
  • create a new project: ng new project1
  • inside project1, create a library: ng generate library testlib
  • add the following class to the library, make sure it's part of the public api:
import { ComponentFixture, TestBed } from '@angular/core/testing';

export class LibTest {
  test(arg: any, bed: any) {
    console.log('Is arg instanceof ComponentFixture: ', (arg instanceof ComponentFixture));
    console.log('bed: ', bed);
    console.log('bed === TestBed ', bed === TestBed);
  }
}
  • Build the library: ng build testlib --prod
  • link the built library: cd dist/testlib; yarn link
  • Elsewhere, create a new app: ng new myapp
  • Inside myapp, add testlib by linking it: yarn link "testlib"
  • Edit the app.component.spec.ts file, and add the following:
import { LibTest } from 'testlib';
[...]
  it('should share ComponentFixture and TestBed with testlib', () => {
    const fixture = TestBed.createComponent(AppComponent);
    new LibTest().test(fixture, TestBed);
  });
  • Execute the test: ng test

Observed behavior

LOG: 'Is arg instanceof ComponentFixture: ', false
LOG: 'bed: ', function TestBed() { ... }
LOG: 'bed === TestBed ', false

Desired behavior

LOG: 'Is arg instanceof ComponentFixture: ', true
LOG: 'bed: ', function TestBed() { ... }
LOG: 'bed === TestBed ', true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions