sodatea / vite-jest

First-class Vite integration for Jest
MIT License
401 stars 51 forks source link

Is there any solution to use vite-jest for an application which using Vite + Vue2 + Composition API? #35

Open tomlla opened 2 years ago

tomlla commented 2 years ago

If Vite + Vue2 is not vite-jest support target, I'll close this issue.

I got this error when I run vite-jest

$ ./node_modules/.bin/vite-jest
(node:16061) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Determining test suites to run...Pre-bundling dependencies:
  @vue/composition-api
  ....
  (...and 56 more)
(this will be run only when your dependencies or config have changed)
 > node_modules/@vue/composition-api/dist/vue-composition-api.esm.js:1:16: error: Could not read from file: /home/me/vite-vu2-app/vue/dist/vue.esm-bundler.js
    1 │ import Vue from 'vue';
      ╵                 ~~~~~

Error: Build failed with 1 error:
node_modules/@vue/composition-api/dist/vue-composition-api.esm.js:1:16: error: Could not read from file: /home/me/vite-vu2-app/vue/dist/vue.esm-bundler.js
    at failureErrorWithLog (/home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:1493:15)
    at /home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:1151:28
    at runOnEndCallbacks (/home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:941:63)
    at buildResponseToResult (/home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:1149:7)
    at /home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:1258:14
    at /home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:629:9
    at handleIncomingPacket (/home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:726:9)
    at Socket.readFromStdout (/home/me/vite-vu2-app/node_modules/esbuild/lib/main.js:596:7)
    at Socket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at Socket.Readable.push (node:internal/streams/readable:228:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:199:23)

My environment

$ node --version
v16.13.0
$ jq .version node_modules/vite/package.json
"2.7.7"
$ jq .version node_modules/vue/package.json
"2.6.14"
$ jq .version node_modules/@vue/composition-api/package.json
"1.0.4"
$ grep preset jest.config.js
  preset: "vite-jest",
sodatea commented 2 years ago

The errors here are because that I added some default options to the vite server but didn't realize they are Vue-3 only.

But there are some other issues that prevent vite-jest from supporting the vue2 plugin🤔 I'll take a look.

haug1 commented 2 years ago

Would be cool to see this fixed :eyes:

nagisaando commented 2 years ago

Hi, is there any update on this?

tomdevelops commented 2 years ago

Hey @haug1 and @nagisaando, had the same problem, discovered vitest. With a little rewrite, works like a charm with vue2 + vite 🥳

nagisaando commented 2 years ago

@tomdevelops I am interested in using Vitest but it's just not production ready yet, and that bothers me a bit.😅 Hopefully they will be ready soon tho.

elambro commented 1 year ago

@tomdevelops How did you get it working? I use Laravel 9 / Vue2 / Vite. My tests w/ vitest all throw (e.g.) "captialize is not a function" for simple function import/exports. However import/export works fine with objects & constants.

export function capitalize(str) {
    return str ? str.charAt(0).toUpperCase() + str.slice(1) : str
}
import {capitalize}    from "@utils";

I also tried with jest, but I get Cannot use import.meta outside a module.