-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Importing images used the 'import' keyword for the image 'src' breaks jest test #26749
Comments
As a workaround, try to mock the component in
|
I feel it's kinda related to my case, import logo from "public/logo.png"; and later use it <Image src={logo} height={24} width={24} alt="logo" /> Mocking the whole 'next/image' module helps, though |
Updated my sample with a new version that actually renders an If you'd like to create a manual mock for this (also did the same for next/head), create the following file: And in it:
|
#29039) …/image` Error While learning how to test next.js applications I came across this error when testing components using next/image with an image import eg: ``` // /quiz-hero import Image from 'next/image'; import quizImage from '../../public/undraw-quiz.svg'; // In render <Image height='91' width='198' layout='fixed' src={quizImage} alt='QuizImage' /> ``` ### Error -> Failed to parse src "test-file-stub" on `next/image`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://) This is fixed by adding a "/" to the beginning of your file-stub module export string. This is not an error when you're awaiting an image using async waitFor, But comes up as an error when you're testing a component that holds another component with a "next/image" import. eg: ``` // quizHero.test.tsx works like this without change. const image = await waitFor(() => screen.findByAltText('QuizImage')); // but index.test.tsx fails rendering the homepage without the change. render(<Home />); // Error: Failed to parse src "test-file-stub" on `next/image`... // with change to __mocks__/fileMock quizHero.test.tsx //test pass index.test.tsx //test pass ``` <!-- Thanks for opening a PR! Your contribution is much appreciated. In order to make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Bug possibly related to -> #26749 ## Feature - [X] Documentation added ## Documentation / Examples - [X] Make sure the linting passes
vercel#29039) …/image` Error While learning how to test next.js applications I came across this error when testing components using next/image with an image import eg: ``` // /quiz-hero import Image from 'next/image'; import quizImage from '../../public/undraw-quiz.svg'; // In render <Image height='91' width='198' layout='fixed' src={quizImage} alt='QuizImage' /> ``` ### Error -> Failed to parse src "test-file-stub" on `next/image`, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://) This is fixed by adding a "/" to the beginning of your file-stub module export string. This is not an error when you're awaiting an image using async waitFor, But comes up as an error when you're testing a component that holds another component with a "next/image" import. eg: ``` // quizHero.test.tsx works like this without change. const image = await waitFor(() => screen.findByAltText('QuizImage')); // but index.test.tsx fails rendering the homepage without the change. render(<Home />); // Error: Failed to parse src "test-file-stub" on `next/image`... // with change to __mocks__/fileMock quizHero.test.tsx //test pass index.test.tsx //test pass ``` <!-- Thanks for opening a PR! Your contribution is much appreciated. In order to make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> ## Bug possibly related to -> vercel#26749 ## Feature - [X] Documentation added ## Documentation / Examples - [X] Make sure the linting passes
Please verify that your issue can be recreated with Why was this issue marked with the
|
This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
11.0.1
What version of Node.js are you using?
16.3.0
What browser are you using?
Safari
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
Error message when simple jest test running: "Image with src "placeholder-file" must use "width" and "height" properties or "layout='fill'" property."
Expected Behavior
The jest test should pass because 'width' and 'height' for static images can be automatically defined in Next.js 11.
To Reproduce
file index.js
file fileMock.js
file HomeTest.js
file jest.config.js
The text was updated successfully, but these errors were encountered: