ionicons: bug: IonIcon giving error with vitest

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

While running vitest on the components using IonIcon, the test is failing with following error though the image is rendered properly in the project Using IonIcon as: <IonIcon src="/assets/nozzle.svg"/> error

It works fine when using ion icon with with inbuilt icons provided by ionic but causes issues when we try to use any image url with it.

Expected Behavior

The IonIcon should work with vitest properly when using image url with it.

Steps to Reproduce

  1. Create any react component in ionic framework.
  2. Use IonIcon inside the component as <IonIcon src={imagepath}/> using image path as src in IonIcon.
  3. Run Vitest on the component

Code Reproduction URL

https://stackblitz.com/edit/ionic-react-vitest-example-qfvsxq?file=src%2Fcomponents%2FHeader.tsx

Run the App: npm run dev Run test suite: npm run test

Ionic Info

Ionic:

“@ionic/react”: “^7.0.10”, “@ionic/react-router”: “^7.3.1”

vite:

“vite”: “4.3.9”, “vite-plugin-dts”: “2.3.0”, “vite-plugin-eslint”: “^1.8.1”, “vite-plugin-svgr”: “^4.2.0”, “vite-tsconfig-paths”: “^4.0.2”, “vitest”: “0.32.0”

Additional Information

No response

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 15 (5 by maintainers)

Most upvoted comments

I am facing the same issue in my project: <ion-icon name="heart"></ion-icon> // Works when project runs and vitest passes as well <ion-icon src="/assets/some-icon.svg"></ion-icon> // Works when project runs, but fails in vitest : TypeError: Invalid URL

Important Update: When running the vitest in Node 16.60.0 there seems to be no issue - test runs succesfully. But when the same vitest is run in Node 18.18.0 the test fails with above issue.

There’s likely an issue with the asset path either not being defined or being defined incorrectly when loading the Ionicon. I’m going to move this over to the Ionicons repo so we can take a closer look.