javascript - How to test Next.js app with svg imports as component using vitest? - Stack Overflow

I use vitest to test Next.js app following the official example here.I get error saying 'InvalidC

I use vitest to test Next.js app following the official example here.

I get error saying 'InvalidCharacterError: "../../pany-logo.svg" did not match the Name production'. Finally I figured out that the image was because of svg imported as ponents. When I removed svg imports as ponents, it worked fine.

I am using @svgr/webpack to load svg as ponents in my Next.js 13 app. I tried adding vite-plugin-svgr to vitest.config.ts but it does not work.

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
import svgr from "vite-plugin-svgr";

// /config/
export default defineConfig({
  plugins: [react(), tsconfigPaths(), svgr()],
  test: {
    environment: "jsdom",
  },
});

Any way I can fix this issue?

I use vitest to test Next.js app following the official example here.

I get error saying 'InvalidCharacterError: "../../pany-logo.svg" did not match the Name production'. Finally I figured out that the image was because of svg imported as ponents. When I removed svg imports as ponents, it worked fine.

I am using @svgr/webpack to load svg as ponents in my Next.js 13 app. I tried adding vite-plugin-svgr to vitest.config.ts but it does not work.

import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
import svgr from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), tsconfigPaths(), svgr()],
  test: {
    environment: "jsdom",
  },
});

Any way I can fix this issue?

Share Improve this question asked Mar 12, 2023 at 10:06 Mayank Kumar ChaudhariMayank Kumar Chaudhari 18.9k13 gold badges72 silver badges155 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

I ran into the same issue, I also use Next13 I finally fixed it by using vite-plugin-magical-svg with a target react into my vite.config.js

plugins: [graphql(), magicalSvg({ target: 'react' })]

Hope it will wirk for you !

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745560870a4633100.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信