首页 > 其他分享 >记录nuxt3爬坑,vitest配置,autoimport等问题

记录nuxt3爬坑,vitest配置,autoimport等问题

时间:2022-11-12 12:44:28浏览次数:81  
标签:vue vitest vueuse autoimport nuxt3 import true nuxt

记录日期2022.11.11
项目基于vitesse-nuxt3
package.jsonnuxt.config.tsvitest.config.ts 配置放在结尾

项目版本

nuxt版本:3.0.0-rc.13-27772354.a0a59e2
@nuxt/test-utils-edge版本:3.0.0-rc.13-27772354.a0a59e2

学习记录

在nuxt3中使用vitest和vueuse
vitest配置
在vitest中配置autoimport

解决的问题

  • 全局css
  • nuxt自动引入
  • Vitest was initialized with native Node instead of Vite Node. 错误
  • vitest auto import components、vue、@vueuse/core
  • test.ts 文件中自动引入 vue3 vueuse vitest
  • 在vitest 中使用 @nuxt/test-utils-edge
  • 配置 vitest 路径和自动引入的组件与 nuxt 一致
// package.json
{
  "private": true,
  "packageManager": "[email protected]",
  "scripts": {
    "build": "nuxi build",
    "dev": "nuxi dev",
    "start": "node .output/server/index.mjs",
    "lint": "eslint .",
    "test": "vitest",
    "postinstall": "nuxi prepare",
    "generate": "nuxi generate",
    "up": "taze major -I"
  },
  "devDependencies": {
    "@antfu/eslint-config": "^0.29.4",
    "@iconify-json/carbon": "^1.1.9",
    "@iconify-json/twemoji": "^1.1.5",
    "@nuxt/test-utils-edge": "3.0.0-rc.13-27772354.a0a59e2",
    "@nuxtjs/color-mode": "^3.1.8",
    "@pinia/nuxt": "^0.4.3",
    "@unocss/nuxt": "^0.46.4",
    "@vueuse/nuxt": "^9.5.0",
    "eslint": "^8.27.0",
    "jsdom": "^20.0.2",
    "nuxt": "npm:[email protected]",
    "pinia": "^2.0.23",
    "playwright": "^1.27.1",
    "sass": "^1.56.1",
    "typescript": "^4.8.4",
    "unplugin-auto-import": "^0.11.4",
    "unplugin-vue-components": "^0.22.9",
    "vite": "^3.2.3",
    "vitest": "^0.25.1",
    "vue": "^3.2.44"
  },
  "pnpm": {
    "peerDependencyRules": {
      "ignoreMissing": [
        "webpack"
      ]
    }
  }
}
// nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    '@vueuse/nuxt',
    '@unocss/nuxt',
    '@pinia/nuxt',
    '@nuxtjs/color-mode',
  ],
  alias: {
    style: '/<rootDir>/assets/style',
  },
  // nuxt auto import components
  components: {
    dirs: [
      '~/components',
      '~/layoutComponents',
      '~/pagesComponents',
    ],
  },
  // global css
  css: [
    'assets/style/root.css',
  ],

  // antfu/vitesse-nuxt3 default config
  experimental: {
    reactivityTransform: true,
  },
  unocss: {
    preflight: true,
  },
  colorMode: {
    classSuffix: '',
  },
})
// vitest.config.ts
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    // fix bug: Vitest was initialized with native Node instead of Vite Node
    // https://github.com/nuxt/framework/issues/3252#issuecomment-1126771193
    deps: {
      inline: ['@nuxt/test-utils-edge'],
    },
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, '.'),
    },
  },
  plugins: [
    // auto import vue、vueuse
    AutoImport({
      imports: ['vue', '@vueuse/core'],
      vueTemplate: true,
      dts: true, // not matter
    }),
    // auto import vue component
    Components({
      dirs: [
        './components',
        './layoutComponents',
        './pagesComponents',
      ],
    }),
  ],
})
// tsconfig.json
{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "strict": true,
    "types": ["vitest/globals"]
  }
}

标签:vue,vitest,vueuse,autoimport,nuxt3,import,true,nuxt
From: https://www.cnblogs.com/HDWdemo/p/16883469.html

相关文章

  • nuxt3引入element-plus的三种方法
    1、全部引入安装element-plus依赖npminstallelement-plus--save在nuxt3项目中plugins下新建一个element-plus.client.ts文件 在element-plus.client.ts文件中输......
  • 生成nuxt3项目的三种方法
    方法一:npxnuxiinitnuxt-app方法二:到github的nuxt仓库下的一个叫starter的项目中:https://github.com/nuxt/starter  点击code下载,之后安装依赖方法三:去官网......
  • nuxt3中引入全局sass变量
    在nuxt3项目下的assets文件中创建一个变量文件,例如: 然后在nuxt.config.ts文件中输入exportdefaultdefineNuxtConfig({...//引入scss全局变量vite:{......
  • Nuxt3从入门到实战
    Nuxt3从入门到实战前言大家好,我是村长,欢迎关注我的公众号村长学前端。最近在与DevUI团队一起做直播,给大家分享VueDevUI开源组件库的建设,欢迎大家关注我们直播,多给项目star......
  • 【Nuxt3从入门到实战】第三啪:巧用布局模板,高效开发从这里开始!
    前言大家好,我是村长,欢迎关注我的公众号村长学前端和B站Young村长!上一篇写了nuxt3路由系统,我们试用了两个重要功能:​​动态路由​​​和​​嵌套路由​​。体验便捷的同时,当......
  • 【Nuxt3从入门到实战】第二啪:约定路由用起来可真爽啊!
    前言大家好,我是村长,欢迎关注我的公众号村长学前端。最近在与DevUI团队一起做直播,给大家分享VueDevUI开源组件库的建设,欢迎大家关注我的B站Young村长!上一讲写了最小nuxt3应......