Simulate error handling in NgRX createEffect function

I am currently in the process of creating a test scenario for a dispatch event. Below is the code snippet I am working on:

@Injectable()
export class AuthEffects {
  signIn$ = createEffect(() =>
    this.actions$.pipe(
      ofType(AuthActions.signIn),
      switchMap((action) =>
        from(this.loginService.signIn(action.email, action.password, action.redirectTo)).pipe(
          map(() => AuthActions.signInSuccess()),
          catchError((err: Error) =>
            of(
              AuthActions.signInError({
                message: err.message,
              })
            )
          )
        )
      )
    )
  );

  constructor(
    private actions$: Actions,
    private loginService: LoginService
  ) {}
}

Below is the outline for the test:

describe('AuthEffects', () => {
  let effects: AuthEffects;
  let actions$: Observable<Action>;
  let mockLoginService: LoginService;
  let mockSupabaseService: SupabaseService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [provideMockActions(() => actions$), AuthEffects],
    });
    effects = TestBed.inject(AuthEffects);
    mockLoginService = TestBed.inject(LoginService);
    mockSupabaseService = TestBed.inject(SupabaseService);
  });
  describe('signIn$', () => {
     // Test scenarios go here
  });
});

I am encountering difficulties with simulating the output needed to achieve full coverage on the catchError method. Whenever I execute the test, the error below crops up:

console.error
Unhandled Promise rejection: { name: 'test', message: 'test' } ; Zone: ProxyZone ; Task: null ; Value: { name: 'test', message: 'test' } undefined

How can I resolve this issue?

Answer №1

When setting up your signIn mock, utilize the rxjs throwError operator like so:

jest.spyOn(mockSupabaseService, 'signIn').mockImplementation(() => throwError(mockError));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on resolving the 404 path error in Angular2/TypeScript ASP.NET 4.6.1 on Visual Studio 2015

I'm facing a challenge while developing a new application using TypeScript, Angular2, and ASP.NET 4.6.1 on VS2015. Two issues have come up in the process. First problem: I keep encountering 404 errors with the include files in my index.html file. Upo ...

What is the best way to create a type guard for a path containing a dynamic field

In certain scenarios, my field can potentially contain both a schema and an object where this schema is defined by key. How can a guard effectively tackle this issue? Below is an example of the code: import * as z from 'zod'; import type { ZodTy ...

Ensure to verify the values of two variables when using a switch case statement

I'm working with two variables that can return true or false. My goal is to display a corresponding text message for each variable if it returns false. How can I effectively handle the ValidCheked and repeatChecked variables in a switch statement? ...

What is the process for linking my Next.js application with MongoDB Compass?

Currently, I am working on a project in Next.js called NetMapper where I am developing a web interface for the CLI tool nmap. My main focus right now is creating Sign In/Sign Up forms and storing user information in MongoDB Compass. Despite trying various ...

How to trigger a click event on a div and effectively filter Vuex data in Vue.js?

Currently facing a challenge with exporting the filter function and utilizing it in a vuex store. Everything was smooth sailing until this point. Now, I'm attempting to add an @click event to divs. When I click on a particular brand, such as "Audi," I ...

What is the best approach for managing interactive infographics using JavaScript?

I've been tasked with a project that involves creating a dynamic data visualization using JavaScript. You can check out the final product here. The goal is to take information from a provided PDF and find a way to display it dynamically on an image. ...

Resetting TransferState data in Angular upon transitioning between routes

Currently, I am implementing Angular with server-side rendering and utilizing TransferState to pass http data from the server to the browser. Below is the snippet of my code: getProducts() { let products = this.tstate.get(PRODUCT_KEY, null as any); ...

Utilize Angular's *ngFor to showcase distinct "category" values along with the lowest price for each individual category

I have an array containing objects with properties such as "category" and "price" in my Angular application. My goal is to display only unique values of the "category" property (for example: Economy, Premium, Deluxe) along with the lowest price within each ...

JSP page displaying a table with a text input field named "code" enclosed within <TD> tags

Recently, I designed a JSP page that features a table with two columns: Code and Description. The table data is an input type of "text" with the name attribute set to "code". The main functionality I need to implement is the automatic generation of the d ...

The React JS textarea's onchange event is not functioning properly when trying to set the value

This is a class component I created for an auto-suggest text area similar to Twitter's feature. import React, { Component, createRef } from 'react'; import { TextArea, List, Triangle } from './Styled'; import ge ...

Creating an HTTP interceptor in Backbone: A step-by-step guide

After gaining experience with backbone, I decided to delve into learning angular. The features of angular really caught my interest, especially the HTTP interceptor. I began thinking about how to implement this functionality in backbone to display a spin ...

Leveraging the "dialogclose" event within jQuery Mobile

Is there a way to change an image once it is clicked and change back when the dialog it links to is closed? I found a potential solution on this site, but after modifying it for my needs, it doesn't seem to work. Can anyone help me figure out what I a ...

What is the best way to extract the property name from the AJV output in order to effectively translate validation errors into user-friendly

I am currently utilizing the AJV library for input validation in my nodejs express api. I'm facing an issue with extracting the property name associated with each error object within the returned array. [{ instancePath: '', schemaPath: & ...

What causes the discrepancy in results between Javascript sha1 and PHP5 sha1 when generating hashes for utf-8 strings?

I'm facing an issue with utf-8 characters in a string. The PHP sha1 and Javascript sha1 are generating different codes for the same string "abc艾". Can anyone assist me with this? Thank you in advance. PHP code: $str = "abc艾"; $result = sha1($st ...

Angular's FormGroup for reactive forms is a powerful feature that allows for

Why am I unable to type in the input field before setting a value? html <form action="" [formGroup]="titleForm"> <input class="note-title" type="text" formControlName="title"> </form> ...

Vue is now no longer displaying errors in the console

Something strange is happening while I'm debugging a Vue/Nuxt app. Suddenly, the errors in the console have disappeared whenever my Javascript references a function or variable that doesn't exist. Instead of showing an error, it just fails silent ...

Enhance tick labels in C3.js with supplementary information

I need a specific format for the tick: tick: { fit: true, multiline: false, outer: false, format: function (x) { var value = this.api.ca ...

Troubleshooting asynchronous functions in Ionic3 when using Firebase Storage and Firestore

Attempting to retrieve the downloadURL from an uploaded image. The uploadImage function is used to upload the image to Firebase Storage. uploadImage() { this.image = 'movie-' + new Date().getTime() + '.jpg'; let storageRef: any, ...

Ways to verify whether a callback function supplied as a parameter in Javascript has any arguments

My task involves implementing the following: doSomething .then(success) .catch(failure); The success and failure functions are callbacks that will receive their value at runtime (I am developing a module). Therefore, I need to ensure that the fa ...

Navigating efficiently with AngularJS directives

Hello, I am currently searching for a solution to dynamically modify the navigation links within the navigation bar based on whether a user is logged in or not. Of course, a logged-in user should have access to more pages. I have developed a UserService t ...