记录日期2022.11.11
项目基于vitesse-nuxt3
package.json
、nuxt.config.ts
、vitest.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