Unverified Commit 7a2ec416 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Refactor more tests to createNextDescribe (#44104)

parent b8ae447b
......@@ -5,11 +5,6 @@ import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path'
describe('ReactRefresh app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -5,11 +5,6 @@ import path from 'path'
// TODO-APP: Investigate snapshot mismatch
describe('ReactRefreshLogBox app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -7,11 +7,6 @@ import path from 'path'
// TODO: figure out why snapshots mismatch on GitHub actions
// specifically but work in docker and locally
describe.skip('ReactRefreshLogBox app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -6,11 +6,6 @@ import { check } from 'next-test-utils'
import path from 'path'
describe('ReactRefreshLogBox app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -6,11 +6,6 @@ import path from 'path'
// TODO: re-enable these tests after figuring out what is causing
// them to be so unreliable in CI
describe.skip('ReactRefreshLogBox app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -4,11 +4,6 @@ import { NextInstance } from 'test/lib/next-modes/base'
import { sandbox } from './helpers'
describe('ReactRefreshModule app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -5,11 +5,6 @@ import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path'
describe('ReactRefreshRegression app', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -6,11 +6,6 @@ import path from 'path'
import { check } from 'next-test-utils'
describe('Error Overlay for server components', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
......
......@@ -532,14 +532,10 @@ describe('basic HMR', () => {
)
)
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
expect(await hasRedbox(browser)).toBe(true)
// TODO: Replace this when webpack 5 is the default
expect(await getRedboxHeader(browser)).toMatch(
`Objects are not valid as a React child (found: ${
isReact17 ? '/search/' : '[object RegExp]'
}). If you meant to render a collection of children, use an array instead.`
`Objects are not valid as a React child (found: [object RegExp]). If you meant to render a collection of children, use an array instead.`
)
await next.patchFile(aboutPage, aboutContent)
......
......@@ -597,14 +597,10 @@ describe('basic HMR', () => {
)
)
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
expect(await hasRedbox(browser)).toBe(true)
// TODO: Replace this when webpack 5 is the default
expect(await getRedboxHeader(browser)).toMatch(
`Objects are not valid as a React child (found: ${
isReact17 ? '/search/' : '[object RegExp]'
}). If you meant to render a collection of children, use an array instead.`
`Objects are not valid as a React child (found: [object RegExp]). If you meant to render a collection of children, use an array instead.`
)
await next.patchFile(aboutPage, aboutContent)
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import webdriver from 'next-webdriver'
import { createNextDescribe } from 'e2e-utils'
import path from 'path'
import { readJSON } from 'fs-extra'
describe('app-dir alias handling', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-alias')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
packageJson: {
type: 'module',
},
createNextDescribe(
'app-dir alias handling',
{
files: path.join(__dirname, 'app-alias'),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
packageJson: {
type: 'module',
},
skipDeployment: true,
},
({ next, isNextDev }) => {
it('should handle typescript paths alias correctly', async () => {
const html = await next.render('/button')
expect(html).toContain('click</button>')
})
})
afterAll(() => next.destroy())
it('should handle typescript paths alias correctly', async () => {
const html = await renderViaHTTP(next.url, '/button')
expect(html).toContain('click</button>')
})
it('should resolve css imports from outside with src folder presented', async () => {
const browser = await webdriver(next.url, '/button')
const fontSize = await browser
.elementByCss('button')
.getComputedCss('font-size')
expect(fontSize).toBe('50px')
})
it('should resolve css imports from outside with src folder presented', async () => {
const browser = await next.browser('/button')
const fontSize = await browser
.elementByCss('button')
.getComputedCss('font-size')
expect(fontSize).toBe('50px')
})
if (!(global as any).isNextDev) {
it('should generate app-build-manifest correctly', async () => {
// Remove other page CSS files:
const manifest = await readJSON(
path.join(next.testDir, '.next', 'app-build-manifest.json')
)
if (!isNextDev) {
it('should generate app-build-manifest correctly', async () => {
// Remove other page CSS files:
const manifest = await next.readJSON(
path.join('.next', 'app-build-manifest.json')
)
expect(manifest.pages).not.toBeEmptyObject()
})
expect(manifest.pages).not.toBeEmptyObject()
})
}
}
})
)
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
import path from 'path'
describe('app-dir global edge configuration', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-edge-global')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
createNextDescribe(
'app-dir global edge configuration',
{
files: path.join(__dirname, 'app-edge-global'),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
skipDeployment: true,
},
({ next }) => {
it('should handle edge only routes', async () => {
const html = await next.render('/app-edge')
expect(html).toContain('<p>Edge!</p>')
})
})
afterAll(() => next.destroy())
it('should handle edge only routes', async () => {
const appHtml = await renderViaHTTP(next.url, '/app-edge')
expect(appHtml).toContain('<p>Edge!</p>')
})
})
}
)
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { check, renderViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
import path from 'path'
describe('app-dir edge SSR', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
createNextDescribe(
'app-dir edge SSR',
{
files: path.join(__dirname, 'app-edge'),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
skipDeployment: true,
},
({ next }) => {
it('should handle edge only routes', async () => {
const appHtml = await next.render('/app-edge')
expect(appHtml).toContain('<p>Edge!</p>')
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-edge')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
const pageHtml = await next.render('/pages-edge')
expect(pageHtml).toContain('<p>pages-edge-ssr</p>')
})
})
afterAll(() => next.destroy())
it('should handle edge only routes', async () => {
const appHtml = await renderViaHTTP(next.url, '/app-edge')
expect(appHtml).toContain('<p>Edge!</p>')
const pageHtml = await renderViaHTTP(next.url, '/pages-edge')
expect(pageHtml).toContain('<p>pages-edge-ssr</p>')
})
if ((globalThis as any).isNextDev) {
it('should resolve module without error in edge runtime', async () => {
const logs = []
next.on('stderr', (log) => {
logs.push(log)
if ((globalThis as any).isNextDev) {
it('should resolve module without error in edge runtime', async () => {
const logs = []
next.on('stderr', (log) => {
logs.push(log)
})
await next.render('app-edge')
expect(
logs.some((log) => log.includes(`Attempted import error:`))
).toBe(false)
})
await renderViaHTTP(next.url, 'app-edge')
expect(logs.some((log) => log.includes(`Attempted import error:`))).toBe(
false
)
})
it('should handle edge rsc hmr', async () => {
const pageFile = 'app/app-edge/page.tsx'
const content = await next.readFile(pageFile)
it('should handle edge rsc hmr', async () => {
const pageFile = 'app/app-edge/page.tsx'
const content = await next.readFile(pageFile)
// Update rendered content
const updatedContent = content.replace('Edge!', 'edge-hmr')
await next.patchFile(pageFile, updatedContent)
await check(async () => {
const html = await renderViaHTTP(next.url, '/app-edge')
return html
}, /edge-hmr/)
// Update rendered content
const updatedContent = content.replace('Edge!', 'edge-hmr')
await next.patchFile(pageFile, updatedContent)
await check(async () => {
const html = await next.render('/app-edge')
return html
}, /edge-hmr/)
// Revert
await next.patchFile(pageFile, content)
await check(async () => {
const html = await renderViaHTTP(next.url, '/app-edge')
return html
}, /Edge!/)
})
} else {
// Production tests
it('should generate matchers correctly in middleware manifest', async () => {
const manifest = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')
)
expect(manifest.functions['/(group)/group/page'].matchers).toEqual([
{
regexp: '^/group$',
},
])
})
// Revert
await next.patchFile(pageFile, content)
await check(async () => {
const html = await next.render('/app-edge')
return html
}, /Edge!/)
})
} else {
// Production tests
it('should generate matchers correctly in middleware manifest', async () => {
const manifest = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')
)
expect(manifest.functions['/(group)/group/page'].matchers).toEqual([
{
regexp: '^/group$',
},
])
})
}
}
})
)
This diff is collapsed.
/* eslint-env jest */
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { createNextDescribe, FileRef } from 'e2e-utils'
import cheerio from 'cheerio'
import path from 'path'
describe('app-dir with middleware', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-middleware')),
})
})
describe.each([
{
title: 'Serverless Functions',
path: '/api/dump-headers-serverless',
toJson: (res: Response) => res.json(),
},
{
title: 'Edge Functions',
path: '/api/dump-headers-edge',
toJson: (res: Response) => res.json(),
},
{
title: 'next/headers',
path: '/headers',
toJson: async (res: Response) => {
const $ = cheerio.load(await res.text())
return JSON.parse($('#headers').text())
createNextDescribe(
'app-dir with middleware',
{
files: path.join(__dirname, 'app-middleware'),
skipDeployment: true,
},
({ next }) => {
describe.each([
{
title: 'Serverless Functions',
path: '/api/dump-headers-serverless',
toJson: (res: Response) => res.json(),
},
},
])('Mutate request headers for $title', ({ path, toJson }) => {
it(`Adds new headers`, async () => {
const res = await fetchViaHTTP(next.url, path, null, {
headers: {
'x-from-client': 'hello-from-client',
},
})
expect(await toJson(res)).toMatchObject({
'x-from-client': 'hello-from-client',
'x-from-middleware': 'hello-from-middleware',
})
})
it(`Deletes headers`, async () => {
const res = await fetchViaHTTP(
next.url,
path,
{
'remove-headers': 'x-from-client1,x-from-client2',
{
title: 'Edge Functions',
path: '/api/dump-headers-edge',
toJson: (res: Response) => res.json(),
},
{
title: 'next/headers',
path: '/headers',
toJson: async (res: Response) => {
const $ = cheerio.load(await res.text())
return JSON.parse($('#headers').text())
},
{
},
])('Mutate request headers for $title', ({ path, toJson }) => {
it(`Adds new headers`, async () => {
const res = await next.fetch(path, null, {
headers: {
'x-from-client1': 'hello-from-client',
'X-From-Client2': 'hello-from-client',
'x-from-client': 'hello-from-client',
},
}
)
const json = await toJson(res)
expect(json).not.toHaveProperty('x-from-client1')
expect(json).not.toHaveProperty('X-From-Client2')
expect(json).toMatchObject({
'x-from-middleware': 'hello-from-middleware',
})
expect(await toJson(res)).toMatchObject({
'x-from-client': 'hello-from-client',
'x-from-middleware': 'hello-from-middleware',
})
})
// Should not be included in response headers.
expect(res.headers.get('x-middleware-override-headers')).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-middleware')
).toBeNull()
expect(res.headers.get('x-middleware-request-x-from-client1')).toBeNull()
expect(res.headers.get('x-middleware-request-x-from-client2')).toBeNull()
})
it(`Updates headers`, async () => {
const res = await fetchViaHTTP(
next.url,
path,
{
'update-headers':
'x-from-client1=new-value1,x-from-client2=new-value2',
},
{
headers: {
'x-from-client1': 'old-value1',
'X-From-Client2': 'old-value2',
'x-from-client3': 'old-value3',
it(`Deletes headers`, async () => {
const res = await next.fetch(
path,
{
'remove-headers': 'x-from-client1,x-from-client2',
},
}
)
expect(await toJson(res)).toMatchObject({
'x-from-client1': 'new-value1',
'x-from-client2': 'new-value2',
'x-from-client3': 'old-value3',
'x-from-middleware': 'hello-from-middleware',
{
headers: {
'x-from-client1': 'hello-from-client',
'X-From-Client2': 'hello-from-client',
},
}
)
const json = await toJson(res)
expect(json).not.toHaveProperty('x-from-client1')
expect(json).not.toHaveProperty('X-From-Client2')
expect(json).toMatchObject({
'x-from-middleware': 'hello-from-middleware',
})
// Should not be included in response headers.
expect(res.headers.get('x-middleware-override-headers')).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-middleware')
).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-client1')
).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-client2')
).toBeNull()
})
// Should not be included in response headers.
expect(res.headers.get('x-middleware-override-headers')).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-middleware')
).toBeNull()
expect(res.headers.get('x-middleware-request-x-from-client1')).toBeNull()
expect(res.headers.get('x-middleware-request-x-from-client2')).toBeNull()
expect(res.headers.get('x-middleware-request-x-from-client3')).toBeNull()
it(`Updates headers`, async () => {
const res = await next.fetch(
path,
{
'update-headers':
'x-from-client1=new-value1,x-from-client2=new-value2',
},
{
headers: {
'x-from-client1': 'old-value1',
'X-From-Client2': 'old-value2',
'x-from-client3': 'old-value3',
},
}
)
expect(await toJson(res)).toMatchObject({
'x-from-client1': 'new-value1',
'x-from-client2': 'new-value2',
'x-from-client3': 'old-value3',
'x-from-middleware': 'hello-from-middleware',
})
// Should not be included in response headers.
expect(res.headers.get('x-middleware-override-headers')).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-middleware')
).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-client1')
).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-client2')
).toBeNull()
expect(
res.headers.get('x-middleware-request-x-from-client3')
).toBeNull()
})
})
})
})
describe('app dir middleware without pages dir', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
afterAll(() => next.destroy())
beforeAll(async () => {
next = await createNext({
files: {
app: new FileRef(path.join(__dirname, 'app-middleware/app')),
'next.config.js': new FileRef(
path.join(__dirname, 'app-middleware/next.config.js')
),
'middleware.js': `
import { NextResponse } from 'next/server'
export async function middleware(request) {
return new NextResponse('redirected')
}
export const config = {
matcher: '/headers'
}
`,
},
)
createNextDescribe(
'app dir middleware without pages dir',
{
files: {
app: new FileRef(path.join(__dirname, 'app-middleware/app')),
'next.config.js': new FileRef(
path.join(__dirname, 'app-middleware/next.config.js')
),
'middleware.js': `
import { NextResponse } from 'next/server'
export async function middleware(request) {
return new NextResponse('redirected')
}
export const config = {
matcher: '/headers'
}
`,
},
skipDeployment: true,
},
({ next }) => {
// eslint-disable-next-line jest/no-identical-title
it('Updates headers', async () => {
const html = await next.render('/headers')
expect(html).toContain('redirected')
})
})
it(`Updates headers`, async () => {
const html = await renderViaHTTP(next.url, '/headers')
expect(html).toContain('redirected')
})
})
}
)
This diff is collapsed.
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
import path from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
describe('app-dir assetPrefix handling', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'asset-prefix')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
skipStart: true,
createNextDescribe(
'app-dir assetPrefix handling',
{
files: path.join(__dirname, 'asset-prefix'),
skipDeployment: true,
},
({ next }) => {
it('should redirect route when requesting it directly', async () => {
const res = await next.fetch(
'/a/',
{},
{
redirect: 'manual',
}
)
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(next.url + '/a')
})
await next.start()
})
afterAll(() => next.destroy())
it('should redirect route when requesting it directly', async () => {
const res = await fetchViaHTTP(
next.url,
'/a/',
{},
{
redirect: 'manual',
}
)
expect(res.status).toBe(308)
expect(res.headers.get('location')).toBe(next.url + '/a')
})
it('should render link', async () => {
const html = await renderViaHTTP(next.url, '/')
const $ = cheerio.load(html)
expect($('#to-a-trailing-slash').attr('href')).toBe('/a')
})
it('should render link', async () => {
const $ = await next.render$('/')
expect($('#to-a-trailing-slash').attr('href')).toBe('/a')
})
it('should redirect route when requesting it directly by browser', async () => {
const browser = await webdriver(next.url, '/a')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
it('should redirect route when requesting it directly by browser', async () => {
const browser = await next.browser('/a')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
it('should redirect route when clicking link', async () => {
const browser = await webdriver(next.url, '/')
await browser
.elementByCss('#to-a-trailing-slash')
.click()
.waitForElementByCss('#a-page')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
})
it('should redirect route when clicking link', async () => {
const browser = await next.browser('/')
await browser
.elementByCss('#to-a-trailing-slash')
.click()
.waitForElementByCss('#a-page')
expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page')
})
}
)
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
import { createNextDescribe } from 'e2e-utils'
import path from 'path'
describe('async-component-preload', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'async-component-preload')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
createNextDescribe(
'async-component-preload',
{
files: path.join(__dirname, 'async-component-preload'),
skipDeployment: true,
},
({ next }) => {
it('should handle redirect in an async page', async () => {
const browser = await next.browser('/')
expect(await browser.waitForElementByCss('#success').text()).toBe(
'Success'
)
})
})
afterAll(() => next.destroy())
it('should handle redirect in an async page', async () => {
const browser = await webdriver(next.url, '/')
expect(await browser.waitForElementByCss('#success').text()).toBe('Success')
})
})
}
)
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNextDescribe } from 'e2e-utils'
import path from 'path'
import webdriver from 'next-webdriver'
// TODO-APP: fix test as it's failing randomly
describe.skip('app-dir back button download bug', () => {
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
createNextDescribe(
'app-dir back button download bug',
{
files: path.join(__dirname, 'back-button-download-bug'),
skipDeployment: true,
},
({ next }) => {
it('should redirect route when clicking link', async () => {
const browser = await next.browser('/')
const text = await browser
.elementByCss('#to-post-1')
.click()
.waitForElementByCss('#post-page')
.text()
expect(text).toBe('This is the post page')
let next: NextInstance
await browser.back()
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'back-button-download-bug')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
skipStart: true,
})
await next.start()
})
afterAll(() => next.destroy())
it('should redirect route when clicking link', async () => {
const browser = await webdriver(next.url, '/')
const text = await browser
.elementByCss('#to-post-1')
.click()
.waitForElementByCss('#post-page')
.text()
expect(text).toBe('This is the post page')
await browser.back()
expect(await browser.waitForElementByCss('#home-page').text()).toBe('Home!')
})
expect(await browser.waitForElementByCss('#home-page').text()).toBe(
'Home!'
)
})
}
)
})
import path from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
import { check } from 'next-test-utils'
describe('app-dir create root layout', () => {
......@@ -12,10 +11,6 @@ describe('app-dir create root layout', () => {
return
}
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
if (isDev) {
......@@ -39,7 +34,7 @@ describe('app-dir create root layout', () => {
it('create root layout', async () => {
const outputIndex = next.cliOutput.length
const browser = await webdriver(next.url, '/route')
const browser = await next.browser('/route')
expect(await browser.elementById('page-text').text()).toBe(
'Hello world!'
......@@ -101,7 +96,7 @@ describe('app-dir create root layout', () => {
it('create root layout', async () => {
const outputIndex = next.cliOutput.length
const browser = await webdriver(next.url, '/')
const browser = await next.browser('/')
expect(await browser.elementById('page-text').text()).toBe(
'Hello world'
......@@ -168,7 +163,7 @@ describe('app-dir create root layout', () => {
it('create root layout', async () => {
const outputIndex = next.cliOutput.length
const browser = await webdriver(next.url, '/route/second/inner')
const browser = await next.browser('/route/second/inner')
expect(await browser.elementById('page-text').text()).toBe(
'Hello world'
......@@ -236,7 +231,7 @@ describe('app-dir create root layout', () => {
it('create root layout', async () => {
const outputIndex = next.cliOutput.length
const browser = await webdriver(next.url, '/')
const browser = await next.browser('/')
expect(await browser.elementById('page-text').text()).toBe(
'Hello world!'
......
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNextDescribe } from 'e2e-utils'
import { getRedboxDescription, hasRedbox } from 'next-test-utils'
import path from 'path'
import webdriver from 'next-webdriver'
describe('dynamic-href', () => {
const isDev = (global as any).isNextDev
if ((global as any).isNextDeploy) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'dynamic-href')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
})
})
afterAll(() => next.destroy())
if (isDev) {
it('should error when using dynamic href.pathname in app dir', async () => {
const browser = await webdriver(next.url, '/object')
// Error should show up
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"Error: Dynamic href \`/object/[slug]\` found in <Link> while using the \`/app\` router, this is not supported. Read more: https://nextjs.org/docs/messages/app-dir-dynamic-href"`
)
createNextDescribe(
'dynamic-href',
{
files: path.join(__dirname, 'dynamic-href'),
skipDeployment: true,
},
({ isNextDev: isDev, next }) => {
if (isDev) {
it('should error when using dynamic href.pathname in app dir', async () => {
const browser = await next.browser('/object')
// Error should show up
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"Error: Dynamic href \`/object/[slug]\` found in <Link> while using the \`/app\` router, this is not supported. Read more: https://nextjs.org/docs/messages/app-dir-dynamic-href"`
)
// Fix error
const pageContent = await next.readFile('app/object/page.js')
await next.patchFile(
'app/object/page.js',
pageContent.replace(
"pathname: '/object/[slug]'",
"pathname: '/object/slug'"
// Fix error
const pageContent = await next.readFile('app/object/page.js')
await next.patchFile(
'app/object/page.js',
pageContent.replace(
"pathname: '/object/[slug]'",
"pathname: '/object/slug'"
)
)
expect(await browser.waitForElementByCss('#link').text()).toBe(
'to slug'
)
)
expect(await browser.waitForElementByCss('#link').text()).toBe('to slug')
// Navigate to new page
await browser.elementByCss('#link').click()
expect(await browser.waitForElementByCss('#pathname').text()).toBe(
'/object/slug'
)
expect(await browser.elementByCss('#slug').text()).toBe('1')
})
// Navigate to new page
await browser.elementByCss('#link').click()
expect(await browser.waitForElementByCss('#pathname').text()).toBe(
'/object/slug'
)
expect(await browser.elementByCss('#slug').text()).toBe('1')
})
it('should error when using dynamic href in app dir', async () => {
const browser = await webdriver(next.url, '/string')
it('should error when using dynamic href in app dir', async () => {
const browser = await next.browser('/string')
// Error should show up
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"Error: Dynamic href \`/object/[slug]\` found in <Link> while using the \`/app\` router, this is not supported. Read more: https://nextjs.org/docs/messages/app-dir-dynamic-href"`
)
})
} else {
it('should not error on /object in prod', async () => {
const browser = await webdriver(next.url, '/object')
expect(await browser.elementByCss('#link').text()).toBe('to slug')
})
it('should not error on /string in prod', async () => {
const browser = await webdriver(next.url, '/string')
expect(await browser.elementByCss('#link').text()).toBe('to slug')
})
// Error should show up
expect(await hasRedbox(browser, true)).toBeTrue()
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(
`"Error: Dynamic href \`/object/[slug]\` found in <Link> while using the \`/app\` router, this is not supported. Read more: https://nextjs.org/docs/messages/app-dir-dynamic-href"`
)
})
} else {
it('should not error on /object in prod', async () => {
const browser = await next.browser('/object')
expect(await browser.elementByCss('#link').text()).toBe('to slug')
})
it('should not error on /string in prod', async () => {
const browser = await next.browser('/string')
expect(await browser.elementByCss('#link').text()).toBe('to slug')
})
}
}
})
)
import path from 'path'
import { getRedboxHeader, hasRedbox } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import webdriver from 'next-webdriver'
import { createNextDescribe } from 'e2e-utils'
describe('app dir - global error', () => {
let next: NextInstance
const isDev = (global as any).isNextDev
createNextDescribe(
'app dir - global error',
{
files: path.join(__dirname, './global-error'),
},
({ next, isNextDev }) => {
it('should trigger error component when an error happens during rendering', async () => {
const browser = await next.browser('/throw')
await browser
.waitForElementByCss('#error-trigger-button')
.elementByCss('#error-trigger-button')
.click()
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, './global-error')),
if (isNextDev) {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(/Error: Client error/)
} else {
await browser
expect(await browser.elementByCss('#error').text()).toBe(
'Error message: Client error'
)
}
})
})
afterAll(() => next.destroy())
it('should trigger error component when an error happens during rendering', async () => {
const browser = await webdriver(next.url, '/throw')
await browser
.waitForElementByCss('#error-trigger-button')
.elementByCss('#error-trigger-button')
.click()
if (isDev) {
expect(await hasRedbox(browser)).toBe(true)
expect(await getRedboxHeader(browser)).toMatch(/Error: Client error/)
} else {
await browser
expect(await browser.elementByCss('#error').text()).toBe(
'Error message: Client error'
)
}
})
})
}
)
import path from 'path'
import cheerio from 'cheerio'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'
import { createNextDescribe } from 'e2e-utils'
describe('app dir imports', () => {
if (process.env.NEXT_TEST_REACT_VERSION === '^17') {
it('should skip for react v17', () => {})
return
}
let next: NextInstance
function runTests() {
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'import')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
})
})
afterAll(() => next.destroy())
createNextDescribe(
'app dir imports',
{
files: path.join(__dirname, 'import'),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
},
({ next }) => {
;['js', 'jsx', 'ts', 'tsx'].forEach((ext) => {
it(`we can import all components from .${ext}`, async () => {
const html = await renderViaHTTP(next.url, `/${ext}`)
const $ = cheerio.load(html)
const $ = await next.render$(`/${ext}`)
expect($('#js').text()).toBe('CompJs')
})
})
}
runTests()
})
)
This diff is collapsed.
import path from 'path'
import { renderViaHTTP } from 'next-test-utils'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import cheerio from 'cheerio'
describe('app dir - layout params', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, './layout-params')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
})
})
afterAll(() => next.destroy())
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
if (isReact17) {
it('should skip tests for react 17', () => {})
return
}
describe('basic params', () => {
it('check layout without params get no params', async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')
const $ = cheerio.load(html)
const ids = ['#root-layout', '#lvl1-layout']
ids.forEach((divId) => {
const params = $(`${divId} > div`)
expect(params.length).toBe(0)
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app dir - layout params',
{
files: path.join(__dirname, './layout-params'),
dependencies: {
react: 'latest',
'react-dom': 'latest',
typescript: 'latest',
'@types/react': 'latest',
'@types/node': 'latest',
},
},
({ next }) => {
describe('basic params', () => {
it('check layout without params get no params', async () => {
const $ = await next.render$('/base/something/another')
const ids = ['#root-layout', '#lvl1-layout']
ids.forEach((divId) => {
const params = $(`${divId} > div`)
expect(params.length).toBe(0)
})
})
})
it("check layout renders just it's params", async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')
const $ = cheerio.load(html)
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-param1').text()).toBe('"something"')
})
it("check layout renders just it's params", async () => {
const $ = await next.render$('/base/something/another')
it('check topmost layout renders all params', async () => {
const html = await renderViaHTTP(next.url, '/base/something/another')
const $ = cheerio.load(html)
expect($('#lvl3-layout > div').length).toBe(2)
expect($('#lvl3-param1').text()).toBe('"something"')
expect($('#lvl3-param2').text()).toBe('"another"')
})
})
describe('catchall params', () => {
it('should give catchall params just to last layout', async () => {
const html = await renderViaHTTP(next.url, '/catchall/something/another')
const $ = cheerio.load(html)
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-param1').text()).toBe('"something"')
})
expect($(`#root-layout > div`).length).toBe(0)
it('check topmost layout renders all params', async () => {
const $ = await next.render$('/base/something/another')
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
expect($('#lvl3-layout > div').length).toBe(2)
expect($('#lvl3-param1').text()).toBe('"something"')
expect($('#lvl3-param2').text()).toBe('"another"')
})
})
it('should give optional catchall params just to last layout', async () => {
const html = await renderViaHTTP(
next.url,
'/optional-catchall/something/another'
)
describe('catchall params', () => {
it('should give catchall params just to last layout', async () => {
const $ = await next.render$('/catchall/something/another')
const $ = cheerio.load(html)
expect($(`#root-layout > div`).length).toBe(0)
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
})
expect($(`#root-layout > div`).length).toBe(0)
it('should give optional catchall params just to last layout', async () => {
const $ = await next.render$('/optional-catchall/something/another')
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
})
expect($(`#root-layout > div`).length).toBe(0)
it("should give empty optional catchall params won't give params to any layout", async () => {
const html = await renderViaHTTP(next.url, '/optional-catchall')
expect($('#lvl2-layout > div').length).toBe(1)
expect($('#lvl2-params').text()).toBe('["something","another"]')
})
const $ = cheerio.load(html)
it("should give empty optional catchall params won't give params to any layout", async () => {
const $ = await next.render$('/optional-catchall')
expect($(`#root-layout > div`).length).toBe(0)
expect($('#lvl2-layout > div').length).toBe(0)
expect($(`#root-layout > div`).length).toBe(0)
expect($('#lvl2-layout > div').length).toBe(0)
})
})
})
})
}
)
This diff is collapsed.
This diff is collapsed.
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { createNextDescribe } from 'e2e-utils'
import { waitFor } from 'next-test-utils'
import path from 'path'
import webdriver from 'next-webdriver'
describe('app dir prefetching', () => {
// TODO: re-enable for dev after https://vercel.slack.com/archives/C035J346QQL/p1663822388387959 is resolved (Sep 22nd 2022)
if ((global as any).isNextDeploy || (global as any).isNextDev) {
it('should skip next deploy for now', () => {})
return
}
let next: NextInstance
createNextDescribe(
'app dir prefetching',
{
files: path.join(__dirname, 'app-prefetch'),
skipDeployment: true,
},
({ next, isNextDev }) => {
// TODO: re-enable for dev after https://vercel.slack.com/archives/C035J346QQL/p1663822388387959 is resolved (Sep 22nd 2022)
if (isNextDev) {
it('should skip next deploy for now', () => {})
return
}
it('should show layout eagerly when prefetched with loading one level down', async () => {
const browser = await next.browser('/')
// Ensure the page is prefetched
await waitFor(1000)
const before = Date.now()
await browser
.elementByCss('#to-dashboard')
.click()
.waitForElementByCss('#dashboard-layout')
const after = Date.now()
const timeToComplete = after - before
expect(timeToComplete < 1000).toBe(true)
expect(await browser.elementByCss('#dashboard-layout').text()).toBe(
'Dashboard Hello World'
)
await browser.waitForElementByCss('#dashboard-page')
expect(await browser.waitForElementByCss('#dashboard-page').text()).toBe(
'Welcome to the dashboard'
)
})
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'app-prefetch')),
dependencies: {
react: 'latest',
'react-dom': 'latest',
},
skipStart: true,
it('should not have prefetch error for static path', async () => {
const browser = await next.browser('/')
await browser.eval('window.nd.router.prefetch("/dashboard/123")')
await waitFor(3000)
await browser.eval('window.nd.router.push("/dashboard/123")')
expect(next.cliOutput).not.toContain('ReferenceError')
expect(next.cliOutput).not.toContain('is not defined')
})
await next.start()
})
afterAll(() => next.destroy())
it('should show layout eagerly when prefetched with loading one level down', async () => {
const browser = await webdriver(next.url, '/')
// Ensure the page is prefetched
await waitFor(1000)
const before = Date.now()
await browser
.elementByCss('#to-dashboard')
.click()
.waitForElementByCss('#dashboard-layout')
const after = Date.now()
const timeToComplete = after - before
expect(timeToComplete < 1000).toBe(true)
expect(await browser.elementByCss('#dashboard-layout').text()).toBe(
'Dashboard Hello World'
)
await browser.waitForElementByCss('#dashboard-page')
expect(await browser.waitForElementByCss('#dashboard-page').text()).toBe(
'Welcome to the dashboard'
)
})
it('should not have prefetch error for static path', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.nd.router.prefetch("/dashboard/123")')
await waitFor(3000)
await browser.eval('window.nd.router.push("/dashboard/123")')
expect(next.cliOutput).not.toContain('ReferenceError')
expect(next.cliOutput).not.toContain('is not defined')
})
})
}
)
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -35,8 +35,7 @@ describe('transpile packages', () => {
afterAll(() => next.destroy())
const { isNextDeploy } = global as any
const isReact17 = process.env.NEXT_TEST_REACT_VERSION === '^17'
if (isNextDeploy || isReact17) {
if (isNextDeploy) {
it('should skip tests for next-deploy and react 17', () => {})
return
}
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment