Namespace 'jasmine' has no exported member 'SpyObj'
Asked Answered
N

5

9

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;
Nuss answered 17/9, 2018 at 8:34 Comment(3)
can you try let spy: jasmine.createSpyObj<GeneralService>;Unapproachable
check this #24321807Unapproachable
@HardikPatel Your link doesn't answer what type should be used insteadJoinder
D
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.

Dipnoan answered 30/12, 2022 at 10:26 Comment(0)
J
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.

Joinder answered 1/3, 2019 at 16:0 Comment(0)
M
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/[email protected]'

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.

Mighell answered 13/12, 2021 at 16:9 Comment(0)
P
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

Prestigious answered 5/7, 2023 at 16:30 Comment(0)
T
0

My problem was not having jasmine in the tsconfig.spec.json. Adding this fixed it for me:

"types": [
  "jasmine"
],
Thatcher answered 22/4, 2024 at 15:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.