5

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?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Ole
  • 41,793
  • 59
  • 191
  • 359

1 Answers1

5

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();
    });
});
Jags
  • 1,639
  • 1
  • 16
  • 30
  • Funny enough the observable he's testing is _not_ asynchronous. So this seems like a bug in Jest. – Ben Lesh Mar 18 '20 at 15:50
  • The observable itself may not be asynchronous, but the subscribe call is, and Jest completes the test prior to the expect call being reached – Jags Mar 18 '20 at 17:25