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

Use toEmit with async code #86

Open
DanweDE opened this issue Mar 7, 2020 · 4 comments
Open

Use toEmit with async code #86

DanweDE opened this issue Mar 7, 2020 · 4 comments

Comments

@DanweDE
Copy link

DanweDE commented Mar 7, 2020

It would be great if toEmit (i.e. createTestHelpers(Kefir).watch) would allow asserting that an observable emits, also asynchronously. For example:

const seq = Kefir.sequentially(100, [1, 2]).toProperty(() => 0)

await expect(seq).toEmit([
  value(0),
  value(1),
  value(2, { current: true }),
  end({ current: true }),
], async () => {
  await Promise((r) => setTimeout(r, 1000));
})
@mAAdhaTTah
Copy link
Contributor

mAAdhaTTah commented Mar 7, 2020

toEmitInTime is used for observables that emit values over time, but we use it with lolex to mock time. So the above example would be as follows:

const seq = Kefir.sequentially(100, [1, 2]).toProperty(() => 0)

expect(seq).toEmitInTime([
  [0, value(0, { current: true })],
  [100, value(1)],
  [200, value(2)],
  [200, end()],
], tick => {
  tick(200);
});

@DanweDE
Copy link
Author

DanweDE commented Mar 9, 2020

Yes, I am aware of that. In theory that is sufficient, in practice it might not work when testing code that also relies on promises so I still think this suggestion would make a lot of sense for real-world testing cases.
The second use-case where this would be helpful would be when in your tests you don't care about the exact time but just the order something emitted.

@mAAdhaTTah mAAdhaTTah reopened this Mar 9, 2020
@mAAdhaTTah
Copy link
Contributor

Ok, will reopen to investigate, but not sure when I'll get to it. Open to a PR though if you're interested!

@mAAdhaTTah
Copy link
Contributor

mAAdhaTTah commented Apr 17, 2020

Two things:

  1. I don't think this actually requires any changes in KTU. watch returns the log array and an unwatch function, which in toEmit is called synchronously, but it doesn't need to do that. I'm going to transfer this issue to the jest-kefir repo.
  2. Looking into this now, it appears this would be a breaking change, because expect(obs).toEmit([...]) would now need to be await'd. Instead of modifying toEmit to allow async functions, what do you think about a new matcher called toEmitAsync that expects an async function?

@mAAdhaTTah mAAdhaTTah transferred this issue from kefirjs/kefir-test-utils Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants