Jest reports that test passes even though expect assertion fails

1.9k Views Asked by At

In this code sandbox the following test is executed:

    import { of } from "rxjs";
    import { map } from "rxjs/operators";

    const source = of("World");

    test("It should fail", () => {
      source.subscribe(x => expect(x).toContain("NOTHING"));
    });

Jest reports it as passing, even though the expectation fails. Thoughts?

1

There are 1 best solutions below

2
On BEST ANSWER

rxjs observables are asynchronous. Take a look at this page to help with testing asynchronous code

You want to add the done argument as below ->

test("It should fail", done => {
  source.subscribe(
    x => {
      expect(x).toContain("NOTHING")
      done();
    });
});