repos / starfx

supercharged async flow control library.
git clone https://github.com/neurosnap/starfx.git

starfx / test
Eric Bower · 18 Jan 24

safe.test.ts

 1import { describe, expect, it } from "../test.ts";
 2import { call, run } from "../mod.ts";
 3
 4const tests = describe("call()");
 5
 6it(tests, "should call the generator function", async () => {
 7  function* me() {
 8    return "valid";
 9  }
10
11  await run(function* () {
12    const result = yield* call(me);
13    expect(result).toBe("valid");
14  });
15});
16
17it(tests, "should return an Err()", async () => {
18  const err = new Error("bang!");
19  function* me() {
20    throw err;
21  }
22
23  await run(function* () {
24    try {
25      yield* call(me);
26    } catch (err) {
27      expect(err).toEqual(err);
28    }
29  });
30});
31
32it(tests, "should call a promise", async () => {
33  const me = () =>
34    new Promise<string>((resolve) => {
35      setTimeout(() => {
36        resolve("valid");
37      }, 10);
38    });
39
40  await run(function* () {
41    const result = yield* call(me);
42    expect(result).toEqual("valid");
43  });
44});