Unverified Commit ee0f4831 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Migrate basic-basepath tests into basic (#44776)

Removes `test/development/basic-basepath` and migrates those tests into
`test/development/basic` instead. Currently the tests are duplicated but
`/basic-basepath` is just a subset of `/basic`.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

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

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 7fe9e035
This diff is collapsed.
export default () => {
return (
<div id="dynamic-component">
Dynamic Component
<style jsx>{`
div {
font-size: 100px;
}
`}</style>
</div>
)
}
export default function About() {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default function About2() {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default function About4() {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => {
return (
<div className="hmr-about-page">
<p>This is the about page.</p>
</div>
)
}
export default () => (
<div className="hmr-contact-page">
<p>This is the contact page.</p>
</div>
)
import React from 'react'
export default class Counter extends React.Component {
state = { count: 0 }
incr() {
const { count } = this.state
this.setState({ count: count + 1 })
}
render() {
return (
<div>
<p>COUNT: {this.state.count}</p>
<button onClick={() => this.incr()}>Increment</button>
</div>
)
}
}
import React from 'react'
export default class extends React.Component {
static getInitialProps() {
const error = new Error('an-expected-error-in-gip')
throw error
}
render() {
return <div>Hello</div>
}
}
import Link from 'next/link'
export default () => (
<div>
<Link href="/hmr/error-in-gip" id="error-in-gip-link">
Bad Page
</Link>
</div>
)
import React from 'react'
import dynamic from 'next/dynamic'
const HmrDynamic = dynamic(import('../../components/hmr/dynamic'))
export default () => {
return <HmrDynamic />
}
import React, { Component } from 'react'
export default class StyleStateFul extends Component {
render() {
return (
<React.Fragment>
<div className="hmr-style-page">
<p>
This is the style page.
<style jsx>{`
p {
font-size: 100px;
}
`}</style>
</p>
</div>
</React.Fragment>
)
}
}
import React from 'react'
export default function Style() {
return (
<React.Fragment>
<div className="hmr-style-page">
<p>
This is the style page.
<style jsx>{`
p {
font-size: 100px;
}
`}</style>
</p>
</div>
</React.Fragment>
)
}
import url from 'url'
import { join } from 'path'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
describe('misc basic dev tests', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'misc/pages')),
public: new FileRef(join(__dirname, 'misc/public')),
},
nextConfig: {
basePath: '/docs',
},
})
})
afterAll(() => next.destroy())
it('should set process.env.NODE_ENV in development', async () => {
const browser = await webdriver(next.url, '/docs/process-env')
const nodeEnv = await browser.elementByCss('#node-env').text()
expect(nodeEnv).toBe('development')
await browser.close()
})
it('should allow access to public files', async () => {
const data = await renderViaHTTP(next.url, '/docs/data/data.txt')
expect(data).toBe('data')
const legacy = await renderViaHTTP(next.url, '/docs/static/legacy.txt')
expect(legacy).toMatch(`new static folder`)
})
describe('With Security Related Issues', () => {
it('should not allow accessing files outside .next/static and .next/server directory', async () => {
const pathsToCheck = [
`/docs/_next/static/../BUILD_ID`,
`/docs/_next/static/../routes-manifest.json`,
]
for (const path of pathsToCheck) {
const res = await fetchViaHTTP(next.url, path)
const text = await res.text()
try {
expect(res.status).toBe(404)
expect(text).toMatch(/This page could not be found/)
} catch (err) {
throw new Error(`Path ${path} accessible from the browser`)
}
}
})
it('should handle encoded / value for trailing slash correctly', async () => {
const res = await fetchViaHTTP(
next.url,
'/docs/%2fexample.com/',
undefined,
{ redirect: 'manual' }
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe('/docs/%2fexample.com')
expect(hostname).not.toBe('example.com')
const text = await res.text()
expect(text).toEqual('/docs/%2fexample.com')
})
})
async function getLogs$(path) {
let foundLog = false
let browser
try {
browser = await webdriver(next.url, path)
const browserLogs = await browser.log('browser')
browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) {
foundLog = true
}
})
} finally {
if (browser) {
await browser.close()
}
}
return foundLog
}
describe('Development Logs', () => {
it('should warn when prefetch is true', async () => {
const foundLog = await getLogs$('/docs/development-logs')
expect(foundLog).toBe(true)
})
it('should not warn when prefetch is false', async () => {
const foundLog = await getLogs$(
'/docs/development-logs/link-with-prefetch-false'
)
expect(foundLog).toBe(false)
})
it('should not warn when prefetch is not specified', async () => {
const foundLog = await getLogs$(
'/docs/development-logs/link-with-no-prefetch'
)
expect(foundLog).toBe(false)
})
})
})
export default () => <div className="about-page">About Page</div>
import Link from 'next/link'
export default function IndexPage() {
return (
<div>
<Link href="/about" prefetch>
To About Page
</Link>
</div>
)
}
import Link from 'next/link'
export default function NoPrefetchPage() {
return (
<div>
<Link href="/about">No prefetch</Link>
</div>
)
}
import Link from 'next/link'
export default function PrefetchFalsePage() {
return (
<div>
<Link href="/about" prefetch={false}>
Prefetch set to false
</Link>
</div>
)
}
export default () => <div id="node-env">{process.env.NODE_ENV}</div>
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import { createNext, FileRef } from 'e2e-utils'
import { renderViaHTTP, check } from 'next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base'
describe('basic next/dynamic usage', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
components: new FileRef(join(__dirname, 'next-dynamic/components')),
pages: new FileRef(join(__dirname, 'next-dynamic/pages')),
},
nextConfig: {
basePath: '/docs',
},
})
})
afterAll(() => next.destroy())
async function get$(path, query?: any) {
const html = await renderViaHTTP(next.url, path, query)
return cheerio.load(html)
}
describe('Dynamic import', () => {
describe('default behavior', () => {
it('should render dynamic import components', async () => {
const $ = await get$('/docs/dynamic/ssr')
// Make sure the client side knows it has to wait for the bundle
expect(JSON.parse($('#__NEXT_DATA__').html()).dynamicIds).toContain(
'dynamic/ssr.js -> ../../components/hello1'
)
expect($('body').text()).toMatch(/Hello World 1/)
})
it('should render dynamic import components using a function as first parameter', async () => {
const $ = await get$('/docs/dynamic/function')
// Make sure the client side knows it has to wait for the bundle
expect(JSON.parse($('#__NEXT_DATA__').html()).dynamicIds).toContain(
'dynamic/function.js -> ../../components/hello1'
)
expect($('body').text()).toMatch(/Hello World 1/)
})
it('should render even there are no physical chunk exists', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/no-chunk')
await check(
() => browser.elementByCss('body').text(),
/Welcome, normal/
)
await check(
() => browser.elementByCss('body').text(),
/Welcome, dynamic/
)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should hydrate nested chunks', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/nested')
await check(() => browser.elementByCss('body').text(), /Nested 1/)
await check(() => browser.elementByCss('body').text(), /Nested 2/)
await check(
() => browser.elementByCss('body').text(),
/Browser hydrated/
)
if ((global as any).browserName === 'chrome') {
const logs = await browser.log('browser')
logs.forEach((logItem) => {
expect(logItem.message).not.toMatch(
/Expected server HTML to contain/
)
})
}
} finally {
if (browser) {
await browser.close()
}
}
})
it('should render the component Head content', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/head')
await check(() => browser.elementByCss('body').text(), /test/)
const backgroundColor = await browser
.elementByCss('.dynamic-style')
.getComputedCss('background-color')
const height = await browser
.elementByCss('.dynamic-style')
.getComputedCss('height')
expect(height).toBe('200px')
expect(backgroundColor).toMatch(/rgba?\(0, 128, 0/)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('ssr:false option', () => {
it('should not render loading on the server side', async () => {
const $ = await get$('/docs/dynamic/no-ssr')
expect($('body').html()).not.toContain('"dynamicIds"')
expect($('body').text()).not.toMatch('loading...')
})
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/no-ssr')
await check(
() => browser.elementByCss('body').text(),
/Hello World 1/
)
} finally {
if (browser) {
await browser.close()
}
}
})
if (!(global as any).isNextDev) {
it('should not include ssr:false imports to server trace', async () => {
const trace = JSON.parse(
await next.readFile('.next/server/pages/dynamic/no-ssr.js.nft.json')
) as { files: string[] }
expect(trace).not.toContain('hello1')
})
}
})
describe('ssr:true option', () => {
it('Should render the component on the server side', async () => {
const $ = await get$('/docs/dynamic/ssr-true')
expect($('body').html()).toContain('"dynamicIds"')
expect($('p').text()).toBe('Hello World 1')
})
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/ssr-true')
await check(
() => browser.elementByCss('body').text(),
/Hello World 1/
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('custom chunkfilename', () => {
it('should render the correct filename', async () => {
const $ = await get$('/docs/dynamic/chunkfilename')
expect($('body').text()).toMatch(/test chunkfilename/)
expect($('html').html()).toMatch(/hello-world\.js/)
})
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/chunkfilename')
await check(
() => browser.elementByCss('body').text(),
/test chunkfilename/
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('custom loading', () => {
it('should render custom loading on the server side when `ssr:false` and `loading` is provided', async () => {
const $ = await get$('/docs/dynamic/no-ssr-custom-loading')
expect($('p').text()).toBe('LOADING')
})
it('should render the component on client side', async () => {
let browser
try {
browser = await webdriver(
next.url,
'/docs/dynamic/no-ssr-custom-loading'
)
await check(
() => browser.elementByCss('body').text(),
/Hello World 1/
)
} finally {
if (browser) {
await browser.close()
}
}
})
})
describe('Multiple modules', () => {
it('should only include the rendered module script tag', async () => {
const $ = await get$('/docs/dynamic/multiple-modules')
const html = $('html').html()
expect(html).toMatch(/hello1\.js/)
expect(html).not.toMatch(/hello2\.js/)
})
it('should only load the rendered module in the browser', async () => {
let browser
try {
browser = await webdriver(next.url, '/docs/dynamic/multiple-modules')
const html = await browser.eval('document.documentElement.innerHTML')
expect(html).toMatch(/hello1\.js/)
expect(html).not.toMatch(/hello2\.js/)
} finally {
if (browser) {
await browser.close()
}
}
})
it('should only render one bundle if component is used multiple times', async () => {
const $ = await get$('/docs/dynamic/multiple-modules')
const html = $('html').html()
try {
expect(html.match(/chunks[\\/]hello1\.js/g).length).toBe(1)
expect(html).not.toMatch(/hello2\.js/)
} catch (err) {
console.error(html)
throw err
}
})
})
})
})
import React from 'react'
import PropTypes from 'prop-types'
export default class extends React.Component {
static contextTypes = {
data: PropTypes.object,
}
render() {
const { data } = this.context
return <div>{data.title}</div>
}
}
import dynamic from 'next/dynamic'
const Nested2 = dynamic(() => import('./nested2'))
export default () => (
<div>
Nested 1
<Nested2 />
</div>
)
import dynamic from 'next/dynamic'
const BrowserLoaded = dynamic(async () => () => <div>Browser hydrated</div>, {
ssr: false,
})
export default () => (
<div>
<div>Nested 2</div>
<BrowserLoaded />
</div>
)
import React from 'react'
export default class Welcome extends React.Component {
state = { name: null }
componentDidMount() {
const { name } = this.props
this.setState({ name })
}
render() {
const { name } = this.state
if (!name) return null
return <p>Welcome, {name}</p>
}
}
import dynamic from 'next/dynamic'
const Hello = dynamic(
import(
/* webpackChunkName: 'hello-world' */ '../../components/hello-chunkfilename'
)
)
export default Hello
import dynamic from 'next/dynamic'
const Hello = dynamic(() => import('../../components/hello1'))
export default Hello
import dynamic from 'next/dynamic'
import Head from 'next/head'
const Test = dynamic({
loader: async () => {
// component
return () => {
return (
<div className="dynamic-style">
<Head>
<style
dangerouslySetInnerHTML={{
__html: `
.dynamic-style {
background-color: green;
height: 200px;
}
`,
}}
/>
</Head>
test
</div>
)
}
},
ssr: false,
})
export default Test
import Link from 'next/link'
export default () => (
<div>
<Link href="/dynamic/no-chunk">No Chunk</Link>
</div>
)
/* eslint-disable */
import dynamic from 'next/dynamic'
const Hello = dynamic(
import(/* webpackChunkName: 'hello1' */ '../../components/hello3')
)
const Hello2 = dynamic(
import(/* webpackChunkName: 'hello2' */ '../../components/hello4')
)
export default () => {
return (
<div>
<Hello />
<Hello />
<Hello />
<Hello />
<Hello />
<Hello />
<Hello />
</div>
)
}
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../../components/nested1'))
export default DynamicComponent
import dynamic from 'next/dynamic'
import Welcome from '../../components/welcome'
const Welcome2 = dynamic(import('../../components/welcome'))
export default () => (
<div>
<Welcome name="normal" />
<Welcome2 name="dynamic" />
</div>
)
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/hello1'), {
ssr: false,
loading: () => <p>LOADING</p>,
})
export default Hello
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/hello1'), { ssr: false })
export default Hello
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/hello1'), { ssr: true })
export default Hello
import dynamic from 'next/dynamic'
const Hello = dynamic(import('../../components/hello1'))
export default Hello
This diff is collapsed.
...@@ -5,101 +5,117 @@ import { createNext, FileRef } from 'e2e-utils' ...@@ -5,101 +5,117 @@ import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
describe('misc basic dev tests', () => { describe.each([[''], ['/docs']])(
let next: NextInstance 'misc basic dev tests, basePath: %p',
(basePath: string) => {
let next: NextInstance
beforeAll(async () => { beforeAll(async () => {
next = await createNext({ next = await createNext({
files: { files: {
pages: new FileRef(join(__dirname, 'misc/pages')), pages: new FileRef(join(__dirname, 'misc/pages')),
public: new FileRef(join(__dirname, 'misc/public')), public: new FileRef(join(__dirname, 'misc/public')),
}, },
nextConfig: {
basePath,
},
})
}) })
}) afterAll(() => next.destroy())
afterAll(() => next.destroy())
it('should set process.env.NODE_ENV in development', async () => {
const browser = await webdriver(next.url, '/process-env')
const nodeEnv = await browser.elementByCss('#node-env').text()
expect(nodeEnv).toBe('development')
await browser.close()
})
it('should allow access to public files', async () => { it('should set process.env.NODE_ENV in development', async () => {
const data = await renderViaHTTP(next.url, '/data/data.txt') const browser = await webdriver(next.url, basePath + '/process-env')
expect(data).toBe('data') const nodeEnv = await browser.elementByCss('#node-env').text()
expect(nodeEnv).toBe('development')
await browser.close()
})
const legacy = await renderViaHTTP(next.url, '/static/legacy.txt') it('should allow access to public files', async () => {
expect(legacy).toMatch(`new static folder`) const data = await renderViaHTTP(next.url, basePath + '/data/data.txt')
}) expect(data).toBe('data')
describe('With Security Related Issues', () => { const legacy = await renderViaHTTP(
it('should not allow accessing files outside .next/static and .next/server directory', async () => { next.url,
const pathsToCheck = [ basePath + '/static/legacy.txt'
`/_next/static/../BUILD_ID`, )
`/_next/static/../routes-manifest.json`, expect(legacy).toMatch(`new static folder`)
]
for (const path of pathsToCheck) {
const res = await fetchViaHTTP(next.url, path)
const text = await res.text()
try {
expect(res.status).toBe(404)
expect(text).toMatch(/This page could not be found/)
} catch (err) {
throw new Error(`Path ${path} accessible from the browser`)
}
}
}) })
it('should handle encoded / value for trailing slash correctly', async () => { describe('With Security Related Issues', () => {
const res = await fetchViaHTTP(next.url, '/%2fexample.com/', undefined, { it('should not allow accessing files outside .next/static and .next/server directory', async () => {
redirect: 'manual', const pathsToCheck = [
basePath + '/_next/static/../BUILD_ID',
basePath + '/_next/static/../routes-manifest.json',
]
for (const path of pathsToCheck) {
const res = await fetchViaHTTP(next.url, path)
const text = await res.text()
try {
expect(res.status).toBe(404)
expect(text).toMatch(/This page could not be found/)
} catch (err) {
throw new Error(`Path ${path} accessible from the browser`)
}
}
}) })
const { pathname, hostname } = url.parse( it('should handle encoded / value for trailing slash correctly', async () => {
res.headers.get('location') || '' const res = await fetchViaHTTP(
) next.url,
expect(res.status).toBe(308) basePath + '/%2fexample.com/',
expect(pathname).toBe('/%2fexample.com') undefined,
expect(hostname).not.toBe('example.com') {
const text = await res.text() redirect: 'manual',
expect(text).toEqual('/%2fexample.com') }
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe(basePath + '/%2fexample.com')
expect(hostname).not.toBe('example.com')
const text = await res.text()
expect(text).toEqual(basePath + '/%2fexample.com')
})
}) })
})
async function getLogs$(path) { async function getLogs$(path) {
let foundLog = false let foundLog = false
let browser let browser
try { try {
browser = await webdriver(next.url, path) browser = await webdriver(next.url, path)
const browserLogs = await browser.log('browser') const browserLogs = await browser.log('browser')
browserLogs.forEach((log) => { browserLogs.forEach((log) => {
if (log.message.includes('Next.js auto-prefetches automatically')) { if (log.message.includes('Next.js auto-prefetches automatically')) {
foundLog = true foundLog = true
}
})
} finally {
if (browser) {
await browser.close()
} }
})
} finally {
if (browser) {
await browser.close()
} }
return foundLog
} }
return foundLog describe('Development Logs', () => {
} it('should warn when prefetch is true', async () => {
describe('Development Logs', () => { const foundLog = await getLogs$(basePath + '/development-logs')
it('should warn when prefetch is true', async () => { expect(foundLog).toBe(true)
const foundLog = await getLogs$('/development-logs') })
expect(foundLog).toBe(true) it('should not warn when prefetch is false', async () => {
}) const foundLog = await getLogs$(
it('should not warn when prefetch is false', async () => { basePath + '/development-logs/link-with-prefetch-false'
const foundLog = await getLogs$( )
'/development-logs/link-with-prefetch-false' expect(foundLog).toBe(false)
) })
expect(foundLog).toBe(false) it('should not warn when prefetch is not specified', async () => {
}) const foundLog = await getLogs$(
it('should not warn when prefetch is not specified', async () => { basePath + '/development-logs/link-with-no-prefetch'
const foundLog = await getLogs$('/development-logs/link-with-no-prefetch') )
expect(foundLog).toBe(false) expect(foundLog).toBe(false)
})
}) })
}) }
}) )
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