8

I'm trying to create a spy object for my tests:

let spy: jasmine.SpyObj<GeneralService>;

Unfortunately, I'm getting this error when running ng test:

Namespace 'jasmine' has no exported member 'SpyObj'

In addition, WebStorm marks SpyObj as unresolved variable.

Here is devDependencies from my package.json:

"devDependencies": {
    "@angular/cli": "1.7.3",
    "@types/jasmine": "2.5.36",
    "@types/node": "7.0.27",
    "jasmine": "~2.4.1",
    "jasmine-core": "~2.4.1",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "lodash": "4.16.2",
    "ts-node": "1.3.0",
    "tslint": "^3.15.1",
    "typescript": "2.6.2"
  }

UPDATE:

I refer to Angular testing docs: https://angular.io/guide/testing

let masterService: MasterService;
let valueServiceSpy: jasmine.SpyObj<ValueService>;

beforeEach(() => {
  const spy = jasmine.createSpyObj('ValueService', ['getValue']);

  TestBed.configureTestingModule({
    // Provide both the service-to-test and its (spy) dependency
    providers: [
      MasterService,
      { provide: ValueService, useValue: spy }
    ]
  });
  // Inject both the service-to-test and its (spy) dependency
  masterService = TestBed.get(MasterService);
  valueServiceSpy = TestBed.get(ValueService);
});

I end up configure spy as any:

 let spy: any;
ohadinho
  • 6,894
  • 16
  • 71
  • 124

4 Answers4

1

I had working Jasmine tests with spy objects and one day it just stopped to work in my environment (but the same commit was fine in CI pipeline) with the very same error as yours (both from ng test invoked from terminal and from IDE intelli sense).

Restarting and even upgrading the IDE (Jet Brains Rider) didn't help, so I just removed node_modules directory and invoked npm install again and this solved the problem.

tometchy
  • 609
  • 5
  • 6
0

Your jasmine version does not have support for SpyObj type which was introduced in the later version.

Try updating jasmine-core and @types/jasmine from the package.json to the latest stable version or try with "jasmine-core": "2.8.0", @types/jasmine": "2.6.0" should work fine without the errors.

0

Try This:

I had the same problem message which was connected to another problem message:

File './node_modules/@types/jasmine/ts3.1/index.d.ts' not found. The file is in the program because: Entry point for implicit type library 'jasmine' with packageId '@types/jasmine/ts3.1/index.d.ts@3.5.10'

The weird thing was, that I actually had this file in my node_modules.

The solution was painfully easy:

Restart (and update) VS Code.

Everything worked again without any problem messages.

MikhailRatner
  • 452
  • 6
  • 13
0

In my case this was because typescript used jasmine from jasmine-marbles. This was because my tsconfig.json had typeRoots specified. After removing, it started using types from @types/jasmine.

TypeRoots documentation: https://www.typescriptlang.org/tsconfig#typeRoots

Karol
  • 223
  • 1
  • 3
  • 8