Reputation: 13
I am working on Frontend Unit Tests (Jasmine, Karma) for an angular project that has utilised a pre-built AWS angular template. I am new to the project and AWS. The app itself works without issue. Ultimately, there should be no AWS/server connection, and all services will be mocked.
The problem I am running into is inconsistent errors arising from seemingly random components when the Karma Unit Test spec is run. These components have no AWS related calls or service calls. Many of them are simple and contain mainly front-end templates (there is no back-end related logic).
Error: Uncaught (in promise): No Cognito Identity pool provided for unauthenticated access
I am still trying to wrap my head around AWS services (User pools, identity pools etc) - and have no idea why this error is occurring.
Could someone please explain what is happening or how I can go about mocking AWS entirely across the app with some sort of configuration, or create a mock user identity pool to elevate this error. I have read what feels like every webpage and am still stuck :(
Thanks in advance!
Upvotes: 0
Views: 1411
Reputation: 13
I fixed the following issue by mocking the Auth service that is called by the application. This could also be completed by adding a spy on the AWS Cognito call. I simply returned fake credentials that would indicate to the app that I was logged in, and told my TestBed module to use that instead of the real AuthService. Below is the only code I added to this mocked service class, and it has worked so far.
auth.mock-service.ts
AuthContext$ = of({
username: ''
});
This works for my frontend unit tests as there was no need to connect to AWS at all.
Upvotes: 1
Reputation: 18809
I am thinking it is happening because the AWS logic is still being ran in a service that is injected into one of the components or those functions still run.
https://testing-angular.com/debugging-tests/#debugging-tests
^ That is a very good resource in learning unit testing but there is the chapter on debugging tests.
https://testing-angular.com/faking-dependencies/#faking-dependencies
^ Here is a chapter on faking dependencies.
https://stackoverflow.com/a/62935131/7365461
^ Unfortunately, as TypeScript and Jasmine have progressed, it is difficult to mock an npm package like amazon-web-services. I have found the above answer to be the best way.
Hope all of this helps.
Upvotes: 0