最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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 !

发布评论

评论列表(0)

  1. 暂无评论