Giter VIP home page Giter VIP logo

Comments (6)

dubzzz avatar dubzzz commented on July 17, 2024 1

Thanks you so much @sheremet-va, you are doing an awesome job!

from vitest.

dubzzz avatar dubzzz commented on July 17, 2024

The source of the issue seems to be in tinyspy:

function I(e) {
  R(u("function", e) || u("undefined", e), "cannot spy on a non-function value");
  let t = function(...s) {
    let r = A(t);
    r.called = !0, r.callCount++, r.calls.push(s);
    let m = r.next.shift();
    if (m) {
      r.results.push(m);
      let [l, o] = m;
      if (l === "ok")
        return o;
      throw o;
    }
    let p, d = "ok";
    if (r.impl)
      try {
        new.target ? p = Reflect.construct(r.impl, s, new.target) : p = r.impl.apply(this, s), d = "ok";
        //                                                          ^ It calls my function and puts its
        //                                                            result in p without any extra wrapping 
      } catch (l) {
        throw p = l, d = "error", r.results.push([d, l]), l;
      }
    let a = [d, p];
    if (b(p)) {
      let l = p.then((o) => a[1] = o).catch((o) => {
        throw a[0] = "error", a[1] = o, o;
      });
      Object.assign(l, p), p = l;
      //                   ^ It assigns l to p, l being p with extra .then.catch
    }
    return r.results.push(a), p;
  };
  i(t, "_isMockFunction", !0), i(t, "length", e ? e.length : 0), i(t, "name", e && e.name || "spy");
  let n = A(t);
  return n.reset(), n.impl = e, t;
}

See my comments in the minified code

from vitest.

dubzzz avatar dubzzz commented on July 17, 2024

Without the minified code, the source of the issue is the rewrapping of the Promise done by:

    if (isPromise(result)) {
      const newPromise = result
        .then((r: any) => (resultTuple[1] = r))
        .catch((e: any) => {
          // @ts-expect-error TS for some reasons narrows down the type
          resultTuple[0] = 'error'
          resultTuple[1] = e
          throw e
        })
      // we need to reassign it because if it fails, the suite will fail
      // see `async error` test in `test/index.test.ts`
      Object.assign(newPromise, result)
      result = newPromise
    }

Result is not anymore the original result but a value derived from it and paying the cost of an extra .then. This extra .then is the reason why the code with vi.fn() does not behave the same way.

from vitest.

dubzzz avatar dubzzz commented on July 17, 2024

I'm attempting to come up with a fix (if feasible).

from vitest.

dubzzz avatar dubzzz commented on July 17, 2024

Fix sent: tinylibs/tinyspy#45

from vitest.

dubzzz avatar dubzzz commented on July 17, 2024

Is it worth adding a dedicated test on Vitest's side once the fix gets released on tinyspy? If so I can work on adding such test

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.