| import { nextTestSetup } from 'e2e-utils' |
| import { retry } from 'next-test-utils' |
|
|
| describe('custom-cache-handler-image', () => { |
| const { next, skipped } = nextTestSetup({ |
| files: __dirname, |
| skipDeployment: true, |
| env: { |
| |
| MAX_IMAGE_CACHE_ENTRIES: '2', |
| }, |
| }) |
|
|
| if (skipped) { |
| return |
| } |
|
|
| it('should use custom cache handler for image optimization', async () => { |
| |
| const $ = await next.render$('/') |
| expect($('p').text()).toBe('hello world') |
|
|
| const smallImgSrc = $('#image-small').attr('src') |
| expect(smallImgSrc).toContain('/_next/image') |
|
|
| |
| const imageRes = await next.fetch(smallImgSrc) |
| expect(imageRes.status).toBe(200) |
|
|
| |
| await retry(() => { |
| expect(next.cliOutput).toContain('initialized custom cache-handler') |
| expect(next.cliOutput).toContain('cache-handler set') |
| expect(next.cliOutput).toMatch(/kind:.*IMAGE/) |
| }) |
| }) |
|
|
| it('should evict oldest entries when cache exceeds max size', async () => { |
| const $ = await next.render$('/') |
|
|
| const smallImgSrc = $('#image-small').attr('src') |
| const mediumImgSrc = $('#image-medium').attr('src') |
| const largeImgSrc = $('#image-large').attr('src') |
|
|
| |
| |
| |
|
|
| |
| await next.fetch(smallImgSrc) |
| await retry(() => { |
| expect(next.cliOutput).toContain('cache-handler image cache size: 1') |
| }) |
|
|
| |
| await next.fetch(mediumImgSrc) |
| await retry(() => { |
| expect(next.cliOutput).toContain('cache-handler image cache size: 2') |
| }) |
|
|
| |
| await next.fetch(largeImgSrc) |
| await retry(() => { |
| expect(next.cliOutput).toContain('cache-handler evicting') |
| |
| const sizeMatches = next.cliOutput.match( |
| /cache-handler image cache size: (\d+)/g |
| ) |
| const lastSize = sizeMatches?.[sizeMatches.length - 1] |
| expect(lastSize).toContain('size: 2') |
| }) |
| }) |
|
|
| it('should miss cache for evicted entries', async () => { |
| const $ = await next.render$('/') |
|
|
| const smallImgSrc = $('#image-small').attr('src') |
| const mediumImgSrc = $('#image-medium').attr('src') |
| const largeImgSrc = $('#image-large').attr('src') |
|
|
| |
| await next.fetch(smallImgSrc) |
| await next.fetch(mediumImgSrc) |
| await next.fetch(largeImgSrc) |
|
|
| |
| const outputBefore = next.cliOutput |
|
|
| |
| await next.fetch(smallImgSrc) |
|
|
| await retry(() => { |
| |
| const newOutput = next.cliOutput.slice(outputBefore.length) |
| |
| expect(newOutput).toContain('cache-handler miss') |
| |
| expect(newOutput).toContain('cache-handler set') |
| }) |
| }) |
| }) |
|
|