Closed JoshuaKGoldberg closed 1 month ago
Thanks for the reproduction. It looks like this is because "then"
property access is send to not-a-log's proxy due to interop.
https://github.com/vitest-dev/vitest/blob/5d6d8013371b46522ff55cb64015d29e617994f2/packages/vite-node/src/client.ts#L547-L553
First of all, "then"
access shouldn't happen for static import, but Vitest (Vite SSR) rewrite's all imports as dynamic import internally, so that part cannot be probably fixed (cf. https://github.com/vitest-dev/vitest/issues/5122).
Technically, the same error can happen in innocent-looking async/await code outside of Vitest, so maybe not-a-log
should be robust about this use case. For example, a following code causes same error on node:
// node repro.mjs
async function main() {
// "await" implicitly checks `mod.default.then`
await import("not-a-log").then(mod => mod.default);
}
main();
// or maybe this looks more innocent
async function main2() {
const { default: logger } = await import("not-a-log");
return logger;
}
// "await" implicitly checks `logger.then`
const logger = await main2();
One way to save error this on Vitest is to fix this TODO
or you can try disabling this "interop" entirely by test.deps.interopDefault: false
, which would preserve node import semantics.
export default defineConfig({
test: {
deps: {
interopDefault: false,
}
},
})
Thanks for the deep dive & explanation! I filed an issue upstream: https://github.com/jimmywarting/not-a-log/issues/2.
I am not sure if this is a bug in Vitest. I don't think it's possible to support this case because then
on an object is always called even if you return then
from a module:
// mod.ts
export const then = () => {}
// index.ts
await import('./mod.ts')
Named export of then
would be impossible (which is the case of actor library in https://github.com/vitest-dev/vitest/issues/5122), but this issue is slightly different since Vitest is accessing then
on default export object due to cjs interop.
Since not-a-log
has type: "module"
, cjs interop should not be necessary, but Vitest does it currently due to this heuristics:
actor
library crashes on Vite SSR, but not-a-log
works on Vite SSR.
I'm closing this for the same reason as https://github.com/vitest-dev/vitest/issues/5122 and upstream https://github.com/vitejs/vite/issues/18328
Describe the bug
When
import
ing from an installed npm package such asnot-a-log
that uses aProxy
, Vitest crashes with:Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-keoxtv
I also set up a full reproduction here: https://github.com/JoshuaKGoldberg/repros/tree/vitest-proxy-not-a-log
System Info
Used Package Manager
npm
Validations