Skip to content

Jasmine createSpyObj w/ properties: Properties lost when spy is provided to and retrieved from TestBed #33657

@daleclements

Description

@daleclements

🐞 bug report

Is this a regression?

No

Description

Jasmine 3.5.0 added a new parameter to createSpyObj that allows you to specify properties in addition to methods (jasmine/jasmine#1442), however if I create a spy object that way in a beforeEach() and provide it in TestBed.configureTestingModule, then get it with TestBed.get() in an "it" block, the mock function is there but the mock property is not.

🔬 Minimal Reproduction

https://stackblitz.com/edit/angular-issue-repro2-yayobe I tried to make a blitz with a repro but I have no idea how to make it actually run the test.

The important part is this:

describe('AppComponent', () => {
  beforeEach(() => {

    //Note the third parameter
    // @ts-ignore
    const someServiceSpy = jasmine.createSpyObj('SomeService', ['someFunction'], ['aProperty']);

    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      providers: [
        {provide: SomeService, useValue: someServiceSpy},
      ]
    }).compileComponents();

  });

  it('should create the app', () => {
    //spyObj will have someFunction, but will not have aProperty
    let spyObj = TestBed.get(SomeService);
  });

🌍 Your Environment

Angular Version:



Angular CLI: 7.3.9
Node: 10.15.0
OS: win32 x64
Angular: 7.2.15
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.8.9
@angular-devkit/build-angular     0.8.9
@angular-devkit/build-optimizer   0.8.9
@angular-devkit/build-webpack     0.8.9
@angular-devkit/core              0.8.9
@angular-devkit/schematics        7.3.9
@angular/cdk                      7.3.7
@angular/cli                      7.3.9
@ngtools/webpack                  6.2.9
@schematics/angular               7.3.9
@schematics/update                0.13.9
rxjs                              6.5.3
typescript                        3.2.4
webpack                           4.16.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testingIssues related to Angular testing features, such as TestBed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions