簡體   English   中英

Vue3 Vite 和 jest 測試沒有模板編譯器

[英]Vue3 Vite and tests with jest has no template compiler

我使用了一個用 Vue3 Vite 制作的代碼庫,但我找不到一種方法來運行一個簡單的 Jest 測試導入組件。 這在使用 Vue-cli 創建的應用程序中運行良好,但我找不到讓 Jest 在使用 Vue3 的 Vue-Vite 應用程序中工作的方法。 這是我遇到的錯誤

 FAIL  docs/homepage.spec.js
  ● Test suite failed to run



    Vue packages version mismatch:

    - vue@3.0.0 (/var/www/html/my_project_vue3_vite/node_modules/vue/index.js)
    - vue-template-compiler@2.6.12 (/var/www/html/my_project_vue3_vite/node_modules/vue-template-compiler/package.json)

但是 vue-template-compiler 在版本 3 中還不存在,所以今天沒有辦法讓它工作。

我找到了這個示例 repo,但它使用 Mocha 和 Chai,而不是 Jest,我找不到適應它的方法。 https://github.com/JessicaSachs/vite-component-test-starter

這是我的包 json :

{
  "name": "front-operation",
  "version": "0.1.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "start": "npm run dev && wait-on http://localhost:3000",
    "test": "jest --coverage",
    "tdd:vite": "vite --config test/vite.config.test.js",
    "tdd": "aria-vue -w --path test-ui --script test/plugins.js",
    "headless": "aria-vue --offline -w -H --script test/plugins.js ",
    "test:watch": "jest --watch",
    "test:ci": "jest --ci",
    "lint": "prettier --write \"src/**/*.{js,jsx,ts,tsx,md,html,css,scss}\"",
    "e2e": "jest",
    "format:check": "prettier --list-different \"src/{app,environments,assets}/**/*{.ts,.js,.json,.css,.scss}\"",
    "format:all": "prettier --write \"src/**/*.{js,jsx,ts,tsx,md,html,css,scss}\"",
    "doc": "compodoc -p tsconfig.json src ",
    "mock:server": "json-server --port 8000 --watch ./src/mocks/db.json --routes ./src/mocks/routes.json",
    "start:proxy": "vite serve --proxy-config proxy.conf.json",
    "start:proxymock": "concurrently --kill-others \"npm run mock:server\" \"yarn start:proxy\""
  },
  "dependencies": {
    "@tailwindcss/typography": "^0.2.0",
    "@tailwindcss/ui": "^0.6.2",
    "@vue/compiler-sfc": "3.0.0",
    "axios": "^0.21.0",
    "core-js": "^3.6.5",
    "jest": "^26.6.1",
    "moment": "^2.29.1",
    "qrcode.vue": "^1.7.0",
    "tailwindcss": "^1.8.12",
    "vue": "3.0.0",
    "vue-template-compiler": "^2.6.12"
  },
  "devDependencies": {
    "@babel/preset-typescript": "^7.12.1",
    "@types/jest": "^24.0.19",
    "@types/koa-router": "^7.4.1",
    "@typescript-eslint/eslint-plugin": "^4.4.0",
    "@typescript-eslint/parser": "^4.4.0",
    "@vue/babel-preset-app": "^4.5.8",
    "@vue/eslint-config-standard": "^5.1.2",
    "@vue/eslint-config-typescript": "^7.0.0",
    "@vue/test-utils": "2.0.0-beta.5",
    "aria-mocha": "0.5.2",
    "aria-vue": "0.4.1",
    "autoprefixer": "^6.7.7",
    "babel-core": "^7.0.0-bridge.0",
    "compodoc": "0.0.41",
    "eslint": "^7.10.0",
    "eslint-plugin-vue": "^7.0.1",
    "faker": "^5.1.0",
    "husky": "^4.3.0",
    "jest": "^26.6.1",
    "jest-cli": "^26.6.1",
    "jest-config": "^26.6.1",
    "jest-css-modules": "^2.1.0",
    "jest-each": "^26.6.1",
    "jest-puppeteer": "^4.4.0",
    "jest-serializer-vue": "^2.0.2",
    "jest-vue-preprocessor": "^1.7.1",
    "json-server": "^0.16.2",
    "lint-staged": "^10.5.0",
    "prettier": "^2.1.2",
    "puppeteer": "^5.4.1",
    "ts-jest": "^26.4.3",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3",
    "vite": "^1.0.0-rc.4",
    "vue-gue": "^0.2.0",
    "vue-jest": "^3.0.7",
    "wait-on": "^5.2.0"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "src/**/*.{js,jsx,ts,tsx,md,html,css,scss}": [
      "prettier --write",
      "git add"
    ],
    "*.js": [
      "prettier --write"
    ]
  }
}

Vue 3 的包在測試階段都使用不同的名稱。 你正在尋找這個包: https : //www.npmjs.com/package/@vue/compiler-sfc

雖然@Duncan 是正確的,但vue-template-compiler現在應該是@vue/compiler-sfc這對您的情況沒有真正的幫助,因為vue-template-compilervue-jest包的依賴項。

vue-jest在第 5 版(仍處於 alpha 階段)中引入了 Vue 3 支持,您可以使用npm install --save-dev vue-jest@nextyarn add --dev vue-jest@next

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM