forked from vercel/next.js
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pretty error when image import is invalid format (vercel#41267)
This PR improves the error message when an invalid image is imported. It also ensures the page that imported the image is displayed. ### Before <img width="1062" alt="image" src="https://user-images.githubusercontent.com/229881/194674177-70f4ae73-64c9-497f-8e20-098f949a4219.png"> ### After <img width="1002" alt="image" src="https://user-images.githubusercontent.com/229881/194716285-27dd3455-60a9-440f-a50e-eff8d49e764b.png"> Co-authored-by: Balázs Orbán <info@balazsorban.com>
- Loading branch information
Showing
6 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
test/integration/image-future/invalid-image-import/pages/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import React from 'react' | ||
import Image from 'next/future/image' | ||
import test from '../public/invalid.svg' | ||
|
||
const Page = () => { | ||
return ( | ||
<div> | ||
<h1>Try to import and invalid image file</h1> | ||
<Image id="invalid-img" src={test} width={400} height={400} /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Page |
3 changes: 3 additions & 0 deletions
3
test/integration/image-future/invalid-image-import/public/invalid.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions
70
test/integration/image-future/invalid-image-import/test/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* eslint-env jest */ | ||
|
||
import { join } from 'path' | ||
import { | ||
findPort, | ||
getRedboxHeader, | ||
getRedboxSource, | ||
hasRedbox, | ||
killApp, | ||
launchApp, | ||
nextBuild, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
|
||
const appDir = join(__dirname, '../') | ||
let appPort: number | ||
let app | ||
let stderr = '' | ||
const msg = | ||
'Error: Image import "../public/invalid.svg" is not a valid image file. The image may be corrupted or an unsupported format.' | ||
|
||
function runTests({ isDev }) { | ||
it('should show error', async () => { | ||
if (isDev) { | ||
const browser = await webdriver(appPort, '/') | ||
expect(await hasRedbox(browser)).toBe(true) | ||
expect(await getRedboxHeader(browser)).toBe('Failed to compile') | ||
expect(await getRedboxSource(browser)).toBe(`./pages/index.js:3\n${msg}`) | ||
expect(stderr).toContain(msg) | ||
} else { | ||
expect(stderr).toContain(msg) | ||
} | ||
}) | ||
} | ||
|
||
describe('Missing Import Image Tests', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort, { | ||
onStderr(msg) { | ||
stderr += msg || '' | ||
}, | ||
}) | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: true }) | ||
}) | ||
|
||
describe('server mode', () => { | ||
beforeAll(async () => { | ||
stderr = '' | ||
const result = await nextBuild(appDir, [], { stderr: true }) | ||
stderr = result.stderr | ||
}) | ||
afterAll(async () => { | ||
if (app) { | ||
await killApp(app) | ||
} | ||
}) | ||
|
||
runTests({ isDev: false }) | ||
}) | ||
}) |