Skip to content
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

fix(next/image): don't call ReactDOM.preload if missing, such as jest #53443

Merged
merged 7 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/src/client/image-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function ImagePreload({
...getDynamicProps(imgAttributes.fetchPriority),
}

if (isAppRouter) {
if (isAppRouter && preload) {
styfle marked this conversation as resolved.
Show resolved Hide resolved
// See https://github.com/facebook/react/pull/26940
preload(
imgAttributes.src,
Expand Down
1 change: 1 addition & 0 deletions test/production/jest/next-image-preload/app/app/next.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/production/jest/next-image-preload/app/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Image from 'next/image'
import logo from './next.svg'

export default function MyImage() {
return <Image alt="" src={logo} priority />
}
16 changes: 16 additions & 0 deletions test/production/jest/next-image-preload/app/jest.config.js
Original file line number Diff line number Diff line change
@@ -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', '<rootDir>/'],
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)
52 changes: 52 additions & 0 deletions test/production/jest/next-image-preload/next-image-preload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import path from 'path'

const appDir = path.join(__dirname, 'app')

describe('next/jest', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
app: new FileRef(path.join(appDir, 'app')),
styfle marked this conversation as resolved.
Show resolved Hide resolved
[`${appDir}/app/index.test.tsx`]: `
import { render, screen } from '@testing-library/react'
import Page from './page'

it('<Page /> renders', () => {
render(<Page />)

const logo = screen.getByRole('img')

expect(logo).toBeVisible()
})
`,
'jest.config.js': new FileRef(path.join(appDir, 'jest.config.js')),
},
dependencies: {
jest: '27.4.7',
'@testing-library/react': '^13.1.1',
styfle marked this conversation as resolved.
Show resolved Hide resolved
jsdom: '^19.0.0',
styfle marked this conversation as resolved.
Show resolved Hide resolved
'@testing-library/jest-dom': '5.16.4',
styfle marked this conversation as resolved.
Show resolved Hide resolved
},
packageJson: {
scripts: {
// Runs jest and bails if jest fails
build:
'pnpm jest --forceExit tests/index.test.tsx && pnpm next build',
},
},
buildCommand: `pnpm build`,
styfle marked this conversation as resolved.
Show resolved Hide resolved
})
})
afterAll(() => next.destroy())

it('Should not throw preload is undefined error', async () => {
expect(next.cliOutput).not.toContain(
'"Error: Uncaught [TypeError: (0 , _reactdom.preload) is not a function]'
)
})
})