Skip to content

Use of throwError makes the next test fail #386

Closed
@zerock54

Description

@zerock54

Hello,

I have a component where I am using the RXJS's throwError method to re-throw an error from a catchError operator.
The test itself passes but the issue is that the error thrown by throwError makes the next test fail.
Here is a reproduction:

import {Component} from '@angular/core';
import {throwError} from 'rxjs';

@Component({
  selector: 'app-test',
  template: `<button (click)="onTest()">Test</button>`,
  styles: [],
})
export class TestComponent {
  onTest() {
    throwError(() => new Error('myerror')).subscribe();
  }
}
import {render, screen} from '@testing-library/angular';
import userEvent from '@testing-library/user-event';
import {TestComponent} from './test.component';

describe('TestComponent', () => {
  it('does not fail', async () => {
    await render(TestComponent);

    await userEvent.click(screen.getByText('Test'));
  });

  it('fails because of the previous one', async () => {
    await render(TestComponent);

    await userEvent.click(screen.getByText('Test'));
  });
});
image

How can i avoid the error to make the test fail ?
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions