PillowPillow / ng2-webstorage

Localstorage and sessionstorage manager - Angular service
MIT License
428 stars 91 forks source link

Can't resolve all parameters for LocalStorageService: (?). #100

Closed lyubomir-bozhinov closed 5 years ago

lyubomir-bozhinov commented 5 years ago

@PillowPillow, apologies, if this has been solved in another ticket - I looked through them but couldn't find an answer.

Versions (please complete the following information):

Describe the bug I cannot create tests for components which import my project's StorageService (which internally imports LocalStorageService and SessionStorageService). Angular dependency injection needs them to be imported into the spec file or else it throws a 'missing providers' error. However, I end up with the following error when I do import them:

Failed: Can't resolve all parameters for LocalStorageService: (?).
        error properties: Object({ ngSyntaxError: true })
        Error: Can't resolve all parameters for LocalStorageService: (?).
            at syntaxError (dist/vendor/@angular/compiler/bundles/compiler.umd.js:2497:21)
            at CompileMetadataResolver._getDependenciesMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18746:39)
            at CompileMetadataResolver._getTypeMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18639:30)
            at CompileMetadataResolver._getInjectableTypeMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18861:25)
            at CompileMetadataResolver.getProviderMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18870:26)
            at eval (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18808:53)
            at <Jasmine>
            at CompileMetadataResolver._getProvidersMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18768:23)
            at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18245:34)
            at CompileMetadataResolver._getEntryComponentMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18842:32)
            at eval (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18833:35)
            at <Jasmine>
            at CompileMetadataResolver._getEntryComponentsFromProvider (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18832:34)
            at eval (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18805:96)
            at <Jasmine>
            at CompileMetadataResolver._getProvidersMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18768:23)
            at eval (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18770:27)
            at <Jasmine>
            at CompileMetadataResolver._getProvidersMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18768:23)
            at eval (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18391:76)
            at <Jasmine>
            at CompileMetadataResolver.getNgModuleMetadata (dist/vendor/@angular/compiler/bundles/compiler.umd.js:18382:53)
            at JitCompiler._loadModules (dist/vendor/@angular/compiler/bundles/compiler.umd.js:25771:55)
            at JitCompiler._compileModuleAndAllComponents (dist/vendor/@angular/compiler/bundles/compiler.umd.js:25759:40)
            at JitCompiler.compileModuleAndAllComponentsAsync (dist/vendor/@angular/compiler/bundles/compiler.umd.js:25718:41)
            at CompilerImpl.compileModuleAndAllComponentsAsync (dist/vendor/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js:212:35)
            at TestingCompilerImpl.compileModuleAndAllComponentsAsync (dist/vendor/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js:148:35)
            at TestBedViewEngine.compileComponents (dist/vendor/@angular/core/bundles/core-testing.umd.js:1724:35)
            at Function.TestBedViewEngine.compileComponents (dist/vendor/@angular/core/bundles/core-testing.umd.js:1583:81)
            at UserContext.testing_1.async (dist/@xtip/components/betslip/betslip.spec.js:195:27)
            at ZoneDelegate.invoke (dist/vendor/zone.js/dist/zone.js:388:26)
            at AsyncTestZoneSpec.onInvoke (dist/vendor/zone.js/dist/async-test.js:106:39)
            at ProxyZoneSpec.onInvoke (dist/vendor/zone.js/dist/proxy.js:125:39)
            at ZoneDelegate.invoke (dist/vendor/zone.js/dist/zone.js:387:32)
            at Zone.runGuarded (dist/vendor/zone.js/dist/zone.js:151:47)
            at runInTestZone (dist/vendor/zone.js/dist/async-test.js:234:29)
            at UserContext.<anonymous> (dist/vendor/zone.js/dist/async-test.js:168:17)
            at ZoneDelegate.invoke (dist/vendor/zone.js/dist/zone.js:388:26)
            at ProxyZoneSpec.onInvoke (dist/vendor/zone.js/dist/proxy.js:128:39)
            at ZoneDelegate.invoke (dist/vendor/zone.js/dist/zone.js:387:32)
            at Zone.run (dist/vendor/zone.js/dist/zone.js:138:43)
            at runInTestZone (dist/vendor/zone.js/dist/jasmine-patch.js:142:34)
            at UserContext.<anonymous> (dist/vendor/zone.js/dist/jasmine-patch.js:158:20)
            at <Jasmine>
            at ZoneDelegate.invokeTask (dist/vendor/zone.js/dist/zone.js:421:31)
            at Zone.runTask (dist/vendor/zone.js/dist/zone.js:188:47)
            at drainMicroTaskQueue (dist/vendor/zone.js/dist/zone.js:595:35)

Example code:

import { LocalStorageService, SessionStorageService} from 'ngx-webstorage';
import { StorageService } from '@app/common/services';

...

TestBed.configureTestingModule({
      imports: [...],
      declarations: [...],
      providers: [
        LocalStorageService,
        SessionStorageService,
        StorageService,
      ]
    });

This looks like quite a common use case to me, and maybe I'm missing something. Any help would be appreciated.

PillowPillow commented 5 years ago

Hi @MasterRennor , did you try to import NgxWebstorage in your testing module instead of declaring the services you need? As you can see here LocalStorageService & SessionStorageService need some extra inner services to run and with your current method they can't access them.

Here is an example of how to test the library (with a mock but it's not required).

regards,

lyubomir-bozhinov commented 5 years ago

@PillowPillow, thanks for you help! I'll try out your suggestions soon (and the second is sure to work) - but you can go ahead and close the issue.

nexaddo commented 5 years ago

I'm also getting this error when writing tests that include SessionStorageService or LocalStorageService as a provider. Importing NgxWebstorageModule.forRoot() doesn't solve the issue for me in our failed tests.

Is there a way to get the tests working without mocking the internal state of the ngx-webstorage library?

lyubomir-bozhinov commented 5 years ago

@nexaddo, in most cases, importing NgxWebstorageModule.forRoot() and removing the imported SessionStorageService and LocalStorageService fixes the issue. In several of my specs I had to use the mock approach. Just my two cents.

PillowPillow commented 5 years ago

Hi @nexaddo , as @MasterRennor said, you shouldn't have to declare SessionStorageService & LocalStorageService in the providers section. Import NgxWebstorageModule.forRoot() and the services should be available without mocking anything.