You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Importing images used the 'import' keyword for the image 'src' breaks jest test #26749
import Home from '../pages/index'
import { render } from '@testing-library/react'
// `describe` is not required, but it helps the tests read nicely
describe('The Home Page Component', () => {
// Each test for the component will get an `it` block
it('should have exactly 1 `main` section', () => {
// The getByRole will error if there are less or more than 1 element found
const { getByRole } = render(<Home />)
const main = getByRole('main')
expect(main).toBeInTheDocument()
})
})
file jest.config.js
module.exports = {
// Automatically clear mock calls and instances between every test
clearMocks: true,
// The directory where Jest should output its coverage files
coverageDirectory: ".coverage",
moduleNameMapper: {
"\\.(css|less|scss|sss|styl)$": "<rootDir>/node_modules/jest-css-modules",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ['./jest.setup.js'],
testPathIgnorePatterns: [
"<rootDir>/.next/",
"<rootDir>/node_modules/"
],
transform: {
"^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
// transformer for svg files
"^.+\\.svg$": "<rootDir>/svgTransform.js"
}
}
Reacted by Ellie Farrer, tatisaucedo, Dan Page, yesimm, Timothy Johnson, Lu, Jun, Lucas Caton, Faouzi Mohamed, Patrik Affentranger, Kirill Merkushev and 8 more
Reacted by Lucas Caton, jdq, Laszlo Horvath, Helena Natanael, Diego Feder and Tiago Celestino
Reacted by Lucas Caton, Faouzi Mohamed, Lucas Lima, Salah Akil, Kirill Merkushev, Helena Natanael, Diego Feder, Tiago Celestino and krendeleno
Reacted by Lucas Caton, Helena Natanael, Diego Feder and Tiago Celestino
I feel it's kinda related to my case,
I'm getting Error: Uncaught [Error: An object should only be passed to the image component src parameter if it comes from a static image import. It must include src. Received "default"] in my tests, when I statically import image like
importlogofrom"public/logo.png";
and later use it
<Imagesrc={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 <img> element
If you'd like to create a manual mock for this (also did the same for next/head), create the following file: __mocks__/next/image.tsx
And in it:
import * as React from 'react'
import { ImageProps } from 'next/image'
const mock = (props: ImageProps): React.ReactElement => {
// in my case, I was only using these props, but you may need to filter out more if you are using more props
// that shouldn't be rested onto an <img /> element
const { objectFit, objectPosition, src, ...filteredProps } = props
return <img src={src.toString()} {...filteredProps} />
}
export default mock
Reacted by Alex, Duncan-Brain, Michael Nahkies, Nguyễn Vũ Quang, Bruce Harrison, Taiwo Ogunola, Gabriel Tavares, Kirill Merkushev and mkondel
Please verify that your issue can be recreated with next@canary.
Why was this issue marked with the please verify canary label?
We noticed the provided reproduction was using an older version of Next.js, instead of canary.
The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.
If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.
How can I quickly verify if my issue has been fixed in canary?
The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases.
My issue has been open for a long time, why do I need to verify canary now?
Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.
What happens if I don't verify against the canary version of Next.js?
An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.
If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.
I did not open this issue, but it is relevant to me, what can I do to help?
Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.
I think my reproduction is good enough, why aren't you looking into it quicker?
We look into every Next.js issue and constantly monitor open issues for new comments.
However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.
Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.
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.
Activity
alexsabdev commentedon Jul 23, 2021
As a workaround, try to mock the component in
jest.setup.js:vx17rt commentedon Aug 4, 2021
I feel it's kinda related to my case,
I'm getting
Error: Uncaught [Error: An object should only be passed to the image component src parameter if it comes from a static image import. It must include src. Received "default"]in my tests, when I statically import image likeand later use it
Mocking the whole 'next/image' module helps, though
thepuzzlemaster commentedon Oct 13, 2021
Updated my sample with a new version that actually renders an
<img>elementIf you'd like to create a manual mock for this (also did the same for next/head), create the following file:
__mocks__/next/image.tsxAnd in it:
Update testing based on Failed to parse src "test-file-stub" on `next… (
github-actions commentedon Dec 1, 2022
Please verify that your issue can be recreated with
next@canary.Why was this issue marked with the
please verify canarylabel?We noticed the provided reproduction was using an older version of Next.js, instead of
canary.The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running
npm install next@canaryand test it in your project, using your reproduction steps.If the issue does not reproduce with the
canaryversion, then it has already been fixed and this issue can be closed.How can I quickly verify if my issue has been fixed in
canary?The safest way is to install
next@canaryin your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases.My issue has been open for a long time, why do I need to verify
canarynow?Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.
What happens if I don't verify against the canary version of Next.js?
An issue with the
please verify canarythat receives no meaningful activity (e.g. new comments that acknowledge verification againstcanary) will be automatically closed and locked after 30 days.If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using
next@canary.I did not open this issue, but it is relevant to me, what can I do to help?
Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the 👍 reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.
I think my reproduction is good enough, why aren't you looking into it quicker?
We look into every Next.js issue and constantly monitor open issues for new comments.
However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.
Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.
Useful Resources
balazsorban44 commentedon Jan 10, 2023
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.
github-actions commentedon Feb 10, 2023
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.