kefirjs / kefir-test-utils

Framework-agnostic testing tools for Kefir
MIT License
0 stars 1 forks source link

Testing fromPromise fails #574

Closed fernandogmar closed 1 year ago

fernandogmar commented 1 year ago

Hi!

just trying to test a stream that contains a fromPromise call, it does not give me the expected results. But if we would replace it with constant it works fine. Below an example:

const events = {
  a: value(1),
  b: value(2),
  c: end()
};
           withFakeTime((tick) => {
            const obs = stream();
            const out_1 = obs.flatMap( 
                (v) => Kefir.fromPromise(Promise.resolve(v+1))
            );
            const out_2 = obs.map(v => 0);
            const out = Kefir.merge([out_2, out_1]);
            const { log } = watchWithTime(out);
            sendFrames(obs, {
                frames: parseDiagram('abc', events),
                advance: () => tick(200),
            });
            out.onEnd(() => {
                t.deepEqual(log, [
                    [0, value(0)],
                    [0, value(1+1)],
                    [200, value(0)],
                    [200, value(2+1)],
                    [400, end()],
                ]);
            });
        });
mAAdhaTTah commented 1 year ago

Promises are async always so you're going to get different results compared to constant which is sync.

fernandogmar commented 1 year ago

Thanks! then I am missing an async version for testing streams with promises 😅 ...anyway, I have already got a different approach using directly sinon. Thanks again!

mAAdhaTTah commented 1 year ago

No problem, I don't think the built in test tooling handles actual async particularly well. It sort of pretends to handle async with the parseDiagram and underlying clock mocking but if you need to use an actual promise, you'll need to deal with that in the test.