If I include the configuration setting noImplicitAny
in the tsconfig.json
file of my Angular 4+ project:
"noImplicitAny": true,
...and then try to import and use Spy
in a unit test:
import { Spy } from "karma-jasmine";
I encounter this console error when running npm test
:
ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
Could not find a declaration file for module 'karma-jasmine'.
'C:/test-project/node_modules/karma-jasmine/lib/index.js' implicitly has an 'any' type.
Try `npm install @types/karma-jasmine` if it exists or add a new declaration (.d.ts) file containing `declare module 'karma-jasmine';`
To address this issue, I attempted the following steps:
npm install --save-dev @types/karma-jasmine
Add
"types": [ "karma-jasmine" ]
to yourtsconfig.json
However, now I am faced with a different console error after running npm test
:
ERROR in C:/test-project/src/app/components/test/test.component.spec.ts (1,21):
File 'C:/test-project/node_modules/@types/karma-jasmine/index.d.ts' is not recognized as a module.
So, how can I successfully import Spy
while keeping "noImplicitAny": true
activated?
Note that switching back to false
(the default value) resolves the import issue without errors.
Here's an example usage scenario:
const testSpy: Spy = spyOn(testService, "test").and.callThrough();