I recently changed my app to use aurelia-authentication
. This is injected normally into my services
authentication-service.ts
import { AuthService } from "aurelia-authentication";
@autoinject
export class AuthenticationService {
constructor(private readonly authService: AuthService) { }
etc
}
authService
is injected into all the classes that require it, e.g.
import { HttpClient } from "aurelia-fetch-client";
import { AuthenticationService } from "./authentication-service";
@autoinject
export class DictionaryService extends BaseFetchService<IDictionary> implements IBaseFetchService<IDictionary, IListItem> {
constructor(
http: HttpClient,
authService: AuthenticationService,
) {
super("api/dictionary", http, authService);
}
etc
My jest tests look like
import { HttpClient } from "aurelia-fetch-client";
import { Container } from "aurelia-framework";
import { CostingsService } from "../../src/services/costings-service";
describe("CostingsService", () => {
let auContainer: Container;
let sut: CostingsService;
beforeEach(() => {
auContainer = new Container();
auContainer.registerInstance(HttpClient);
sut = auContainer.get(CostingsService);
});
While the application works perfectly, all my tests now fail with:
Error: Error invoking AuthService. Check the inner error for details.
------------------------------------------------
Inner Error:
Message: Cannot read property 'getItem' of undefined
Inner Error Stack:
TypeError: Cannot read property 'getItem' of undefined
at Storage.get (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-authentication\dist\commonjs\aurelia-authentication.js:549:61)
at new AuthService (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-authentication\dist\commonjs\aurelia-authentication.js:1196:43)
at Object.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:473:12)
at InvocationHandler.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:419:166)
at Container.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:690:23)
at StrategyResolver.get (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:52:36)
at Container.get (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:617:39)
at Object.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:467:31)
at InvocationHandler.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:419:166)
at Container.invoke (C:\Visual Studio\FactoryCashFlow\FactoryCashFlow\node_modules\aurelia-dependency-injection\dist\commonjs\aurelia-dependency-injection.js:690:23)
End Inner Error Stack
------------------------------------------------
I’ve tried auContainer.registerInstance(AuthService)
but that doesn’t work.
Clearly I still haven’t quite grapsed how DI works with aurelia!