-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The package may have incorrect main/module/exports specified in its package.json. when using import #2922
Comments
You don't have "main" entry point. "module" entry point is ignored by Node.js, it is only supported by bundlers. |
If I create a UMD and add it to a main entry point for the dependency, that will allow the tests to “run” but then they don’t see the functions added by the dependency and most of the tests fail, as mentioned in my issue. |
Why do you need to create a UMD, if you already have a valid ESM entry? Did you try putting your "module" into "main"? |
I don’t. “need” to, but I tried this as an attempt to get vitest to run. It certainly changed the behavior. I did actually have vite bundle the src files as both a module and a umd (the latter for the main entry point) but this doesn’t work well… |
If you have |
Interesting, I assumed that the main had to point to a UMD or something. I have tried adding the ESM file to both main and module, and the results in vitest are the same as with a UMD — hundreds of errors. Funny thing is that the project builds just fine, but the test environment fails miserably. |
When I do have a main entry point, the failure that results looks like the following. I think maybe JSDOM can't see the functions from the import, but I don't understand why that would be.
|
If anyone stumbles onto this with a similar issue, here's how I was finally able to fix it on my end. First, I had to have a "main" line in the package.json for the dependency, otherwise vitest refused to run it and couldn't see the dependency package. Second, in the vite.config.js configuration, I had to tell vitest to inline the dependency. With that, I was essentially able to solve all these problems and get my tests working again! 🎉 test: {
environment: 'jsdom',
coverage: {
reporter: ['text', 'json', 'html'],
},
threads: false, // suppresses errors from canvas when starting tests
deps: {
inline: ['@jdlien/validator-utils'],
},
} |
I did in fact come across this and i can honestly say you are a saint and a scholar, my friend. Thank you very much. |
Describe the bug
I'm the author of the @jdlien/validator package which has a test suite using vitest.
In the initial version of validator, it had a validator-utils.ts file that had utility functions in it. When the package was structured this way, vitest worked very well. I have since extracted this library to a separate package, @jdlien/validator-utils
In its current state, if you were to clone https://github.com/jdlien/validator and install its dependencies, you can build everything just fine using vite but doing
npm run test
will fail with the errorThis implies that there's something wrong with @jdlien/validator-utils, which has a single module file it references which does indeed point to the correct file. validator-utils' package.json contains this
It most definitely contains the dist/validator-utils.js and validator-utils.d.ts files. This is clear since the project does build with vite and the resulting js file does work fine in the browser.
Unfortunately since extracting the validator-utils to another package, I am unable to run tests in vitest and I get the following:
Is this a bug in vitest? I have spent several hours trying variations of my configuration and of builds and am unable to get vitest to run with this project.
The only thing that makes a difference is if I change validator-utils to have a "main" property in its package.json, and I can compile a umd version of the library. In this case, instead of the
may have incorrect main/module/exports
error, I get hundreds of errors, one for every time the Validator class attempts to use one of the functions from validator-utils.I'd appreciate it if someone could determine whether I'm doing something wrong in either validator or validator-utils or if I have indeed stumbled upon a genuine bug in vitest.
My packages seem to be building and working just fine in actual use but it's a shame I can't run the comprehensive test suite that I've developed using vitest to verify it.
Reproduction
(
npm run build
andnpm run tw
are only necessary to use the demo page, but you can view that the package works correctly by viewing it under demo/index.html.)System Info
The text was updated successfully, but these errors were encountered: