rsnext/test/e2e/app-dir/next-image/next-image.test.ts
Tim Neutkens 59e042a9a7
Implement client_root for edge in Turbopack (#61024)
## What?

Implements https://github.com/vercel/turbo/pull/7081. Ensures image
imports in the edge runtime have the correct asset url. Previously it
would be `/assets/file.hash.png` but it should be
`/_next/static/media/file.hash.png`.

Updates the image tests to correctly compare the values in Turbopack and
the values in Webpack. Currently it only checks webpack values, causing
the tests to fail in Turbopack.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2194
2024-01-23 18:41:02 +01:00

344 lines
15 KiB
TypeScript

import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app dir - next-image',
{
files: __dirname,
skipDeployment: true,
},
({ next }) => {
describe('ssr content', () => {
it('should render images on / route', async () => {
const $ = await next.render$('/')
const layout = $('#app-layout')
if (process.env.TURBOPACK) {
expect(layout.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=85"`
)
} else {
expect(layout.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=85"`
)
}
if (process.env.TURBOPACK) {
expect(layout.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=85 2x"`
)
} else {
expect(layout.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=85 2x"`
)
}
const page = $('#app-page')
if (process.env.TURBOPACK) {
expect(page.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=90"`
)
} else {
expect(page.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=90"`
)
}
if (process.env.TURBOPACK) {
expect(page.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=90 2x"`
)
} else {
expect(page.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=90 2x"`
)
}
const comp = $('#app-comp')
if (process.env.TURBOPACK) {
expect(comp.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=80"`
)
} else {
expect(comp.attr('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=80"`
)
}
if (process.env.TURBOPACK) {
expect(comp.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=80 2x"`
)
} else {
expect(comp.attr('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=80 2x"`
)
}
})
it('should render images on /client route', async () => {
const $ = await next.render$('/client')
const root = $('#app-layout')
expect(root.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85/
)
expect(root.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=85 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85 2x/
)
const layout = $('#app-client-layout')
expect(layout.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=55/
)
expect(layout.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=55 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=55 2x/
)
const page = $('#app-client-page')
expect(page.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=60/
)
expect(page.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=60 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=60 2x/
)
const comp = $('#app-client-comp')
expect(comp.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=50/
)
expect(comp.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=50 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=50 2x/
)
})
it('should render images nested under page dir on /nested route', async () => {
const $ = await next.render$('/nested')
const root = $('#app-layout')
expect(root.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85/
)
expect(root.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=85 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85 2x/
)
const layout = $('#app-nested-layout')
expect(layout.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=70/
)
expect(layout.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=70 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=70 2x/
)
const page = $('#app-nested-page')
expect(page.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=75/
)
expect(page.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=75 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=75 2x/
)
const comp = $('#app-nested-comp')
expect(comp.attr('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=65/
)
expect(comp.attr('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=65 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=65 2x/
)
})
})
describe('browser content', () => {
it('should render images on / route', async () => {
const browser = await next.browser('/')
const layout = await browser.elementById('app-layout')
if (process.env.TURBOPACK) {
expect(await layout.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=85"`
)
} else {
expect(await layout.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=85"`
)
}
if (process.env.TURBOPACK) {
expect(await layout.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=85 2x"`
)
} else {
expect(await layout.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=85 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=85 2x"`
)
}
const page = await browser.elementById('app-page')
if (process.env.TURBOPACK) {
expect(await page.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=90"`
)
} else {
expect(await page.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=90"`
)
}
if (process.env.TURBOPACK) {
expect(await page.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=90 2x"`
)
} else {
expect(await page.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=90 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=90 2x"`
)
}
const comp = await browser.elementById('app-comp')
if (process.env.TURBOPACK) {
expect(await comp.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=80"`
)
} else {
expect(await comp.getAttribute('src')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=80"`
)
}
if (process.env.TURBOPACK) {
expect(await comp.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.308c602d.png&w=828&q=80 2x"`
)
} else {
expect(await comp.getAttribute('srcset')).toMatchInlineSnapshot(
`"/_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=640&q=80 1x, /_next/image?url=%2F_next%2Fstatic%2Fmedia%2Ftest.3f1a293b.png&w=828&q=80 2x"`
)
}
})
it('should render images nested under page dir on /nested route', async () => {
const browser = await next.browser('/nested')
const root = await browser.elementById('app-layout')
expect(await root.getAttribute('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85/
)
expect(await root.getAttribute('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=640&q=85 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.png&w=828&q=85 2x/
)
const layout = await browser.elementById('app-nested-layout')
expect(await layout.getAttribute('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=70/
)
expect(await layout.getAttribute('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=70 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=70 2x/
)
const page = await browser.elementById('app-nested-page')
expect(await page.getAttribute('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=75/
)
expect(await page.getAttribute('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=75 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=75 2x/
)
const comp = await browser.elementById('app-nested-comp')
expect(await comp.getAttribute('src')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=65/
)
expect(await comp.getAttribute('srcset')).toMatch(
/\/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=640&q=65 1x, \/_next\/image\?url=%2F_next%2Fstatic%2Fmedia%2Ftest\.([^.]+)\.jpg&w=828&q=65 2x/
)
})
})
describe('image content', () => {
it('should render images on / route', async () => {
const $ = await next.render$('/')
const res1 = await next.fetch($('#app-layout').attr('src'))
expect(res1.status).toBe(200)
expect(res1.headers.get('content-type')).toBe('image/png')
const res2 = await next.fetch($('#app-page').attr('src'))
expect(res2.status).toBe(200)
expect(res2.headers.get('content-type')).toBe('image/png')
const res3 = await next.fetch($('#app-comp').attr('src'))
expect(res3.status).toBe(200)
expect(res3.headers.get('content-type')).toBe('image/png')
})
it('should render images on /client route', async () => {
const $ = await next.render$('/client')
const res1 = await next.fetch($('#app-layout').attr('src'))
expect(res1.status).toBe(200)
expect(res1.headers.get('content-type')).toBe('image/png')
const res2 = await next.fetch($('#app-client-layout').attr('src'))
expect(res2.status).toBe(200)
expect(res2.headers.get('content-type')).toBe('image/png')
const res3 = await next.fetch($('#app-client-page').attr('src'))
expect(res3.status).toBe(200)
expect(res3.headers.get('content-type')).toBe('image/png')
const res4 = await next.fetch($('#app-client-comp').attr('src'))
expect(res4.status).toBe(200)
expect(res4.headers.get('content-type')).toBe('image/png')
})
it('should render images nested under page dir on /nested route', async () => {
const $ = await next.render$('/nested')
const res1 = await next.fetch($('#app-layout').attr('src'))
expect(res1.status).toBe(200)
expect(res1.headers.get('content-type')).toBe('image/png')
const res2 = await next.fetch($('#app-nested-layout').attr('src'))
expect(res2.status).toBe(200)
expect(res2.headers.get('content-type')).toBe('image/jpeg')
const res3 = await next.fetch($('#app-nested-page').attr('src'))
expect(res3.status).toBe(200)
expect(res3.headers.get('content-type')).toBe('image/jpeg')
const res4 = await next.fetch($('#app-nested-comp').attr('src'))
expect(res4.status).toBe(200)
expect(res4.headers.get('content-type')).toBe('image/jpeg')
})
it('should render legacy images under /legacy route', async () => {
const $ = await next.render$('/legacy')
const res2 = await next.fetch($('#app-legacy-layout').attr('src'))
expect(res2.status).toBe(200)
expect(res2.headers.get('content-type')).toBe('image/png')
const res3 = await next.fetch($('#app-legacy-page').attr('src'))
expect(res3.status).toBe(200)
expect(res3.headers.get('content-type')).toBe('image/png')
})
it('should render legacy images in edge runtime on /legacy-edge-runtime route', async () => {
const $ = await next.render$('/legacy-edge-runtime')
const res2 = await next.fetch($('#app-legacy-edge-layout').attr('src'))
expect(res2.status).toBe(200)
expect(res2.headers.get('content-type')).toBe('image/png')
const res3 = await next.fetch($('#app-legacy-edge-page').attr('src'))
expect(res3.status).toBe(200)
expect(res3.headers.get('content-type')).toBe('image/png')
})
})
}
)