File size: 5,884 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
import { nextTestSetup } from 'e2e-utils'
import { check, retry } from 'next-test-utils'
import { join } from 'node:path'
describe.each(['NEXT_DEPLOYMENT_ID', 'CUSTOM_DEPLOYMENT_ID'])(
'deployment-id-handling enabled with %s',
(envKey) => {
const deploymentId = Date.now() + ''
const { next } = nextTestSetup({
files: join(__dirname, 'app'),
env: {
[envKey]: deploymentId,
},
})
it.each([
{ urlPath: '/' },
{ urlPath: '/pages-edge' },
{ urlPath: '/from-app' },
{ urlPath: '/from-app/edge' },
])(
'should append dpl query to all assets correctly for $urlPath',
async ({ urlPath }) => {
const $ = await next.render$(urlPath)
expect($('#deploymentId').text()).toBe(deploymentId)
const scripts = Array.from($('script'))
expect(scripts.length).toBeGreaterThan(0)
for (const script of scripts) {
if (script.attribs.src) {
expect(script.attribs.src).toContain('dpl=' + deploymentId)
}
}
const links = Array.from($('link'))
expect(links.length).toBeGreaterThan(0)
for (const link of links) {
if (link.attribs.href && link.attribs.rel !== 'expect') {
if (link.attribs.as === 'font') {
expect(link.attribs.href).not.toContain('dpl=' + deploymentId)
} else {
expect(link.attribs.href).toContain('dpl=' + deploymentId)
}
}
}
const browser = await next.browser(urlPath)
const requests = []
browser.on('request', (req) => {
if (req.url().includes('/_next/static')) {
requests.push(req.url())
}
})
await browser.elementByCss('#dynamic-import').click()
await check(
() => (requests.length > 0 ? 'success' : JSON.stringify(requests)),
'success'
)
try {
expect(
requests.every((item) => item.includes('dpl=' + deploymentId))
).toBe(true)
} finally {
require('console').error('requests', requests)
}
}
)
it.each([{ pathname: '/api/hello' }, { pathname: '/api/hello-app' }])(
'should have deployment id env available',
async ({ pathname }) => {
const res = await next.fetch(pathname)
expect(await res.json()).toEqual({
deploymentId,
})
}
)
it('should contain deployment id in prefetch request', async () => {
const dataHeaders = []
const browser = await next.browser('/', {
beforePageLoad(page) {
page.on('request', async (req) => {
const headers = await req.allHeaders()
if (headers['x-nextjs-data']) {
dataHeaders.push(headers)
}
})
},
})
await browser.elementByCss('#edge-link').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('hello pages edge')
expect(await browser.url()).toContain('/pages-edge')
expect(dataHeaders.length).toBeGreaterThan(0)
})
expect(
dataHeaders.every(
(headers) => headers['x-deployment-id'] === deploymentId
)
).toBe(true)
})
it('should contain deployment id in RSC payload request headers', async () => {
const rscHeaders = []
const browser = await next.browser('/from-app', {
beforePageLoad(page) {
page.on('request', async (req) => {
const headers = await req.allHeaders()
if (headers['rsc']) {
rscHeaders.push(headers)
}
})
},
})
await browser.elementByCss('#other-app').click()
await retry(async () => {
expect(await browser.elementByCss('h1').text()).toBe('other app')
expect(await browser.url()).toContain('/other-app')
expect(rscHeaders.length).toBeGreaterThan(0)
})
expect(
rscHeaders.every(
(headers) => headers['x-deployment-id'] === deploymentId
)
).toBe(true)
})
}
)
describe('deployment-id-handling disabled', () => {
const deploymentId = Date.now() + ''
const { next } = nextTestSetup({
files: join(__dirname, 'app'),
})
it.each([
{ urlPath: '/' },
{ urlPath: '/pages-edge' },
{ urlPath: '/from-app' },
{ urlPath: '/from-app/edge' },
])(
'should not append dpl query to all assets for $urlPath',
async ({ urlPath }) => {
const $ = await next.render$(urlPath)
expect($('#deploymentId').text()).not.toBe(deploymentId)
const scripts = Array.from($('script'))
expect(scripts.length).toBeGreaterThan(0)
for (const script of scripts) {
if (script.attribs.src) {
expect(script.attribs.src).not.toContain('dpl=' + deploymentId)
}
}
const links = Array.from($('link'))
expect(links.length).toBeGreaterThan(0)
for (const link of links) {
if (link.attribs.href) {
if (link.attribs.as === 'font') {
expect(link.attribs.href).not.toContain('dpl=' + deploymentId)
} else {
expect(link.attribs.href).not.toContain('dpl=' + deploymentId)
}
}
}
const browser = await next.browser(urlPath)
const requests = []
browser.on('request', (req) => {
requests.push(req.url())
})
await browser.elementByCss('#dynamic-import').click()
await check(
() => (requests.length > 0 ? 'success' : JSON.stringify(requests)),
'success'
)
try {
expect(
requests.every((item) => !item.includes('dpl=' + deploymentId))
).toBe(true)
} finally {
require('console').error('requests', requests)
}
}
)
})
|