You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I originally opened this issue in the Vitest repo but was told to come here.
I have an external package called @hex-insights/log, when I import this package and try to instantiate any class in my app using Vitest I encounter the error (works fine with just Vite for dev server or build):
FAIL src/Components/GlobalErrorBoundary/test.tsx > GlobalErrorBoundary > Logger Test > should instantiate Logger
TypeError: Cannot call a class as a function
❯ d ../../node_modules/@hex-insights/log/dist/index.js:2:7764
❯ new t ../../node_modules/@hex-insights/log/dist/index.js:2:14712
❯ src/Components/GlobalErrorBoundary/test.tsx:44:21
Here is the source from the log package:
export class ASmallClass {
private secret = "I am a secret";
}
That's a lot of minified code, so here are the important parts:
I'm dealing with the ASmallClass class, which is defined as tt and looks like this originally:
var tt=w((function t(){d(this,t),this.secret="I am a secret"}))
but from the vitest transformed version looks like this:
var tt=w((function t(){d(this,__vite_ssr_import_0__.getObjectKeys),this.secret="I am a secret"}))
Where d (the thrower of the error) is:
function d(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}
Now, at the top of the original file I have:
import{getObjectKeys as t, ...}
So the issue is that t in the d call is getting replaced with the imported t, which then causes d to the throw the error. My guess is that vitest is incorrectly replacing all t variables with __vite_ssr_import_0__.getObjectKeys even though in this case t should clearly be the locally defined t function.
How can I fix this issue? Is it possible to disable the module transform? Is it possible to have the module transform work correctly?
Reproduction
It's hard to provide a reproduction because these packages are private and owned by my company.
Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
Describe the bug
I originally opened this issue in the Vitest repo but was told to come here.
I have an external package called
@hex-insights/log
, when I import this package and try to instantiate any class in my app using Vitest I encounter the error (works fine with just Vite for dev server or build):Here is the source from the log package:
and in my test I have:
Here is my
vitest.config.ts
file:I've done some digging and found the issue has to do with the way vitest transforms modules.
Here is the original package from
node_modules
:And here is the version that vitest uses (received from the output of
test.server.debug.dumpModules: true
:That's a lot of minified code, so here are the important parts:
I'm dealing with the
ASmallClass
class, which is defined astt
and looks like this originally:but from the vitest transformed version looks like this:
Where
d
(the thrower of the error) is:Now, at the top of the original file I have:
So the issue is that
t
in thed
call is getting replaced with the importedt
, which then causesd
to the throw the error. My guess is that vitest is incorrectly replacing allt
variables with__vite_ssr_import_0__.getObjectKeys
even though in this caset
should clearly be the locally definedt
function.How can I fix this issue? Is it possible to disable the module transform? Is it possible to have the module transform work correctly?
Reproduction
It's hard to provide a reproduction because these packages are private and owned by my company.
Steps to reproduce
No response
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: