diff --git a/packages/next/src/client/image-component.tsx b/packages/next/src/client/image-component.tsx index b113c0bb0c209..e04d14344f6de 100644 --- a/packages/next/src/client/image-component.tsx +++ b/packages/next/src/client/image-component.tsx @@ -320,7 +320,7 @@ function ImagePreload({ ...getDynamicProps(imgAttributes.fetchPriority), } - if (isAppRouter) { + if (isAppRouter && preload) { // See https://github.com/facebook/react/pull/26940 preload( imgAttributes.src, diff --git a/test/production/jest/next-image-preload/app/app/layout.tsx b/test/production/jest/next-image-preload/app/app/layout.tsx new file mode 100644 index 0000000000000..21b6b591198b1 --- /dev/null +++ b/test/production/jest/next-image-preload/app/app/layout.tsx @@ -0,0 +1,12 @@ +import React from 'react' +export default function RootLayout({ + children, +}: { + children: React.ReactNode +}) { + return ( + + {children} + + ) +} diff --git a/test/production/jest/next-image-preload/app/app/next.svg b/test/production/jest/next-image-preload/app/app/next.svg new file mode 100644 index 0000000000000..5174b28c565c2 --- /dev/null +++ b/test/production/jest/next-image-preload/app/app/next.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/production/jest/next-image-preload/app/app/page.tsx b/test/production/jest/next-image-preload/app/app/page.tsx new file mode 100644 index 0000000000000..fa19d2064e1a0 --- /dev/null +++ b/test/production/jest/next-image-preload/app/app/page.tsx @@ -0,0 +1,7 @@ +import React from 'react' +import Image from 'next/image' +import logo from './next.svg' + +export default function MyImage() { + return +} diff --git a/test/production/jest/next-image-preload/app/jest.config.js b/test/production/jest/next-image-preload/app/jest.config.js new file mode 100644 index 0000000000000..a52ceba313029 --- /dev/null +++ b/test/production/jest/next-image-preload/app/jest.config.js @@ -0,0 +1,16 @@ +const nextJest = require('next/jest') + +const createJestConfig = nextJest({ + // Provide the path to your Next.js app to load next.config.js and .env files in your test environment + dir: './', +}) + +// Add any custom config to be passed to Jest +const customJestConfig = { + // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work + moduleDirectories: ['node_modules', '/'], + testEnvironment: 'jest-environment-jsdom', +} + +// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async +module.exports = createJestConfig(customJestConfig) diff --git a/test/production/jest/next-image-preload/next-image-preload.test.ts b/test/production/jest/next-image-preload/next-image-preload.test.ts new file mode 100644 index 0000000000000..846ead0dd9e82 --- /dev/null +++ b/test/production/jest/next-image-preload/next-image-preload.test.ts @@ -0,0 +1,50 @@ +import { createNext, FileRef } from 'e2e-utils' +import { NextInstance } from 'test/lib/next-modes/base' +import path from 'path' +import execa from 'execa' + +const appDir = path.join(__dirname, 'app') + +describe('next/jest', () => { + let next: NextInstance + + beforeAll(async () => { + next = await createNext({ + skipStart: true, + files: { + app: new FileRef(path.join(appDir, 'app')), + [`tests/index.test.tsx`]: ` + import { render, screen } from '@testing-library/react' + import Page from '../app/page' + + it(' renders', () => { + render() + const logo = screen.getByRole('img') + expect(logo).toBeDefined() + }) + `, + 'jest.config.js': new FileRef(path.join(appDir, 'jest.config.js')), + }, + dependencies: { + jest: '29.6.2', + 'jest-environment-jsdom': '29.6.2', + '@testing-library/react': '14.0.0', + '@testing-library/jest-dom': '5.17.0', + }, + }) + }) + afterAll(() => next.destroy()) + + it('Should not throw preload is undefined error', async () => { + const { stdout, stderr } = await execa( + 'pnpm', + ['jest', 'tests/index.test.tsx'], + { + cwd: next.testDir, + reject: false, + } + ) + // Uncaught [TypeError: (0 , _reactdom.preload) is not a function] + expect(stdout + stderr).not.toContain('is not a function') + }) +})