Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestScheduler and timer, test never ends. #3957

Closed
pvinis opened this issue Jul 26, 2018 · 6 comments
Closed

TestScheduler and timer, test never ends. #3957

pvinis opened this issue Jul 26, 2018 · 6 comments

Comments

@pvinis
Copy link

pvinis commented Jul 26, 2018

Bug Report

Current Behavior
i have a timer and some other streams that touch this timer.

Reproduction
i dont know how to do stackblitz with jest tests.
you can take the following code and run jest test, or yarn test.

 it('do some testing', () => {
    const testScheduler = new TestScheduler((actual, expected) => expect(actual).toEqual(expected))
    testScheduler.run(helpers => {
      const { cold, expectObservable } = helpers

      const tick = timer(0, 1000, testScheduler).pipe(
        // take(18),
        share(),
      )
      const now = tick
      const bla = cold('---a-----b')
      const blu = bla.pipe(
        withLatestFrom(now),
        map(([b, n]) => b)
      )
      const output = blu
      const expected = '---a-----b'
      expectObservable(output).toBe(expected)
    })
  })

Expected behavior
I want to test the above code, and i would expect that after i have gotten the expected output up to the point i have it, then it would stop the test as a success. with the current test above, the test never finishes, because i guess the timer never stops?
if i uncomment the take, then the test passes.

Environment

  • Runtime: node 8, react native 0.55
  • RxJS version: 6.2

Possible Solution
N/A

Additional context/Screenshots
N/A

@cartant
Copy link
Collaborator

cartant commented Jul 26, 2018

IMO, this is not a bug. Without the take, there is nothing to stop the timer. The bla source does not complete, so the test infrastructure remains subscribed to it and to the timer. The timer keeps emitting notifications, so, as far as the testing infrastructure is concerned, the test still appears to be running. Forever.

@pvinis
Copy link
Author

pvinis commented Jul 26, 2018

but the idea of the marble tests is that I say the expected output and check it. after the timeframe of the expectation, there is no need for the scheduler to keep running. so it could stop everything, right?

@cartant
Copy link
Collaborator

cartant commented Jul 26, 2018

The test scheduler will see that the output matches the expectation, but it also has an observable that's still emitting. It cannot know that it won't receive more notifications, upon receipt of which the received output would not match the expected output.

If your source observable completed - i.e. ended with| - the test would behave as you're expecting it to.

@pvinis
Copy link
Author

pvinis commented Jul 27, 2018

hm ok. so the expected should end with | and bla should also end with |. I will try that in a few hours and report back. is there another way I could make this happen as I want? maybe a takeuntil with some kind end observable? probably no..

@cartant
Copy link
Collaborator

cartant commented Jul 27, 2018

Anything that stops the timer should work - like the take call you commented out.

Basically, you cannot leave an observable emitting and expect the test to end. Observables don't necessarily have to complete, but each time the timer emits, it schedules its next emission and that's the problem. The test infrastructure won't end the test until there are no more scheduled actions.

@pvinis
Copy link
Author

pvinis commented Jul 27, 2018

indeed adding | did allow the test to finish. thank you. i dont know why i didnt think of trying that..

@pvinis pvinis closed this as completed Jul 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Aug 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants