如何编写NUXT 3中使用Vitest的组件的单元测试?
我试图从VUE 3迁移到 nuxt 3 。我使用在我的 vue 应用中使用 的vitest编写了单元测试,但是NUXT应用中的同一测试给我以下错误:
错误:由于内容包含无效的JS语法而无法解析导入分析。
安装 @vitejs/plugin-vue处理.vue文件。
我已经安装了@vitejs/plugin-vue
作为开发依赖性,但什么也没有发生。
这是我的测试文件的一个示例:
import { describe, it, expect } from "vitest";
import { mount } from "@vue/test-utils";
import AtomsButton from "./AtomsButton.vue";
describe("AtomsButton", () => {
it("button renders properly", () => {
const wrapper = mount(AtomsButton, { slots: { default: "Button" } });
expect(wrapper.html()).toContain("Button");
});
});
这是我的package.json
文件:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"test:unit": "vitest --environment jsdom"
},
"devDependencies": {
"@nuxt/test-utils-edge": "^3.0.0-rc.3-27571095.9379606",
"@vitejs/plugin-vue": "^2.3.3",
"@vue/test-utils": "^2.0.0",
"jsdom": "^19.0.0",
"nuxt": "3.0.0-rc.3",
"vitest": "^0.13.1"
}
}
我不知道我在做什么错。任何帮助将不胜感激。
I'm trying to migrate from Vue 3 to Nuxt 3. I've written unit tests for my components using vitest which are working fine in my Vue app, but the same test in the Nuxt app give me the following error:
Error: Failed to parse source for import analysis because the content contains invalid JS syntax.
Install @vitejs/plugin-vue to handle .vue files.
I've installed @vitejs/plugin-vue
as a development dependency but nothing happened.
Here is an example of my test files:
import { describe, it, expect } from "vitest";
import { mount } from "@vue/test-utils";
import AtomsButton from "./AtomsButton.vue";
describe("AtomsButton", () => {
it("button renders properly", () => {
const wrapper = mount(AtomsButton, { slots: { default: "Button" } });
expect(wrapper.html()).toContain("Button");
});
});
Here is my package.json
file:
{
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview",
"test:unit": "vitest --environment jsdom"
},
"devDependencies": {
"@nuxt/test-utils-edge": "^3.0.0-rc.3-27571095.9379606",
"@vitejs/plugin-vue": "^2.3.3",
"@vue/test-utils": "^2.0.0",
"jsdom": "^19.0.0",
"nuxt": "3.0.0-rc.3",
"vitest": "^0.13.1"
}
}
I have no idea what am I doing wrong. Any help would be appreciated.
Here is the reproduction link
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也很挣扎,并且能够通过仅使用自定义的Vite配置文件来使其正常工作。
package.json
文件:vitest.config.js
文件:I was struggling with this too and was able to make it work by using a custom Vite config file only for Vitest.
package.json
file :vitest.config.js
file :