Comments (6)
Thanks you so much @sheremet-va, you are doing an awesome job!
from vitest.
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.
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.
I'm attempting to come up with a fix (if feasible).
from vitest.
Fix sent: tinylibs/tinyspy#45
from vitest.
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)
- `renderHook` import auto detect does not work in browser mode HOT 1
- cannot mock spyOn namespace import in browser mode HOT 1
- Ability to mimic spyOn default behavior with module mock HOT 1
- @vitest/browser fails in Yarn PnP environment due to missing Vite dependency HOT 3
- `--inspect-brk` runs immediately when the debugger attaches HOT 8
- `Intl.RelativeTimeFormat` and `Intl.ListFormat` can't be spied upon HOT 4
- module mock failure on react-router-dom in browser mode HOT 2
- Snapshots modify input by unexpectedly trimming and adjusting indentation
- Unable to npm install @vitest/ui HOT 1
- stack trace incorrect for unhandled errors during test run in browser mode
- expect(...).toMatchSnapshot has typescript error since v2 of vitest HOT 2
- "TypeError: Cannot create proxy with a non-object as target or handler" when importing node module with a Proxy
- stack trace incorrect when react component throws with istanbul coverage HOT 1
- `@vitest/web-worker` didn't transform `.wasm?url` in worker import HOT 11
- Custom equality testers are not properly relayed to typed arrays HOT 2
- correct type for `import { defineConfig } from 'vitest/config';` with `"type": "commonjs"` HOT 1
- Pass down context to `onTestFinished` and `onTestFailed` instead of test result
- Some coverage.exclude glob patterns not matching for vitest 2 workspace HOT 4
- TypeError: Unknown file extension ".css"
- `tsc` with `"vitest"` version 2 requires `"@types/node"` to run without errors HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vitest.