V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
oppddd
V2EX  ›  问与答

vitest 使用问题

  •  
  •   oppddd · 2023-08-21 17:06:34 +08:00 · 481 次点击
    这是一个创建于 461 天前的主题,其中的信息可能已经有所发展或是发生改变。

    安装依赖如下

    // package.json
    
      "scripts": {
        "dev": "vite",
        "test": "vitest",
        "build": "vue-tsc && vite build",
        "preview": "vite preview",
        "eslint": "eslint --fix",
        "lint": "npx lint-staged",
        "stylelint": "npx stylelint --fix"
      },
      "dependencies": {
        "vue": "3.3.4"
      },
     "devDependencies": {
        "@types/jsdom": "21.1.1",
        "@vitejs/plugin-vue": "4.2.3",
        "@vue/test-utils": "^2.4.1",
        "eslint": "8.47.0",
        "happy-dom": "^10.10.4",
        "less": "4.2.0",
        "lint-staged": "^14.0.0",
        "vite": "4.4.9",
        "vitest": "0.34.1",
        "vue-tsc": "1.0.9"
      },
    
    
    // vite.config.ts
    
    /// <reference types="vitest" />
    import { defineConfig } from 'vite'
    import vue from '@vitejs/plugin-vue'
    
    // https://vitejs.dev/config/
    export default defineConfig({
      plugins: [vue()],
      test: {
        globals: true,
        environment: 'happy-dom',
      },
    })
    
    

    启动之后,就会报错

    Error: Cannot find package 'happy-dom' imported from /Users/liuxsen/codes/demos/liuxsen-stack/node_modules/.pnpm/[email protected]/node_modules/local-pkg/index.mjs
    Did you mean to import [email protected]/node_modules/happy-dom/lib/index.js?
     ❯ new NodeError node:internal/errors:387:5
     ❯ packageResolve node:internal/modules/esm/resolve:852:9
     ❯ moduleResolve node:internal/modules/esm/resolve:901:20
     ❯ defaultResolve node:internal/modules/esm/resolve:1115:11
     ❯ nextResolve node:internal/modules/esm/loader:163:28
     ❯ ESMLoader.resolve node:internal/modules/esm/loader:837:30
     ❯ ESMLoader.getModuleJob node:internal/modules/esm/loader:424:18
     ❯ ESMLoader.import node:internal/modules/esm/loader:521:22
     ❯ importModuleDynamically node:internal/modules/esm/translators:110:35
     ❯ importModuleDynamicallyCallback node:internal/process/esm_loader:35:14
    
    
    2 条回复    2023-08-21 17:53:19 +08:00
    grit136907108
        1
    grit136907108  
       2023-08-21 17:18:28 +08:00
    安装 [email protected] 看看
    oppddd
        2
    oppddd  
    OP
       2023-08-21 17:53:19 +08:00
    可以了,如果使用 pnpm 需要 hoist happay-dom
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   923 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:10 · PVG 05:10 · LAX 13:10 · JFK 16:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.