File size: 67,097 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 |
import { nextTestSetup } from 'e2e-utils'
import { check, retry, waitFor } from 'next-test-utils'
import cheerio from 'cheerio'
import stripAnsi from 'strip-ansi'
import {
NEXT_RSC_UNION_QUERY,
RSC_HEADER,
} from 'next/dist/client/components/app-router-headers'
// TODO: We should decide on an established pattern for gating test assertions
// on experimental flags. For example, as a first step we could all the common
// gates like this one into a single module.
const isPPREnabledByDefault = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
describe('app dir - basic', () => {
const { next, isNextDev, isNextStart, isNextDeploy, isTurbopack } =
nextTestSetup({
files: __dirname,
buildCommand: process.env.NEXT_EXPERIMENTAL_COMPILE
? 'pnpm compile-mode'
: undefined,
packageJson: {
scripts: {
'compile-mode': process.env.NEXT_EXPERIMENTAL_COMPILE
? `next build --experimental-build-mode=compile && next build --experimental-build-mode=generate-env`
: undefined,
},
},
dependencies: {
nanoid: '4.0.1',
},
env: {
NEXT_PUBLIC_TEST_ID: Date.now() + '',
},
})
if (isNextStart) {
it('should have correct cache-control for SSR routes', async () => {
for (const path of ['/catch-all/first', '/ssr']) {
const res = await next.fetch(path)
expect(res.status).toBe(200)
expect(res.headers.get('Cache-Control')).toBe(
'private, no-cache, no-store, max-age=0, must-revalidate'
)
}
})
}
if (process.env.NEXT_EXPERIMENTAL_COMPILE) {
it('should provide query for getStaticProps page correctly', async () => {
const res = await next.fetch('/ssg?hello=world')
expect(res.status).toBe(200)
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({ hello: 'world' })
})
}
if (isNextStart) {
it('should contain framework.json', async () => {
const frameworksJson = await next.readJSON(
'.next/diagnostics/framework.json'
)
expect(frameworksJson).toEqual({
name: 'Next.js',
version: require('next/package.json').version,
})
})
it('outputs correct build-diagnostics.json', async () => {
const buildDiagnosticsJson = await next.readJSON(
'.next/diagnostics/build-diagnostics.json'
)
expect(buildDiagnosticsJson).toMatchObject({
buildStage: 'static-generation',
buildOptions: {},
})
})
}
if (isNextStart && !process.env.NEXT_EXPERIMENTAL_COMPILE) {
if (!process.env.NEXT_EXPERIMENTAL_COMPILE) {
it('should have correct size in build output', async () => {
expect(next.cliOutput).toMatch(
/\/dashboard\/another.*? *?[^0]\d{1,} [\w]{1,}B/
)
})
}
it('should have correct preferredRegion values in manifest', async () => {
const middlewareManifest = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')
)
expect(
middlewareManifest.functions['/(rootonly)/dashboard/hello/page'].regions
).toEqual(['iad1', 'sfo1'])
expect(middlewareManifest.functions['/dashboard/page'].regions).toEqual([
'iad1',
])
expect(
middlewareManifest.functions['/slow-page-no-loading/page'].regions
).toEqual(['global'])
expect(middlewareManifest.functions['/test-page/page'].regions).toEqual([
'home',
])
// Inherits from the root layout.
expect(
middlewareManifest.functions['/slow-page-with-loading/page'].regions
).toEqual(['sfo1'])
})
}
it('should work for catch-all edge page', async () => {
const html = await next.render('/catch-all-edge/hello123')
const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual({
slug: ['hello123'],
})
})
it('should return normalized dynamic route params for catch-all edge page', async () => {
const html = await next.render('/catch-all-edge/a/b/c')
const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual({
slug: ['a', 'b', 'c'],
})
})
it('should have correct searchParams and params (server)', async () => {
const html = await next.render('/dynamic/category-1/id-2?query1=value2')
const $ = cheerio.load(html)
expect(JSON.parse($('#id-page-params').text())).toEqual({
category: 'category-1',
id: 'id-2',
})
expect(JSON.parse($('#search-params').text())).toEqual({
query1: 'value2',
})
})
it('should have correct searchParams and params (client)', async () => {
const browser = await next.browser(
'/dynamic-client/category-1/id-2?query1=value2'
)
const html = await browser.eval('document.documentElement.innerHTML')
const $ = cheerio.load(html)
expect(JSON.parse($('#id-page-params').text())).toEqual({
category: 'category-1',
id: 'id-2',
})
expect(JSON.parse($('#search-params').text())).toEqual({
query1: 'value2',
})
})
if (!isNextDev) {
it('should successfully detect app route during prefetch', async () => {
const browser = await next.browser('/')
await check(async () => {
const found = await browser.eval(
'!!window.next.router.components["/dashboard"]'
)
return found
? 'success'
: await browser.eval('Object.keys(window.next.router.components)')
}, 'success')
await browser.elementByCss('a').click()
await browser.waitForElementByCss('#from-dashboard')
})
}
// Turbopack has different chunking in dev/production which results in the entrypoint name not being included in the outputs.
if (!process.env.IS_TURBOPACK_TEST) {
it('should encode chunk path correctly', async () => {
await next.fetch('/dynamic-client/first/second')
const browser = await next.browser('/')
const requests = []
browser.on('request', (req) => {
requests.push(req.url())
})
await browser.eval(
'window.location.href = "/dynamic-client/first/second"'
)
await browser.waitForElementByCss('#id-page-params')
expect(
requests.some(
(req) =>
req.includes(
encodeURI(isTurbopack ? '[category]_[id]' : '/[category]/[id]')
) && req.includes('.js')
)
).toBe(true)
})
}
it.each([
{ pathname: '/redirect-1' },
{ pathname: '/redirect-2' },
{ pathname: '/blog/old-post' },
{ pathname: '/redirect-3/some' },
{ pathname: '/redirect-4' },
{ pathname: '/redirect-4/?q=1&=' },
])(
'should match redirects in pages correctly $path',
async ({ pathname }) => {
let browser = await next.browser('/')
await browser.eval(`next.router.push("${pathname}")`)
await check(async () => {
const href = await browser.eval('location.href')
return href.includes('example.vercel.sh') ? 'yes' : href
}, 'yes')
if (pathname.includes('/blog')) {
browser = await next.browser('/blog/first')
await browser.eval('window.beforeNav = 1')
// check 5 times to ensure a reload didn't occur
for (let i = 0; i < 5; i++) {
await waitFor(500)
expect(
await browser.eval('document.documentElement.innerHTML')
).toContain('hello from pages/blog/[slug]')
expect(await browser.eval('window.beforeNav')).toBe(1)
}
}
}
)
it('should not apply client router filter on shallow', async () => {
const browser = await next.browser('/')
await browser.eval('window.beforeNav = 1')
await check(async () => {
await browser.eval(
`window.next.router.push('/', '/redirect-1', { shallow: true })`
)
return await browser.eval('window.location.pathname')
}, '/redirect-1')
expect(await browser.eval('window.beforeNav')).toBe(1)
})
if (isNextDev) {
it('should not have duplicate config warnings', async () => {
await next.fetch('/')
expect(
stripAnsi(next.cliOutput).match(/Experiments \(use with caution\):/g)
.length
).toBe(1)
})
}
if (!isNextDeploy) {
it('should not share edge workers', async () => {
const controller1 = new AbortController()
const controller2 = new AbortController()
next
.fetch('/slow-page-no-loading', {
signal: controller1.signal,
})
.catch(() => {})
next
.fetch('/slow-page-no-loading', {
signal: controller2.signal,
})
.catch(() => {})
await waitFor(1000)
controller1.abort()
const controller3 = new AbortController()
next
.fetch('/slow-page-no-loading', {
signal: controller3.signal,
})
.catch(() => {})
await waitFor(1000)
controller2.abort()
controller3.abort()
const res = await next.fetch('/slow-page-no-loading')
expect(res.status).toBe(200)
expect(await res.text()).toContain('hello from slow page')
expect(next.cliOutput).not.toContain(
'A separate worker must be used for each render'
)
})
}
if (isNextStart) {
it('should generate build traces correctly', async () => {
const trace = JSON.parse(
await next.readFile(
'.next/server/app/dashboard/deployments/[id]/page.js.nft.json'
)
) as { files: string[] }
expect(trace.files.some((file) => file.endsWith('data.json'))).toBe(true)
})
}
it('should use text/x-component for flight', async () => {
const res = await next.fetch(
`/dashboard/deployments/123?${NEXT_RSC_UNION_QUERY}`,
{
headers: {
[RSC_HEADER]: '1',
},
}
)
expect(res.headers.get('Content-Type')).toBe('text/x-component')
})
it('should use text/x-component for flight with edge runtime', async () => {
const res = await next.fetch(`/dashboard?${NEXT_RSC_UNION_QUERY}`, {
headers: {
[RSC_HEADER]: '1',
},
})
expect(res.headers.get('Content-Type')).toBe('text/x-component')
})
it('should return the `vary` header from edge runtime', async () => {
const res = await next.fetch('/dashboard')
expect(res.headers.get('x-edge-runtime')).toBe('1')
expect(res.headers.get('vary')).toBe(
'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch'
)
})
it('should return the `vary` header from pages for flight requests', async () => {
const res = await next.fetch(`/?${NEXT_RSC_UNION_QUERY}`, {
headers: {
[RSC_HEADER]: '1',
},
})
expect(res.headers.get('vary')).toBe(
isNextDeploy
? 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch'
: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch, Accept-Encoding'
)
})
it('should pass props from getServerSideProps in root layout', async () => {
const $ = await next.render$('/dashboard')
expect($('title').first().text()).toBe('hello world')
})
it('should serve from pages', async () => {
const html = await next.render('/')
expect(html).toContain('hello from pages/index')
})
it('should serve dynamic route from pages', async () => {
const html = await next.render('/blog/first')
expect(html).toContain('hello from pages/blog/[slug]')
})
it('should serve from public', async () => {
const html = await next.render('/hello.txt')
expect(html).toContain('hello world')
})
it('should serve from app', async () => {
const html = await next.render('/dashboard')
expect(html).toContain('hello from app/dashboard')
})
it('should ensure the </body></html> suffix is at the end of the stream', async () => {
const html = await next.render('/dashboard')
// It must end with the suffix and not contain it anywhere else.
const suffix = '</body></html>'
expect(html).toEndWith(suffix)
expect(html.slice(0, -suffix.length)).not.toContain(suffix)
})
if (!isNextDeploy) {
it('should serve /index as separate page', async () => {
const stderr = []
next.on('stderr', (err) => {
stderr.push(err)
})
const html = await next.render('/dashboard/index')
expect(html).toContain('hello from app/dashboard/index')
expect(stderr.some((err) => err.includes('Invalid hook call'))).toBe(
false
)
})
it('should serve polyfills for browsers that do not support modules', async () => {
const html = await next.render('/dashboard/index')
expect(html).toMatch(
isTurbopack
? /<script src="\/_next\/static\/chunks\/([\w-]*polyfill-nomodule|[0-9a-f]+)\.js" noModule="">/
: /<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" noModule="">/
)
})
}
// TODO-APP: handle css modules fouc in dev
it.skip('should handle css imports in next/dynamic correctly', async () => {
const browser = await next.browser('/dashboard/index')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#css-text-dynamic-server')).color`
)
).toBe('rgb(0, 0, 255)')
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('#css-text-lazy')).color`
)
).toBe('rgb(128, 0, 128)')
})
it('should include layouts when no direct parent layout', async () => {
const $ = await next.render$('/dashboard/integrations')
// Should not be nested in dashboard
expect($('h1').text()).toBe('Dashboard')
// Should include the page text
expect($('p').text()).toBe('hello from app/dashboard/integrations')
})
// TODO-APP: handle new root layout
it.skip('should not include parent when not in parent directory with route in directory', async () => {
const $ = await next.render$('/dashboard/hello')
const html = $.html()
// new root has to provide it's own custom root layout or the default
// is used instead
expect(html).toContain('<html')
expect(html).toContain('<body')
expect($('html').hasClass('this-is-the-document-html')).toBeFalsy()
expect($('body').hasClass('this-is-the-document-body')).toBeFalsy()
// Should not be nested in dashboard
expect($('h1').text()).toBeFalsy()
// Should render the page text
expect($('p').text()).toBe('hello from app/dashboard/rootonly/hello')
})
it('should use new root layout when provided', async () => {
const $ = await next.render$('/dashboard/another')
// new root has to provide it's own custom root layout or the default
// is used instead
expect($('html').hasClass('this-is-another-document-html')).toBeTruthy()
expect($('body').hasClass('this-is-another-document-body')).toBeTruthy()
// Should not be nested in dashboard
expect($('h1').text()).toBeFalsy()
// Should render the page text
expect($('p').text()).toBe('hello from newroot/dashboard/another')
})
it('should not create new root layout when nested (optional)', async () => {
const $ = await next.render$('/dashboard/deployments/breakdown')
// new root has to provide it's own custom root layout or the default
// is used instead
expect($('html').hasClass('this-is-the-document-html')).toBeTruthy()
expect($('body').hasClass('this-is-the-document-body')).toBeTruthy()
// Should be nested in dashboard
expect($('h1').text()).toBe('Dashboard')
expect($('h2').text()).toBe('Custom dashboard')
// Should render the page text
expect($('p').text()).toBe(
'hello from app/dashboard/(custom)/deployments/breakdown'
)
})
it('should include parent document when no direct parent layout', async () => {
const $ = await next.render$('/dashboard/integrations')
expect($('html').hasClass('this-is-the-document-html')).toBeTruthy()
expect($('body').hasClass('this-is-the-document-body')).toBeTruthy()
})
it('should not include parent when not in parent directory', async () => {
const $ = await next.render$('/dashboard/changelog')
// Should not be nested in dashboard
expect($('h1').text()).toBeFalsy()
// Should include the page text
expect($('p').text()).toBe('hello from app/dashboard/changelog')
})
it('should serve nested parent', async () => {
const $ = await next.render$('/dashboard/deployments/123')
// Should be nested in dashboard
expect($('h1').text()).toBe('Dashboard')
// Should be nested in deployments
expect($('h2').text()).toBe('Deployments hello')
})
it('should serve dynamic parameter', async () => {
const $ = await next.render$('/dashboard/deployments/123')
// Should include the page text with the parameter
expect($('p').text()).toBe(
'hello from app/dashboard/deployments/[id]. ID is: 123'
)
})
// TODO-APP: fix to ensure behavior matches on deploy
if (!isNextDeploy) {
it('should serve page as a segment name correctly', async () => {
const html = await next.render('/dashboard/page')
expect(html).toContain('hello dashboard/page!')
})
}
it('should include document html and body', async () => {
const $ = await next.render$('/dashboard')
expect($('html').hasClass('this-is-the-document-html')).toBeTruthy()
expect($('body').hasClass('this-is-the-document-body')).toBeTruthy()
})
it('should not serve when layout is provided but no folder index', async () => {
const res = await next.fetch('/dashboard/deployments')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
})
// TODO-APP: do we want to make this only work for /root or is it allowed
// to work for /pages as well?
it.skip('should match partial parameters', async () => {
const html = await next.render('/partial-match-123')
expect(html).toContain('hello from app/partial-match-[id]. ID is: 123')
})
describe('rewrites', () => {
// TODO-APP: rewrite url is broken
it('should support rewrites on initial load', async () => {
const browser = await next.browser('/rewritten-to-dashboard')
expect(await browser.elementByCss('h1').text()).toBe('Dashboard')
expect(await browser.url()).toBe(`${next.url}/rewritten-to-dashboard`)
})
it('should support rewrites on client-side navigation from pages to app with existing pages path', async () => {
await next.fetch('/exists-but-not-routed')
const browser = await next.browser('/link-to-rewritten-path')
try {
// Click the link.
await check(async () => {
await browser.elementById('link-to-rewritten-path').click()
await browser.waitForElementByCss('#from-dashboard', 5000)
// Check to see that we were rewritten and not redirected.
// TODO-APP: rewrite url is broken
// expect(await browser.url()).toBe(`${next.url}/rewritten-to-dashboard`)
// Check to see that the page we navigated to is in fact the dashboard.
expect(await browser.elementByCss('#from-dashboard').text()).toBe(
'hello from app/dashboard'
)
return 'success'
}, 'success')
} finally {
await browser.close()
}
})
it('should support rewrites on client-side navigation', async () => {
const browser = await next.browser('/rewrites')
try {
// Click the link.
await browser.elementById('link').click()
await browser.waitForElementByCss('#from-dashboard')
// Check to see that we were rewritten and not redirected.
expect(await browser.url()).toBe(`${next.url}/rewritten-to-dashboard`)
// Check to see that the page we navigated to is in fact the dashboard.
expect(await browser.elementByCss('#from-dashboard').text()).toBe(
'hello from app/dashboard'
)
} finally {
await browser.close()
}
})
})
// TODO-APP: Enable in development
;(isNextDev ||
// When PPR is enabled, the shared layouts re-render because we prefetch
// from the root. This will be addressed before GA.
isPPREnabledByDefault
? it.skip
: it)(
'should not rerender layout when navigating between routes in the same layout',
async () => {
const browser = await next.browser('/same-layout/first')
try {
// Get the render id from the dom and click the first link.
const firstRenderID = await browser.elementById('render-id').text()
await browser.elementById('link').click()
await browser.waitForElementByCss('#second-page')
// Get the render id from the dom again, it should be the same!
const secondRenderID = await browser.elementById('render-id').text()
expect(secondRenderID).toBe(firstRenderID)
// Navigate back to the first page again by clicking the link.
await browser.elementById('link').click()
await browser.waitForElementByCss('#first-page')
// Get the render id from the dom again, it should be the same!
const thirdRenderID = await browser.elementById('render-id').text()
expect(thirdRenderID).toBe(firstRenderID)
} finally {
await browser.close()
}
}
)
it('should handle hash in initial url', async () => {
const browser = await next.browser('/dashboard#abc')
try {
// Check if hash is preserved
expect(await browser.eval('window.location.hash')).toBe('#abc')
await waitFor(1000)
// Check again to be sure as it might be timed different
expect(await browser.eval('window.location.hash')).toBe('#abc')
} finally {
await browser.close()
}
})
describe('<Link />', () => {
it('should hard push', async () => {
const browser = await next.browser('/link-hard-push/123')
try {
// Click the link on the page, and verify that the history entry was
// added.
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id-456')
expect(await browser.eval('window.history.length')).toBe(3)
// Go back, and redo the navigation by clicking the link.
await browser.back()
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id-456')
} finally {
await browser.close()
}
})
it('should hard replace', async () => {
const browser = await next.browser('/link-hard-replace/123')
try {
// Click the link on the page, and verify that the history entry was NOT
// added.
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id-456')
expect(await browser.eval('window.history.length')).toBe(2)
// Navigate to the subpage, verify that the history entry was NOT added.
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id-123')
expect(await browser.eval('window.history.length')).toBe(2)
// Navigate back again, verify that the history entry was NOT added.
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id-456')
expect(await browser.eval('window.history.length')).toBe(2)
} finally {
await browser.close()
}
})
it('should soft push', async () => {
const browser = await next.browser('/link-soft-push')
// set a flag once the page loads so we can track if a hard nav occurred (which would reset the flag)
await browser.eval('window.__nextSoftPushTest = 1')
try {
// Click the link on the page, and verify that the history entry was
// added.
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementById('link').click()
await browser.waitForElementByCss('#render-id')
expect(await browser.eval('window.history.length')).toBe(3)
// Get the id on the rendered page.
const firstID = await browser.elementById('render-id').text()
// Go back, and redo the navigation by clicking the link.
await browser.back()
await browser.elementById('link').click()
// Get the ID again, and compare, they should be the same.
const secondID = await browser.elementById('render-id').text()
// router cache should have invalidated the page content, so the IDs should be different
expect(firstID).not.toBe(secondID)
// verify that the flag is still set
expect(await browser.eval('window.__nextSoftPushTest')).toBe(1)
} finally {
await browser.close()
}
})
it('should soft replace', async () => {
const browser = await next.browser('/link-soft-replace')
// set a flag once the page loads so we can track if a hard nav occurred (which would reset the flag)
await browser.eval('window.__nextSoftPushTest = 1')
try {
// Get the render ID so we can compare it.
const firstID = await browser.elementById('render-id').text()
// Click the link on the page, and verify that the history entry was NOT
// added.
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementById('self-link').click()
await retry(async () => {
// Get the id on the rendered page.
const secondID = await browser.elementById('render-id').text()
expect(secondID).not.toBe(firstID)
expect(await browser.eval('window.history.length')).toBe(2)
})
// Navigate to the subpage, verify that the history entry was NOT added.
await browser.elementById('subpage-link').click()
await browser.waitForElementByCss('#back-link')
expect(await browser.eval('window.history.length')).toBe(2)
// Navigate back again, verify that the history entry was NOT added.
await browser.elementById('back-link').click()
await browser.waitForElementByCss('#render-id')
expect(await browser.eval('window.history.length')).toBe(2)
await retry(async () => {
// Get the ID again, and compare, they should be the same.
const thirdID = await browser.elementById('render-id').text()
expect(thirdID).not.toBe(firstID)
})
// verify that the flag is still set
expect(await browser.eval('window.__nextSoftPushTest')).toBe(1)
} finally {
await browser.close()
}
})
it('should be soft for back navigation', async () => {
const browser = await next.browser('/with-id')
try {
// Get the id on the rendered page.
const firstID = await browser.elementById('render-id').text()
// Click the link, and go back.
await browser.elementById('link').click()
await browser.waitForElementByCss('#from-navigation')
await browser.back()
// Get the date again, and compare, they should be the same.
const secondID = await browser.elementById('render-id').text()
expect(firstID).toBe(secondID)
} finally {
await browser.close()
}
})
it('should be soft for forward navigation', async () => {
const browser = await next.browser('/with-id')
try {
// Click the link.
await browser.elementById('link').click()
await browser.waitForElementByCss('#from-navigation')
// Get the id on the rendered page.
const firstID = await browser.elementById('render-id').text()
// Go back, then forward.
await browser.back()
await browser.forward()
// Get the date again, and compare, they should be the same.
const secondID = await browser.elementById('render-id').text()
expect(firstID).toBe(secondID)
} finally {
await browser.close()
}
})
it('should allow linking from app page to pages page', async () => {
const browser = await next.browser('/pages-linking')
try {
// Click the link.
await browser.elementById('app-link').click()
expect(await browser.waitForElementByCss('#pages-link').text()).toBe(
'To App Page'
)
// Click the other link.
await browser.elementById('pages-link').click()
expect(await browser.waitForElementByCss('#app-link').text()).toBe(
'To Pages Page'
)
} finally {
await browser.close()
}
})
it('should navigate to pages dynamic route from pages page if it overlaps with an app page', async () => {
await next.fetch('/dynamic-pages-route-app-overlap/app-dir')
const browser = await next.browser('/dynamic-pages-route-app-overlap')
try {
// Click the link.
await check(async () => {
await browser.elementById('pages-link').click()
expect(
await browser.waitForElementByCss('#app-text', 5000).text()
).toBe('hello from app/dynamic-pages-route-app-overlap/app-dir/page')
// When refreshing the browser, the app page should be rendered
await browser.refresh()
expect(await browser.waitForElementByCss('#app-text').text()).toBe(
'hello from app/dynamic-pages-route-app-overlap/app-dir/page'
)
return 'success'
}, 'success')
} finally {
await browser.close()
}
})
it('should push to external url', async () => {
const browser = await next.browser('/link-external/push')
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementByCss('#external-link').click()
expect(await browser.waitForElementByCss('h1').text()).toBe(
'Example Domain'
)
expect(await browser.eval('window.history.length')).toBe(3)
})
it('should replace to external url', async () => {
const browser = await next.browser('/link-external/replace')
expect(await browser.eval('window.history.length')).toBe(2)
await browser.elementByCss('#external-link').click()
expect(await browser.waitForElementByCss('h1').text()).toBe(
'Example Domain'
)
expect(await browser.eval('window.history.length')).toBe(2)
})
})
describe('server components', () => {
// TODO-APP: why is this not servable but /dashboard+rootonly/hello.server.js
// should be? Seems like they both either should be servable or not
it('should not serve .server.js as a path', async () => {
// Without .server.js should serve
const html = await next.render('/should-not-serve-server')
expect(html).toContain('hello from app/should-not-serve-server')
// Should not serve `.server`
const res = await next.fetch('/should-not-serve-server.server')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
// Should not serve `.server.js`
const res2 = await next.fetch('/should-not-serve-server.server.js')
expect(res2.status).toBe(404)
expect(await res2.text()).toContain('This page could not be found')
})
it('should not serve .client.js as a path', async () => {
// Without .client.js should serve
const html = await next.render('/should-not-serve-client')
expect(html).toContain('hello from app/should-not-serve-client')
// Should not serve `.client`
const res = await next.fetch('/should-not-serve-client.client')
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
// Should not serve `.client.js`
const res2 = await next.fetch('/should-not-serve-client.client.js')
expect(res2.status).toBe(404)
expect(await res2.text()).toContain('This page could not be found')
})
it('should serve shared component', async () => {
// Without .client.js should serve
const html = await next.render('/shared-component-route')
expect(html).toContain('hello from app/shared-component-route')
})
describe('dynamic routes', () => {
it('should only pass params that apply to the layout', async () => {
const $ = await next.render$('/dynamic/books/hello-world')
expect($('#dynamic-layout-params').text()).toBe('{}')
expect($('#category-layout-params').text()).toBe('{"category":"books"}')
expect($('#id-layout-params').text()).toBe(
'{"category":"books","id":"hello-world"}'
)
expect($('#id-page-params').text()).toBe(
'{"category":"books","id":"hello-world"}'
)
})
})
describe('catch-all routes', () => {
it('should handle optional segments', async () => {
const params = ['this', 'is', 'a', 'test']
const route = params.join('/')
const $ = await next.render$(`/catch-all-optional/${route}`)
expect($('#text').attr('data-params')).toBe(route)
})
it('should handle optional segments root', async () => {
const $ = await next.render$(`/catch-all-optional`)
expect($('#text').attr('data-params')).toBe('')
})
it('should handle optional catch-all segments link', async () => {
const browser = await next.browser('/catch-all-link')
expect(
await browser
.elementByCss('#to-catch-all-optional')
.click()
.waitForElementByCss('#text')
.text()
).toBe(`hello from /catch-all-optional/this/is/a/test`)
})
it('should handle required segments', async () => {
const params = ['this', 'is', 'a', 'test']
const route = params.join('/')
const $ = await next.render$(`/catch-all/${route}`)
expect($('#text').attr('data-params')).toBe(route)
expect($('#not-a-page').text()).toBe('Not a page')
// Components under catch-all should not be treated as route that errors during build.
// They should be rendered properly when imported in page route.
expect($('#widget').text()).toBe('widget')
})
it('should handle required segments root as not found', async () => {
const res = await next.fetch(`/catch-all`)
expect(res.status).toBe(404)
expect(await res.text()).toContain('This page could not be found')
})
it('should handle catch-all segments link', async () => {
const browser = await next.browser('/catch-all-link')
expect(
await browser
.elementByCss('#to-catch-all')
.click()
.waitForElementByCss('#text')
.text()
).toBe(`hello from /catch-all/this/is/a/test`)
})
})
describe('should serve client component', () => {
it('should serve server-side', async () => {
const $ = await next.render$('/client-component-route')
expect($('p').text()).toBe(
'hello from app/client-component-route. count: 0'
)
})
// TODO-APP: investigate hydration not kicking in on some runs
it('should serve client-side', async () => {
const browser = await next.browser('/client-component-route')
// After hydration count should be 1
expect(await browser.elementByCss('p').text()).toBe(
'hello from app/client-component-route. count: 1'
)
})
})
describe('should include client component layout with server component route', () => {
it('should include it server-side', async () => {
const $ = await next.render$('/client-nested')
// Should not be nested in dashboard
expect($('h1').text()).toBe('Client Nested. Count: 0')
// Should include the page text
expect($('p').text()).toBe('hello from app/client-nested')
})
it('should include it client-side', async () => {
const browser = await next.browser('/client-nested')
// After hydration count should be 1
expect(await browser.elementByCss('h1').text()).toBe(
'Client Nested. Count: 1'
)
// After hydration count should be 1
expect(await browser.elementByCss('p').text()).toBe(
'hello from app/client-nested'
)
})
})
describe('Loading', () => {
it('should render loading.js in initial html for slow page', async () => {
const $ = await next.render$('/slow-page-with-loading')
expect($('#loading').text()).toBe('Loading...')
})
it('should render loading.js in browser for slow page', async () => {
const browser = await next.browser('/slow-page-with-loading', {
waitHydration: false,
})
// TODO-APP: `await next.browser()` causes waiting for the full page to complete streaming. At that point "Loading..." is replaced by the actual content
// expect(await browser.elementByCss('#loading').text()).toBe('Loading...')
expect(await browser.elementByCss('#slow-page-message').text()).toBe(
'hello from slow page'
)
})
it('should render loading.js in initial html for slow layout', async () => {
const $ = await next.render$('/slow-layout-with-loading/slow')
expect($('#loading').text()).toBe('Loading...')
})
it('should render loading.js in browser for slow layout', async () => {
const browser = await next.browser('/slow-layout-with-loading/slow', {
waitHydration: false,
})
// TODO-APP: `await next.browser()` causes waiting for the full page to complete streaming. At that point "Loading..." is replaced by the actual content
// expect(await browser.elementByCss('#loading').text()).toBe('Loading...')
expect(await browser.elementByCss('#slow-layout-message').text()).toBe(
'hello from slow layout'
)
expect(await browser.elementByCss('#page-message').text()).toBe(
'Hello World'
)
})
it('should render loading.js in initial html for slow layout and page', async () => {
const $ = await next.render$('/slow-layout-and-page-with-loading/slow')
expect($('#loading-layout').text()).toBe('Loading layout...')
expect($('#loading-page').text()).toBe('Loading page...')
})
it('should render loading.js in browser for slow layout and page', async () => {
const browser = await next.browser(
'/slow-layout-and-page-with-loading/slow',
{
waitHydration: false,
}
)
// TODO-APP: `await next.browser()` causes waiting for the full page to complete streaming. At that point "Loading..." is replaced by the actual content
// expect(await browser.elementByCss('#loading-layout').text()).toBe('Loading...')
// expect(await browser.elementByCss('#loading-page').text()).toBe('Loading...')
expect(await browser.elementByCss('#slow-layout-message').text()).toBe(
'hello from slow layout'
)
expect(await browser.elementByCss('#slow-page-message').text()).toBe(
'hello from slow page'
)
})
})
describe('middleware', () => {
it.each(['rewrite', 'redirect'])(
`should strip internal query parameters from requests to middleware for %s`,
async (method) => {
const browser = await next.browser('/internal')
// Wait for and click the navigation element, this should trigger
// the flight request that'll be caught by the middleware. If the
// middleware sees any flight data on the request it'll redirect to
// a page with an element of #failure, otherwise, we'll see the
// element for #success.
await browser
.waitForElementByCss(`#navigate-${method}`)
.elementById(`navigate-${method}`)
.click()
await check(
async () => await browser.elementByCss('#success').text(),
/Success/
)
}
)
})
describe('next/router', () => {
it('should support router.back and router.forward', async () => {
const browser = await next.browser('/back-forward/1')
const firstMessage = 'Hello from 1'
const secondMessage = 'Hello from 2'
expect(await browser.elementByCss('#message-1').text()).toBe(
firstMessage
)
try {
const message2 = await browser
.waitForElementByCss('#to-other-page')
.click()
.waitForElementByCss('#message-2')
.text()
expect(message2).toBe(secondMessage)
const message1 = await browser
.waitForElementByCss('#back-button')
.click()
.waitForElementByCss('#message-1')
.text()
expect(message1).toBe(firstMessage)
const message2Again = await browser
.waitForElementByCss('#forward-button')
.click()
.waitForElementByCss('#message-2')
.text()
expect(message2Again).toBe(secondMessage)
} finally {
await browser.close()
}
})
})
describe('client components', () => {
if (!isNextDeploy) {
it('should have consistent query and params handling', async () => {
const $ = await next.render$('/param-and-query/params?slug=query')
const el = $('#params-and-query')
expect(el.attr('data-params')).toBe('params')
expect(el.attr('data-query')).toBe('query')
})
}
})
})
if (isNextDev) {
describe('HMR', () => {
it('should HMR correctly for server component', async () => {
const filePath = 'app/dashboard/index/page.js'
const origContent = await next.readFile(filePath)
try {
const browser = await next.browser('/dashboard/index')
expect(await browser.elementByCss('p').text()).toContain(
'hello from app/dashboard/index'
)
await next.patchFile(
filePath,
origContent.replace('hello from', 'swapped from')
)
await check(() => browser.elementByCss('p').text(), /swapped from/)
} finally {
await next.patchFile(filePath, origContent)
}
})
it('should HMR correctly for client component', async () => {
const filePath = 'app/client-component-route/page.js'
const origContent = await next.readFile(filePath)
try {
const browser = await next.browser('/client-component-route')
const ssrInitial = await next.render('/client-component-route')
expect(ssrInitial).toContain('hello from app/client-component-route')
expect(await browser.elementByCss('p').text()).toContain(
'hello from app/client-component-route'
)
await next.patchFile(
filePath,
origContent.replace('hello from', 'swapped from')
)
await check(() => browser.elementByCss('p').text(), /swapped from/)
const ssrUpdated = await next.render('/client-component-route')
expect(ssrUpdated).toContain('swapped from')
await next.patchFile(filePath, origContent)
await check(() => browser.elementByCss('p').text(), /hello from/)
expect(await next.render('/client-component-route')).toContain(
'hello from'
)
} finally {
await next.patchFile(filePath, origContent)
}
})
// TODO: investigate flakey behavior with this test case
it.skip('should HMR correctly when changing the component type', async () => {
const filePath = 'app/dashboard/page/page.jsx'
const origContent = await next.readFile(filePath)
try {
const browser = await next.browser('/dashboard/page')
expect(await browser.elementByCss('p').text()).toContain(
'hello dashboard/page!'
)
// Test HMR with server component
await next.patchFile(
filePath,
origContent.replace(
'hello dashboard/page!',
'hello dashboard/page in server component!'
)
)
await check(
() => browser.elementByCss('p').text(),
/in server component/
)
// Change to client component
await next.patchFile(
filePath,
origContent
.replace("// 'use client'", "'use client'")
.replace(
'hello dashboard/page!',
'hello dashboard/page in client component!'
)
)
await check(
() => browser.elementByCss('p').text(),
/in client component/
)
// Change back to server component
await next.patchFile(
filePath,
origContent.replace(
'hello dashboard/page!',
'hello dashboard/page in server component2!'
)
)
await check(
() => browser.elementByCss('p').text(),
/in server component2/
)
// Change to client component again
await next.patchFile(
filePath,
origContent
.replace("// 'use client'", "'use client'")
.replace(
'hello dashboard/page!',
'hello dashboard/page in client component2!'
)
)
await check(
() => browser.elementByCss('p').text(),
/in client component2/
)
} finally {
await next.patchFile(filePath, origContent)
}
})
})
}
describe('searchParams prop', () => {
describe('client component', () => {
it('should have the correct search params', async () => {
const $ = await next.render$(
'/search-params-prop?first=value&second=other%20value&third'
)
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
it('should have the correct search params on rewrite', async () => {
const $ = await next.render$('/search-params-prop-rewrite')
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
it('should have the correct search params on middleware rewrite', async () => {
const $ = await next.render$('/search-params-prop-middleware-rewrite')
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
})
describe('server component', () => {
it('should have the correct search params', async () => {
const $ = await next.render$(
'/search-params-prop/server?first=value&second=other%20value&third'
)
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
it('should have the correct search params on rewrite', async () => {
const $ = await next.render$('/search-params-prop-server-rewrite')
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
it('should have the correct search params on middleware rewrite', async () => {
const $ = await next.render$(
'/search-params-prop-server-middleware-rewrite'
)
const el = $('#params')
expect(el.attr('data-param-first')).toBe('value')
expect(el.attr('data-param-second')).toBe('other value')
expect(el.attr('data-param-third')).toBe('')
expect(el.attr('data-param-not-real')).toBe('N/A')
})
})
})
describe('template component', () => {
it('should render the template that holds state in a client component and reset on navigation', async () => {
const browser = await next.browser('/template/clientcomponent')
expect(await browser.elementByCss('h1').text()).toBe('Template 0')
await browser.elementByCss('button').click()
expect(await browser.elementByCss('h1').text()).toBe('Template 1')
await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#other-page')
expect(await browser.elementByCss('h1').text()).toBe('Template 0')
await browser.elementByCss('button').click()
expect(await browser.elementByCss('h1').text()).toBe('Template 1')
await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#page')
expect(await browser.elementByCss('h1').text()).toBe('Template 0')
})
// TODO-APP: disable failing test and investigate later
;(isNextDev ||
// When PPR is enabled, the shared layouts re-render because we prefetch
// from the root. This will be addressed before GA.
isPPREnabledByDefault
? it.skip
: it)(
'should render the template that is a server component and rerender on navigation',
async () => {
const browser = await next.browser('/template/servercomponent')
// eslint-disable-next-line jest/no-standalone-expect
expect(await browser.elementByCss('h1').text()).toStartWith('Template')
const currentTime = await browser
.elementByCss('#performance-now')
.text()
await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#other-page')
// eslint-disable-next-line jest/no-standalone-expect
expect(await browser.elementByCss('h1').text()).toStartWith('Template')
// template should rerender on navigation even when it's a server component
// eslint-disable-next-line jest/no-standalone-expect
expect(await browser.elementByCss('#performance-now').text()).toBe(
currentTime
)
await browser.elementByCss('#link').click()
await browser.waitForElementByCss('#page')
// eslint-disable-next-line jest/no-standalone-expect
expect(await browser.elementByCss('#performance-now').text()).toBe(
currentTime
)
}
)
})
describe('known bugs', () => {
describe('should support React cache', () => {
it('server component', async () => {
const browser = await next.browser('/react-cache/server-component')
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
expect(val1).toBe(val2)
})
it('server component client-navigation', async () => {
const browser = await next.browser('/react-cache')
await browser
.elementByCss('#to-server-component')
.click()
.waitForElementByCss('#value-1', 10000)
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
expect(val1).toBe(val2)
})
it('client component', async () => {
const browser = await next.browser('/react-cache/client-component')
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
// React.cache is not supported in client components.
expect(val1).not.toBe(val2)
})
it('client component client-navigation', async () => {
const browser = await next.browser('/react-cache')
await browser
.elementByCss('#to-client-component')
.click()
.waitForElementByCss('#value-1', 10000)
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
// React.cache is not supported in client components.
expect(val1).not.toBe(val2)
})
it('middleware overriding headers', async () => {
const browser = await next.browser('/searchparams-normalization-bug')
await browser.eval(`window.didFullPageTransition = 'no'`)
expect(await browser.elementByCss('#header-empty').text()).toBe(
'Header value: empty'
)
expect(
await browser
.elementByCss('#button-a')
.click()
.waitForElementByCss('#header-a')
.text()
).toBe('Header value: a')
expect(
await browser
.elementByCss('#button-b')
.click()
.waitForElementByCss('#header-b')
.text()
).toBe('Header value: b')
expect(
await browser
.elementByCss('#button-c')
.click()
.waitForElementByCss('#header-c')
.text()
).toBe('Header value: c')
expect(await browser.eval(`window.didFullPageTransition`)).toBe('no')
})
})
describe('should support React fetch instrumentation', () => {
it('server component', async () => {
// trigger compilation of 404 here first.
// Any other page being compiled between refresh of a page would get us fresh modules i.e. not catch previous regressions where we restored the wrong fetch.
await next.browser('/_not-found')
const browser = await next.browser('/react-fetch/server-component')
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
expect(val1).toBe(val2)
await browser.refresh()
const val1AfterRefresh = await browser.elementByCss('#value-1').text()
const val2AfterRefresh = await browser.elementByCss('#value-2').text()
expect(val1AfterRefresh).toBe(val2AfterRefresh)
})
it('server component client-navigation', async () => {
const browser = await next.browser('/react-fetch')
await browser
.elementByCss('#to-server-component')
.click()
.waitForElementByCss('#value-1', 10000)
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
// TODO: enable when fetch cache is enabled in dev
if (!isNextDev) {
expect(val1).toBe(val2)
}
})
// TODO-APP: React doesn't have fetch deduping for client components yet.
it.skip('client component', async () => {
const browser = await next.browser('/react-fetch/client-component')
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
expect(val1).toBe(val2)
})
// TODO-APP: React doesn't have fetch deduping for client components yet.
it.skip('client component client-navigation', async () => {
const browser = await next.browser('/react-fetch')
await browser
.elementByCss('#to-client-component')
.click()
.waitForElementByCss('#value-1', 10000)
const val1 = await browser.elementByCss('#value-1').text()
const val2 = await browser.elementByCss('#value-2').text()
expect(val1).toBe(val2)
})
})
it('should not share flight data between requests', async () => {
const fetches = await Promise.all(
[...new Array(5)].map(() => next.render('/loading-bug/electronics'))
)
for (const text of fetches) {
const $ = cheerio.load(text)
expect($('#category-id').text()).toBe('electronicsabc')
}
})
it('should handle router.refresh without resetting state', async () => {
const browser = await next.browser(
'/navigation/refresh/navigate-then-refresh-bug'
)
await browser
.elementByCss('#to-route')
// Navigate to the page
.click()
// Wait for new page to be loaded
.waitForElementByCss('#refresh-page')
// Click the refresh button to trigger a refresh
.click()
// Wait for element that is shown when refreshed and verify text
expect(await browser.waitForElementByCss('#refreshed').text()).toBe(
'Refreshed page successfully!'
)
expect(
await browser.eval(
`window.getComputedStyle(document.querySelector('h1')).backgroundColor`
)
).toBe('rgb(34, 139, 34)')
})
it('should handle as on next/link', async () => {
const browser = await next.browser('/link-with-as')
expect(
await browser
.elementByCss('#link-to-info-123')
.click()
.waitForElementByCss('#message')
.text()
).toBe(`hello from app/dashboard/deployments/info/[id]. ID is: 123`)
})
it('should handle next/link back to initially loaded page', async () => {
const browser = await next.browser('/linking/about')
expect(
await browser
.elementByCss('a[href="/linking"]')
.click()
.waitForElementByCss('#home-page')
.text()
).toBe(`Home page`)
expect(
await browser
.elementByCss('a[href="/linking/about"]')
.click()
.waitForElementByCss('#about-page')
.text()
).toBe(`About page`)
})
it('should not do additional pushState when already on the page', async () => {
const browser = await next.browser('/linking/about')
const goToLinkingPage = async () => {
expect(
await browser
.elementByCss('a[href="/linking"]')
.click()
.waitForElementByCss('#home-page')
.text()
).toBe(`Home page`)
}
await goToLinkingPage()
await waitFor(1000)
await goToLinkingPage()
await waitFor(1000)
await goToLinkingPage()
await waitFor(1000)
expect(
await browser.back().waitForElementByCss('#about-page', 2000).text()
).toBe(`About page`)
})
})
describe('next/script', () => {
if (!isNextDeploy) {
it('should support next/script and render in correct order', async () => {
const browser = await next.browser('/script')
// Wait for lazyOnload scripts to be ready.
await check(async () => {
expect(await browser.eval(`window._script_order`)).toStrictEqual([
1,
1.5,
2,
2.5,
'render',
3,
4,
])
return 'yes'
}, 'yes')
})
it('should pass on extra props for beforeInteractive scripts with a src prop', async () => {
const browser = await next.browser('/script')
const foundProps = await browser.eval(
`document.querySelector('#script-with-src-noop-test').getAttribute('data-extra-prop')`
)
expect(foundProps).toBe('script-with-src')
})
it('should pass on extra props for beforeInteractive scripts without a src prop', async () => {
const browser = await next.browser('/script')
const foundProps = await browser.eval(
`document.querySelector('#script-without-src-noop-test-dangerouslySetInnerHTML').getAttribute('data-extra-prop')`
)
expect(foundProps).toBe('script-without-src')
})
}
it('should insert preload tags for beforeInteractive and afterInteractive scripts', async () => {
const html = await next.render('/script')
const $ = cheerio.load(html)
const scriptPreloads = $(
'link[rel="preload"][as="script"][href^="/test"]'
)
const expectedHrefs = new Set(['/test1.js', '/test2.js', '/test3.js'])
expect(scriptPreloads.length).toBe(3)
scriptPreloads.each((i, el) => {
expect(expectedHrefs.has(el.attribs.href)).toBe(true)
expectedHrefs.delete(el.attribs.href)
})
// test4.js has lazyOnload which doesn't need to be preloaded
const lazyPreloads = $(
'link[rel="preload"][as="script"][href="/test4.js"]'
)
expect(lazyPreloads.length).toBe(0)
})
it('should load stylesheets for next/scripts', async () => {
const html = await next.render('/script')
const $ = cheerio.load(html)
expect($('link[href="/style3.css"]').length).toBe(1)
expect($('link[href="/style1a.css"]').length).toBe(1)
expect($('link[href="/style1b.css"]').length).toBe(1)
})
it('should pass `nonce`', async () => {
const html = await next.render('/script-nonce')
const $ = cheerio.load(html)
const scripts = $('script, link[rel="preload"][as="script"]')
scripts.each((_, element) => {
expect(element.attribs.nonce).toBeTruthy()
})
if (!isNextDev) {
const browser = await next.browser('/script-nonce')
await retry(async () => {
await browser.elementByCss('#get-order').click()
const order = JSON.parse(await browser.elementByCss('#order').text())
expect(order?.length).toBe(2)
})
}
})
it('should pass manual `nonce`', async () => {
const html = await next.render('/script-manual-nonce')
const $ = cheerio.load(html)
let scripts = $('script, link[rel="preload"][as="script"]')
scripts = scripts.filter((_, element) =>
(element.attribs.src || element.attribs.href)?.startsWith('/test')
)
expect(scripts.length).toBeGreaterThan(0)
scripts.each((_, element) => {
expect(element.attribs.nonce).toBeTruthy()
})
if (!isNextDev) {
const browser = await next.browser('/script-manual-nonce')
await retry(async () => {
await browser.elementByCss('#get-order').click()
const order = JSON.parse(await browser.elementByCss('#order').text())
expect(order?.length).toBe(2)
})
}
})
it('should pass manual `nonce` pages', async () => {
const html = await next.render('/pages-script-manual-nonce')
const $ = cheerio.load(html)
let scripts = $('script, link[rel="preload"][as="script"]')
scripts = scripts.filter((_, element) =>
(element.attribs.src || element.attribs.href)?.startsWith('/test')
)
expect(scripts.length).toBeGreaterThan(0)
scripts.each((_, element) => {
expect(element.attribs.nonce).toBeTruthy()
})
if (!isNextDev) {
await retry(async () => {
const browser = await next.browser('/pages-script-manual-nonce')
await browser.elementByCss('#get-order').click()
const order = JSON.parse(await browser.elementByCss('#order').text())
expect(order?.length).toBe(2)
})
}
})
it('should pass nonce when using next/font', async () => {
const html = await next.render('/script-nonce/with-next-font')
const $ = cheerio.load(html)
const scripts = $('script, link[rel="preload"][as="script"]')
scripts.each((_, element) => {
expect(element.attribs.nonce).toBeTruthy()
})
})
})
describe('data fetch with response over 16KB with chunked encoding', () => {
it('should load page when fetching a large amount of data', async () => {
const browser = await next.browser('/very-large-data-fetch')
expect(await (await browser.waitForElementByCss('#done')).text()).toBe(
'Hello world'
)
expect(await browser.elementByCss('p').text()).toBe('item count 128000')
})
})
describe('bootstrap scripts', () => {
it('should only bootstrap with one script, prinitializing the rest', async () => {
const html = await next.render('/bootstrap')
const $ = cheerio.load(html)
// We assume a minimum of 2 scripts, webpack runtime + main-app
expect($('script[async]').length).toBeGreaterThan(1)
expect($('body').find('script[async]').length).toBe(1)
})
// This test is here to ensure that we don't accidentally turn CSP off
// for the prod version.
it('should successfully bootstrap even when using CSP', async () => {
// This path has a nonce applied in middleware
const browser = await next.browser('/bootstrap/with-nonce')
const response = await next.fetch('/bootstrap/with-nonce')
// We expect this page to response with CSP headers requiring a nonce for scripts
expect(response.headers.get('content-security-policy')).toEqual(
isNextDev
? "script-src 'nonce-my-random-nonce' 'strict-dynamic' 'unsafe-eval';"
: "script-src 'nonce-my-random-nonce' 'strict-dynamic';"
)
// We expect to find the updated text which demonstrates our app
// was able to bootstrap successfully (scripts run)
await retry(async () => {
expect(await browser.elementByCss('#val').text()).toEqual('[[updated]]')
})
})
})
// this one comes at the end to not change behavior from above
// assertions with compile mode specifically
// consider breaking out into separate fixture if we expand this any more
if (process.env.NEXT_EXPERIMENTAL_COMPILE) {
it('should run generate command correctly', async () => {
await next.stop()
next.buildCommand = `pnpm next build --experimental-build-mode=generate`
await next.start()
let browser = await next.browser('/')
expect(await browser.elementByCss('#my-env').text()).toBe(
next.env.NEXT_PUBLIC_TEST_ID
)
expect(await browser.elementByCss('#my-other-env').text()).toBe(
`${next.env.NEXT_PUBLIC_TEST_ID}-suffix`
)
browser = await next.browser('/dashboard/deployments/123')
expect(await browser.elementByCss('#my-env').text()).toBe(
next.env.NEXT_PUBLIC_TEST_ID
)
expect(await browser.elementByCss('#my-other-env').text()).toBe(
`${next.env.NEXT_PUBLIC_TEST_ID}-suffix`
)
})
}
})
|