Unverified Commit c1c3675f authored by Leah's avatar Leah Committed by GitHub

refactor tests for readability (#51051)

You'll probably want to disable whitespace in the diff

## Description

This allows for better editor support by using `describe` or functions called `describe` with the same syntax instead of custom names.

Changes:
- `nextTestSetup` can be used instead of `createNextDescribe` keeping the same behaviour but being called inside a `describe` "block" (not applied everywhere)
- `getSnapshotTestDescribe` replaced with a custom `describe.each`
- `sandbox` helper function for `acceptance`/`acceptance-app` merged into a single shared one
- `outdent` to remove the indent from inline files in tests which helps with consistent snapshots
parent c30f21fa
...@@ -169,6 +169,7 @@ ...@@ -169,6 +169,7 @@
"npm-run-all": "4.1.5", "npm-run-all": "4.1.5",
"nprogress": "0.2.0", "nprogress": "0.2.0",
"open": "9.0.0", "open": "9.0.0",
"outdent": "0.8.0",
"pixrem": "5.0.0", "pixrem": "5.0.0",
"playwright-chromium": "1.28.1", "playwright-chromium": "1.28.1",
"plop": "3.0.5", "plop": "3.0.5",
......
...@@ -128,6 +128,7 @@ importers: ...@@ -128,6 +128,7 @@ importers:
npm-run-all: 4.1.5 npm-run-all: 4.1.5
nprogress: 0.2.0 nprogress: 0.2.0
open: 9.0.0 open: 9.0.0
outdent: 0.8.0
pixrem: 5.0.0 pixrem: 5.0.0
playwright-chromium: 1.28.1 playwright-chromium: 1.28.1
plop: 3.0.5 plop: 3.0.5
...@@ -295,6 +296,7 @@ importers: ...@@ -295,6 +296,7 @@ importers:
npm-run-all: 4.1.5 npm-run-all: 4.1.5
nprogress: 0.2.0 nprogress: 0.2.0
open: 9.0.0 open: 9.0.0
outdent: 0.8.0
pixrem: 5.0.0 pixrem: 5.0.0
playwright-chromium: 1.28.1 playwright-chromium: 1.28.1
plop: 3.0.5 plop: 3.0.5
...@@ -18878,6 +18880,10 @@ packages: ...@@ -18878,6 +18880,10 @@ packages:
os-tmpdir: 1.0.2 os-tmpdir: 1.0.2
dev: true dev: true
/outdent/0.8.0:
resolution: {integrity: sha512-KiOAIsdpUTcAXuykya5fnVVT+/5uS0Q1mrkRHcF89tpieSmY33O/tmc54CqwA+bfhbtEfZUNLHaPUiB9X3jt1A==}
dev: true
/p-cancelable/0.3.0: /p-cancelable/0.3.0:
resolution: {integrity: sha512-RVbZPLso8+jFeq1MfNvgXtCRED2raz/dKpacfTNxsx6pLEpEomM7gah6VeHSYV3+vo0OAi4MkArtQcWWXuQoyw==} resolution: {integrity: sha512-RVbZPLso8+jFeq1MfNvgXtCRED2raz/dKpacfTNxsx6pLEpEomM7gah6VeHSYV3+vo0OAi4MkArtQcWWXuQoyw==}
engines: {node: '>=4'} engines: {node: '>=4'}
......
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
describe('ReactRefresh app', () => { describe('ReactRefresh app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ react: 'latest',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), 'react-dom': 'latest',
dependencies: { },
react: 'latest', skipStart: true,
'react-dom': 'latest',
},
skipStart: true,
})
}) })
afterAll(() => next.destroy())
test('can edit a component without losing state', async () => { test('can edit a component without losing state', async () => {
const { session, cleanup } = await sandbox(next) const { session, cleanup } = await sandbox(next)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
...@@ -43,7 +38,7 @@ describe('ReactRefresh app', () => { ...@@ -43,7 +38,7 @@ describe('ReactRefresh app', () => {
).toBe('1') ).toBe('1')
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
...@@ -72,7 +67,7 @@ describe('ReactRefresh app', () => { ...@@ -72,7 +67,7 @@ describe('ReactRefresh app', () => {
await session.write( await session.write(
'NudgeOverview.js', 'NudgeOverview.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
import { foo } from './routes'; import { foo } from './routes';
...@@ -88,7 +83,7 @@ describe('ReactRefresh app', () => { ...@@ -88,7 +83,7 @@ describe('ReactRefresh app', () => {
await session.write( await session.write(
'SurveyOverview.js', 'SurveyOverview.js',
` outdent`
const SurveyOverview = () => { const SurveyOverview = () => {
return 100; return 100;
}; };
...@@ -99,7 +94,7 @@ describe('ReactRefresh app', () => { ...@@ -99,7 +94,7 @@ describe('ReactRefresh app', () => {
await session.write( await session.write(
'Milestones.js', 'Milestones.js',
` outdent`
import React from 'react'; import React from 'react';
import { fragment } from './DashboardPage'; import { fragment } from './DashboardPage';
...@@ -115,7 +110,7 @@ describe('ReactRefresh app', () => { ...@@ -115,7 +110,7 @@ describe('ReactRefresh app', () => {
await session.write( await session.write(
'DashboardPage.js', 'DashboardPage.js',
` outdent`
import React from 'react'; import React from 'react';
import Milestones from './Milestones'; import Milestones from './Milestones';
...@@ -140,7 +135,7 @@ describe('ReactRefresh app', () => { ...@@ -140,7 +135,7 @@ describe('ReactRefresh app', () => {
await session.write( await session.write(
'routes.js', 'routes.js',
` outdent`
import DashboardPage from './DashboardPage'; import DashboardPage from './DashboardPage';
export const foo = {}; export const foo = {};
...@@ -154,7 +149,7 @@ describe('ReactRefresh app', () => { ...@@ -154,7 +149,7 @@ describe('ReactRefresh app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
import DashboardPage from './routes'; import DashboardPage from './routes';
...@@ -173,7 +168,7 @@ describe('ReactRefresh app', () => { ...@@ -173,7 +168,7 @@ describe('ReactRefresh app', () => {
let didFullRefresh = !(await session.patch( let didFullRefresh = !(await session.patch(
'SurveyOverview.js', 'SurveyOverview.js',
` outdent`
const SurveyOverview = () => { const SurveyOverview = () => {
return 200; return 200;
}; };
...@@ -189,7 +184,7 @@ describe('ReactRefresh app', () => { ...@@ -189,7 +184,7 @@ describe('ReactRefresh app', () => {
didFullRefresh = !(await session.patch( didFullRefresh = !(await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
import DashboardPage from './routes'; import DashboardPage from './routes';
...@@ -209,7 +204,7 @@ describe('ReactRefresh app', () => { ...@@ -209,7 +204,7 @@ describe('ReactRefresh app', () => {
didFullRefresh = !(await session.patch( didFullRefresh = !(await session.patch(
'SurveyOverview.js', 'SurveyOverview.js',
` outdent`
const SurveyOverview = () => { const SurveyOverview = () => {
return 300; return 300;
}; };
......
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { getSnapshotTestDescribe } from 'next-test-utils' import { describeVariants as describe } from 'next-test-utils'
import { outdent } from 'outdent'
// TODO-APP: Investigate snapshot mismatch // TODO-APP: Investigate snapshot mismatch
for (const variant of ['default', 'turbo']) { describe.each(['default', 'turbo'])('ReactRefreshLogBox app %s', () => {
getSnapshotTestDescribe(variant)(`ReactRefreshLogBox app ${variant}`, () => { const { next } = nextTestSetup({
let next: NextInstance files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: {
beforeAll(async () => { react: 'latest',
next = await createNext({ 'react-dom': 'latest',
files: new FileRef( },
path.join(__dirname, 'fixtures', 'default-template') skipStart: true,
), })
dependencies: {
react: 'latest', // Module trace is only available with webpack 5
'react-dom': 'latest', test('Node.js builtins', async () => {
}, const { session, cleanup } = await sandbox(
skipStart: true, next,
}) new Map([
}) [
afterAll(() => next.destroy()) 'node_modules/my-package/index.js',
outdent`
// Module trace is only available with webpack 5 const dns = require('dns')
test('Node.js builtins', async () => { module.exports = dns
const { session, cleanup } = await sandbox( `,
next, ],
new Map([ [
[ 'node_modules/my-package/package.json',
'node_modules/my-package/index.js', outdent`
` {
const dns = require('dns') "name": "my-package",
module.exports = dns "version": "0.0.1"
`, }
], `,
[ ],
'node_modules/my-package/package.json', ])
` )
{
"name": "my-package", await session.patch(
"version": "0.0.1" 'index.js',
} outdent`
`, import pkg from 'my-package'
],
]) export default function Hello() {
) return (pkg ? <h1>Package loaded</h1> : <h1>Package did not load</h1>)
}
await session.patch( `
'index.js', )
` expect(await session.hasRedbox(true)).toBe(true)
import pkg from 'my-package' expect(await session.getRedboxSource()).toMatchSnapshot()
export default function Hello() { await cleanup()
return (pkg ? <h1>Package loaded</h1> : <h1>Package did not load</h1>) })
}
` test('Module not found', async () => {
) const { session, cleanup } = await sandbox(next)
expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchSnapshot() await session.patch(
'index.js',
await cleanup() outdent`
}) import Comp from 'b'
export default function Oops() {
test('Module not found', async () => { return (
const { session, cleanup } = await sandbox(next) <div>
<Comp>lol</Comp>
await session.patch( </div>
'index.js', )
`import Comp from 'b' }
export default function Oops() { `
return ( )
<div>
<Comp>lol</Comp> expect(await session.hasRedbox(true)).toBe(true)
</div>
) const source = await session.getRedboxSource()
} expect(source).toMatchSnapshot()
`
) await cleanup()
})
expect(await session.hasRedbox(true)).toBe(true)
test('Module not found empty import trace', async () => {
const source = await session.getRedboxSource() const { session, cleanup } = await sandbox(next)
expect(source).toMatchSnapshot()
await session.patch(
await cleanup() 'app/page.js',
}) outdent`
'use client'
test('Module not found empty import trace', async () => { import Comp from 'b'
const { session, cleanup } = await sandbox(next) export default function Oops() {
return (
await session.patch( <div>
'app/page.js', <Comp>lol</Comp>
`'use client' </div>
import Comp from 'b' )
export default function Oops() { }
return ( `
<div> )
<Comp>lol</Comp>
</div> expect(await session.hasRedbox(true)).toBe(true)
)
} const source = await session.getRedboxSource()
` expect(source).toMatchSnapshot()
)
await cleanup()
expect(await session.hasRedbox(true)).toBe(true) })
const source = await session.getRedboxSource() test('Module not found missing global CSS', async () => {
expect(source).toMatchSnapshot() const { session, cleanup } = await sandbox(
next,
await cleanup() new Map([
}) [
'app/page.js',
test('Module not found missing global CSS', async () => { outdent`
const { session, cleanup } = await sandbox( 'use client'
next, import './non-existent.css'
new Map([ export default function Page(props) {
[ return <p>index page</p>
'app/page.js', }
`'use client' `,
import './non-existent.css' ],
])
)
expect(await session.hasRedbox(true)).toBe(true)
const source = await session.getRedboxSource()
expect(source).toMatchSnapshot()
await session.patch(
'app/page.js',
outdent`
'use client'
export default function Page(props) { export default function Page(props) {
return <p>index page</p> return <p>index page</p>
} }
`, `
], )
]) expect(await session.hasRedbox(false)).toBe(false)
) expect(
expect(await session.hasRedbox(true)).toBe(true) await session.evaluate(() => document.documentElement.innerHTML)
).toContain('index page')
const source = await session.getRedboxSource()
expect(source).toMatchSnapshot() await cleanup()
await session.patch(
'app/page.js',
`'use client'
export default function Page(props) {
return <p>index page</p>
}
`
)
expect(await session.hasRedbox(false)).toBe(false)
expect(
await session.evaluate(() => document.documentElement.innerHTML)
).toContain('index page')
await cleanup()
})
}) })
} })
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
describe('ReactRefreshLogBox app', () => { describe('ReactRefreshLogBox app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ sass: 'latest',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), react: 'latest',
skipStart: true, 'react-dom': 'latest',
dependencies: { },
sass: 'latest', skipStart: true,
react: 'latest',
'react-dom': 'latest',
},
})
}) })
afterAll(() => next.destroy())
test('scss syntax errors', async () => { test('scss syntax errors', async () => {
const { session, cleanup } = await sandbox(next) const { session, cleanup } = await sandbox(next)
...@@ -26,7 +21,7 @@ describe('ReactRefreshLogBox app', () => { ...@@ -26,7 +21,7 @@ describe('ReactRefreshLogBox app', () => {
await session.write('index.module.scss', `.button { font-size: 5px; }`) await session.write('index.module.scss', `.button { font-size: 5px; }`)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import './index.module.scss'; import './index.module.scss';
export default () => { export default () => {
return ( return (
......
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
// TODO: re-enable these tests after figuring out what is causing // TODO: re-enable these tests after figuring out what is causing
// them to be so unreliable in CI // them to be so unreliable in CI
describe.skip('ReactRefreshLogBox app', () => { describe.skip('ReactRefreshLogBox app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ react: 'latest',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), 'react-dom': 'latest',
dependencies: { },
react: 'latest', skipStart: true,
'react-dom': 'latest',
},
skipStart: true,
})
}) })
afterAll(() => next.destroy())
test('<Link> with multiple children', async () => { test('<Link> with multiple children', async () => {
const { session, cleanup } = await sandbox(next) const { session, cleanup } = await sandbox(next)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Index() { export default function Index() {
...@@ -64,7 +59,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -64,7 +59,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -80,7 +75,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -80,7 +75,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -92,7 +87,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -92,7 +87,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -116,7 +111,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -116,7 +111,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -140,7 +135,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -140,7 +135,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -164,7 +159,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -164,7 +159,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -189,7 +184,7 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -189,7 +184,7 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Link from 'next/link' import Link from 'next/link'
export default function Hello() { export default function Hello() {
...@@ -220,7 +215,8 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -220,7 +215,8 @@ describe.skip('ReactRefreshLogBox app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import myLibrary from 'my-non-existent-library' import myLibrary from 'my-non-existent-library'
export async function getStaticProps() { export async function getStaticProps() {
return { return {
......
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { NextInstance } from 'test/lib/next-modes/base' import { sandbox } from 'development-sandbox'
import { sandbox } from './helpers' import { outdent } from 'outdent'
describe('ReactRefreshModule app', () => { describe('ReactRefreshModule app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ react: 'latest',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), 'react-dom': 'latest',
dependencies: { },
react: 'latest', skipStart: true,
'react-dom': 'latest',
},
skipStart: true,
})
}) })
afterAll(() => next.destroy())
it('should allow any variable names', async () => { it('should allow any variable names', async () => {
const { session, cleanup } = await sandbox(next, new Map([])) const { session, cleanup } = await sandbox(next, new Map([]))
...@@ -33,11 +28,13 @@ describe('ReactRefreshModule app', () => { ...@@ -33,11 +28,13 @@ describe('ReactRefreshModule app', () => {
for await (const variable of variables) { for await (const variable of variables) {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
import { default as ${variable} } from 'next/link' 'use client'
export default function Page() { import { default as ${variable} } from 'next/link'
return null export default function Page() {
}` return null
}
`
) )
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
expect(next.cliOutput).not.toContain( expect(next.cliOutput).not.toContain(
......
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { outdent } from 'outdent'
describe('ReactRefreshRegression app', () => { describe('ReactRefreshRegression app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ 'styled-components': '5.1.0',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), '@next/mdx': 'canary',
skipStart: true, '@mdx-js/loader': '0.18.0',
dependencies: { react: 'latest',
'styled-components': '5.1.0', 'react-dom': 'latest',
'@next/mdx': 'canary', },
'@mdx-js/loader': '0.18.0', skipStart: true,
react: 'latest',
'react-dom': 'latest',
},
})
}) })
afterAll(() => next.destroy())
// https://github.com/vercel/next.js/issues/12422 // https://github.com/vercel/next.js/issues/12422
// TODO-APP: port to app directory // TODO-APP: port to app directory
...@@ -29,7 +24,7 @@ describe('ReactRefreshRegression app', () => { ...@@ -29,7 +24,7 @@ describe('ReactRefreshRegression app', () => {
const files = new Map() const files = new Map()
files.set( files.set(
'pages/_document.js', 'pages/_document.js',
` outdent`
import Document from 'next/document' import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components' import { ServerStyleSheet } from 'styled-components'
...@@ -67,7 +62,7 @@ describe('ReactRefreshRegression app', () => { ...@@ -67,7 +62,7 @@ describe('ReactRefreshRegression app', () => {
// We start here. // We start here.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import React from 'react' import React from 'react'
import styled from 'styled-components' import styled from 'styled-components'
...@@ -92,7 +87,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -92,7 +87,8 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
...@@ -118,7 +114,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -118,7 +114,8 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
...@@ -151,7 +148,7 @@ describe('ReactRefreshRegression app', () => { ...@@ -151,7 +148,7 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
export const revalidate = 0 export const revalidate = 0
import Component from '../index' import Component from '../index'
...@@ -162,7 +159,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -162,7 +159,8 @@ describe('ReactRefreshRegression app', () => {
) )
await session.patch( await session.patch(
'index.js', 'index.js',
`'use client' outdent`
'use client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
...@@ -192,7 +190,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -192,7 +190,8 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
`'use client' outdent`
'use client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Index() { export default function Index() {
...@@ -229,7 +228,7 @@ describe('ReactRefreshRegression app', () => { ...@@ -229,7 +228,7 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
export const config = {} export const config = {}
import Component from '../index' import Component from '../index'
...@@ -241,7 +240,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -241,7 +240,8 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'index.js', 'index.js',
`'use client' outdent`
'use client'
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export const config = {} export const config = {}
...@@ -316,8 +316,10 @@ describe('ReactRefreshRegression app', () => { ...@@ -316,8 +316,10 @@ describe('ReactRefreshRegression app', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
export default function Page() { throw new Error('boom'); }` 'use client'
export default function Page() { throw new Error('boom'); }
`
) )
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
...@@ -337,7 +339,7 @@ describe('ReactRefreshRegression app', () => { ...@@ -337,7 +339,7 @@ describe('ReactRefreshRegression app', () => {
const files = new Map() const files = new Map()
files.set( files.set(
'next.config.js', 'next.config.js',
` outdent`
const withMDX = require("@next/mdx")({ const withMDX = require("@next/mdx")({
extension: /\\.mdx?$/, extension: /\\.mdx?$/,
}); });
...@@ -350,12 +352,13 @@ describe('ReactRefreshRegression app', () => { ...@@ -350,12 +352,13 @@ describe('ReactRefreshRegression app', () => {
files.set('app/content.mdx', `Hello World!`) files.set('app/content.mdx', `Hello World!`)
files.set( files.set(
'app/page.js', 'app/page.js',
`'use client' outdent`
import MDX from './content.mdx' 'use client'
export default function Page() { import MDX from './content.mdx'
return <div id="content"><MDX /></div> export default function Page() {
} return <div id="content"><MDX /></div>
` }
`
) )
const { session, cleanup } = await sandbox(next, files) const { session, cleanup } = await sandbox(next, files)
......
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
describe('ReactRefreshRequire app', () => { describe('ReactRefreshRequire app', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { dependencies: {
next = await createNext({ react: 'latest',
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), 'react-dom': 'latest',
dependencies: { },
react: 'latest', skipStart: true,
'react-dom': 'latest',
},
skipStart: true,
})
}) })
afterAll(() => next.destroy())
// https://github.com/facebook/metro/blob/b651e535cd0fc5df6c0803b9aa647d664cb9a6c3/packages/metro/src/lib/polyfills/__tests__/require-test.js#L989-L1048 // https://github.com/facebook/metro/blob/b651e535cd0fc5df6c0803b9aa647d664cb9a6c3/packages/metro/src/lib/polyfills/__tests__/require-test.js#L989-L1048
test('re-runs accepted modules', async () => { test('re-runs accepted modules', async () => {
...@@ -87,12 +82,12 @@ describe('ReactRefreshRequire app', () => { ...@@ -87,12 +82,12 @@ describe('ReactRefreshRequire app', () => {
await session.write( await session.write(
'./foo.js', './foo.js',
` outdent`
window.log.push('init FooV1'); window.log.push('init FooV1');
require('./bar'); require('./bar');
// Exporting a component marks it as auto-accepting. // Exporting a component marks it as auto-accepting.
export default function Foo() {}; export default function Foo() {};
` `
) )
...@@ -144,10 +139,10 @@ describe('ReactRefreshRequire app', () => { ...@@ -144,10 +139,10 @@ describe('ReactRefreshRequire app', () => {
await session.evaluate(() => ((window as any).log = [])) await session.evaluate(() => ((window as any).log = []))
await session.patch( await session.patch(
'./bar.js', './bar.js',
` outdent`
window.log.push('init BarV3'); window.log.push('init BarV3');
// Exporting a component marks it as auto-accepting. // Exporting a component marks it as auto-accepting.
export default function Bar() {}; export default function Bar() {};
` `
) )
expect(await session.evaluate(() => (window as any).log)).toEqual([ expect(await session.evaluate(() => (window as any).log)).toEqual([
...@@ -165,9 +160,9 @@ describe('ReactRefreshRequire app', () => { ...@@ -165,9 +160,9 @@ describe('ReactRefreshRequire app', () => {
await session.evaluate(() => ((window as any).log = [])) await session.evaluate(() => ((window as any).log = []))
await session.patch( await session.patch(
'./bar.js', './bar.js',
` outdent`
window.log.push('init BarV4'); window.log.push('init BarV4');
export default function Bar() {}; export default function Bar() {};
` `
) )
expect(await session.evaluate(() => (window as any).log)).toEqual([ expect(await session.evaluate(() => (window as any).log)).toEqual([
...@@ -203,34 +198,34 @@ describe('ReactRefreshRequire app', () => { ...@@ -203,34 +198,34 @@ describe('ReactRefreshRequire app', () => {
await session.write( await session.write(
'root.js', 'root.js',
` outdent`
window.log.push('init RootV1'); window.log.push('init RootV1');
import './middleA'; import './middleA';
import './middleB'; import './middleB';
import './middleC'; import './middleC';
export default function Root() {}; export default function Root() {};
` `
) )
await session.write( await session.write(
'middleA.js', 'middleA.js',
` outdent`
log.push('init MiddleAV1'); log.push('init MiddleAV1');
import './leaf'; import './leaf';
export default function MiddleA() {}; export default function MiddleA() {};
` `
) )
await session.write( await session.write(
'middleB.js', 'middleB.js',
` outdent`
log.push('init MiddleBV1'); log.push('init MiddleBV1');
import './leaf'; import './leaf';
export default function MiddleB() {}; export default function MiddleB() {};
` `
) )
// This one doesn't import leaf and also doesn't export a component (so it // This one doesn't import leaf and also doesn't export a component (so it
...@@ -290,12 +285,12 @@ describe('ReactRefreshRequire app', () => { ...@@ -290,12 +285,12 @@ describe('ReactRefreshRequire app', () => {
await session.evaluate(() => ((window as any).log = [])) await session.evaluate(() => ((window as any).log = []))
await session.patch( await session.patch(
'middleB.js', 'middleB.js',
` outdent`
log.push('init MiddleBV2'); log.push('init MiddleBV2');
import './leaf'; import './leaf';
export default function MiddleB() {}; export default function MiddleB() {};
` `
) )
expect(await session.evaluate(() => (window as any).log)).toEqual([ expect(await session.evaluate(() => (window as any).log)).toEqual([
......
...@@ -4,9 +4,9 @@ exports[`ReactRefreshLogBox app default Module not found 1`] = ` ...@@ -4,9 +4,9 @@ exports[`ReactRefreshLogBox app default Module not found 1`] = `
"./index.js (1:0) "./index.js (1:0)
Module not found: Can't resolve 'b' Module not found: Can't resolve 'b'
> 1 | import Comp from 'b' > 1 | import Comp from 'b'
2 | export default function Oops() { 2 | export default function Oops() {
3 | return ( 3 | return (
4 | <div> 4 | <div>
https://nextjs.org/docs/messages/module-not-found https://nextjs.org/docs/messages/module-not-found
...@@ -15,33 +15,31 @@ Import trace for requested module: ...@@ -15,33 +15,31 @@ Import trace for requested module:
`; `;
exports[`ReactRefreshLogBox app default Module not found empty import trace 1`] = ` exports[`ReactRefreshLogBox app default Module not found empty import trace 1`] = `
"./app/page.js (2:6) "./app/page.js (2:0)
Module not found: Can't resolve 'b' Module not found: Can't resolve 'b'
1 | 'use client' 1 | 'use client'
> 2 | import Comp from 'b' > 2 | import Comp from 'b'
| ^ 3 | export default function Oops() {
3 | export default function Oops() { 4 | return (
4 | return ( 5 | <div>
5 | <div>
https://nextjs.org/docs/messages/module-not-found" https://nextjs.org/docs/messages/module-not-found"
`; `;
exports[`ReactRefreshLogBox app default Module not found missing global CSS 1`] = ` exports[`ReactRefreshLogBox app default Module not found missing global CSS 1`] = `
"./app/page.js:2:10 "./app/page.js:2:0
Module not found: Can't resolve './non-existent.css' Module not found: Can't resolve './non-existent.css'
1 | 'use client' 1 | 'use client'
> 2 | import './non-existent.css' > 2 | import './non-existent.css'
| ^ 3 | export default function Page(props) {
3 | export default function Page(props) { 4 | return <p>index page</p>
4 | return <p>index page</p> 5 | }
5 | }
https://nextjs.org/docs/messages/module-not-found" https://nextjs.org/docs/messages/module-not-found"
`; `;
exports[`ReactRefreshLogBox app default Node.js builtins 1`] = ` exports[`ReactRefreshLogBox app default Node.js builtins 1`] = `
"./node_modules/my-package/index.js (2:0) "./node_modules/my-package/index.js (1:0)
Module not found: Can't resolve 'dns' Module not found: Can't resolve 'dns'
https://nextjs.org/docs/messages/module-not-found https://nextjs.org/docs/messages/module-not-found
......
...@@ -22,15 +22,15 @@ Import trace for requested module: ...@@ -22,15 +22,15 @@ Import trace for requested module:
`; `;
exports[`ReactRefreshLogBox app default Should not show __webpack_exports__ when exporting anonymous arrow function 1`] = ` exports[`ReactRefreshLogBox app default Should not show __webpack_exports__ when exporting anonymous arrow function 1`] = `
"index.js (4:16) @ default "index.js (3:10) @ default
2 | export default () => { 1 | export default () => {
3 | if (typeof window !== 'undefined') { 2 | if (typeof window !== 'undefined') {
> 4 | throw new Error('test') > 3 | throw new Error('test')
| ^ | ^
5 | } 4 | }
6 | 5 |
7 | return null" 6 | return null"
`; `;
exports[`ReactRefreshLogBox app default boundaries 1`] = ` exports[`ReactRefreshLogBox app default boundaries 1`] = `
...@@ -41,15 +41,14 @@ exports[`ReactRefreshLogBox app default boundaries 1`] = ` ...@@ -41,15 +41,14 @@ exports[`ReactRefreshLogBox app default boundaries 1`] = `
`; `;
exports[`ReactRefreshLogBox app default conversion to class component (1) 1`] = ` exports[`ReactRefreshLogBox app default conversion to class component (1) 1`] = `
"Child.js (5:18) @ ClickCount.render "Child.js (4:10) @ ClickCount.render
3 | export default class ClickCount extends Component { 2 | export default class ClickCount extends Component {
4 | render() { 3 | render() {
> 5 | throw new Error() > 4 | throw new Error()
| ^ | ^
6 | } 5 | }
7 | } 6 | }"
8 | "
`; `;
exports[`ReactRefreshLogBox app default css syntax errors 1`] = ` exports[`ReactRefreshLogBox app default css syntax errors 1`] = `
...@@ -77,25 +76,25 @@ exports[`ReactRefreshLogBox app default logbox: anchors links in error messages ...@@ -77,25 +76,25 @@ exports[`ReactRefreshLogBox app default logbox: anchors links in error messages
exports[`ReactRefreshLogBox app default logbox: anchors links in error messages 9`] = `"http://example.com/"`; exports[`ReactRefreshLogBox app default logbox: anchors links in error messages 9`] = `"http://example.com/"`;
exports[`ReactRefreshLogBox app default module init error not shown 1`] = ` exports[`ReactRefreshLogBox app default module init error not shown 1`] = `
"index.js (4:14) @ eval "index.js (3:6) @ eval
2 | // top offset for snapshot 1 | // top offset for snapshot
3 | import * as React from 'react'; 2 | import * as React from 'react';
> 4 | throw new Error('no') > 3 | throw new Error('no')
| ^ | ^
5 | class ClassDefault extends React.Component { 4 | class ClassDefault extends React.Component {
6 | render() { 5 | render() {
7 | return <h1>Default Export</h1>;" 6 | return <h1>Default Export</h1>;"
`; `;
exports[`ReactRefreshLogBox app default should strip whitespace correctly with newline 1`] = ` exports[`ReactRefreshLogBox app default should strip whitespace correctly with newline 1`] = `
"index.js (9:34) @ onClick "index.js (8:26) @ onClick
7 | 6 |
8 | <a onClick={() => { 7 | <a onClick={() => {
> 9 | throw new Error('idk') > 8 | throw new Error('idk')
| ^ | ^
10 | }}> 9 | }}>
11 | click me 10 | click me
12 | </a>" 11 | </a>"
`; `;
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { check, hasRedbox, waitFor, retry } from 'next-test-utils' import { check, hasRedbox, waitFor, retry } from 'next-test-utils'
import path from 'path' import path from 'path'
createNextDescribe( describe('Error overlay - RSC build errors', () => {
'Error overlay - RSC build errors', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'app-hmr-changes')), files: new FileRef(path.join(__dirname, 'fixtures', 'app-hmr-changes')),
dependencies: { dependencies: {
'@next/mdx': 'canary', '@next/mdx': 'canary',
...@@ -18,48 +17,47 @@ createNextDescribe( ...@@ -18,48 +17,47 @@ createNextDescribe(
'image-size': '^1.0.2', 'image-size': '^1.0.2',
autoprefixer: '^10.4.13', autoprefixer: '^10.4.13',
}, },
}, })
({ next }) => {
it('should handle successive HMR changes with errors correctly', async () => {
const browser = await retry(
() => next.browser('/2020/develop-preview-test'),
1000,
500
)
await check( it('should handle successive HMR changes with errors correctly', async () => {
() => browser.eval('document.documentElement.innerHTML'), const browser = await retry(
/A few years ago I tweeted/ () => next.browser('/2020/develop-preview-test'),
) 1000,
500
)
const pagePath = 'app/(post)/2020/develop-preview-test/page.mdx' await check(
const originalPage = await next.readFile(pagePath) () => browser.eval('document.documentElement.innerHTML'),
/A few years ago I tweeted/
)
const break1 = originalPage.replace('break 1', '<Figure>') const pagePath = 'app/(post)/2020/develop-preview-test/page.mdx'
const originalPage = await next.readFile(pagePath)
await next.patchFile(pagePath, break1) const break1 = originalPage.replace('break 1', '<Figure>')
const break2 = break1.replace('{/* break point 2 */}', '<Figure />')
await next.patchFile(pagePath, break2) await next.patchFile(pagePath, break1)
for (let i = 0; i < 5; i++) { const break2 = break1.replace('{/* break point 2 */}', '<Figure />')
await next.patchFile(pagePath, break2.replace('break 3', '<Hello />'))
await next.patchFile(pagePath, break2) await next.patchFile(pagePath, break2)
expect(await hasRedbox(browser, true)).toBe(true)
await next.patchFile(pagePath, break1) for (let i = 0; i < 5; i++) {
await waitFor(100) await next.patchFile(pagePath, break2.replace('break 3', '<Hello />'))
await next.patchFile(pagePath, originalPage) await next.patchFile(pagePath, break2)
expect(await hasRedbox(browser, false)).toBe(false) expect(await hasRedbox(browser, true)).toBe(true)
}
await check( await next.patchFile(pagePath, break1)
() => browser.eval('document.documentElement.innerHTML'), await waitFor(100)
/A few years ago I tweeted/
) await next.patchFile(pagePath, originalPage)
}) expect(await hasRedbox(browser, false)).toBe(false)
} }
)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/A few years ago I tweeted/
)
})
})
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
createNextDescribe( describe('Component Stack in error overlay', () => {
'Component Stack in error overlay', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should show a component stack on hydration error', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/component.js',
`
'use client'
const isClient = typeof window !== 'undefined'
export default function Component() {
return (
<div>
<p>{isClient ? "client" : "server"}</p>
</div>
);
}
`,
],
[
'app/page.js',
`
import Component from './component'
export default function Mismatch() {
return (
<main>
<Component />
</main>
);
}
`,
],
])
)
await session.waitForAndOpenRuntimeError() it('should show a component stack on hydration error', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/component.js',
outdent`
'use client'
const isClient = typeof window !== 'undefined'
export default function Component() {
return (
<div>
<p>{isClient ? "client" : "server"}</p>
</div>
);
}
`,
],
[
'app/page.js',
outdent`
import Component from './component'
export default function Mismatch() {
return (
<main>
<Component />
</main>
);
}
`,
],
])
)
expect(await session.getRedboxComponentStack()).toMatchInlineSnapshot(` await session.waitForAndOpenRuntimeError()
expect(await session.getRedboxComponentStack()).toMatchInlineSnapshot(`
"p "p
div div
Component Component
main" main"
`) `)
await cleanup() await cleanup()
}) })
} })
)
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNextDescribe } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
createNextDescribe( describe('dynamic = "error" in devmode', () => {
'dynamic = "error" in devmode', const { next } = nextTestSetup({
{ files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
files: path.join(__dirname, 'fixtures', 'default-template'),
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should show error overlay when dynamic is forced', async () => { it('should show error overlay when dynamic is forced', async () => {
const { session, cleanup } = await sandbox(next, undefined, '/server') const { session, cleanup } = await sandbox(next, undefined, '/server')
// dynamic = "error" and force dynamic // dynamic = "error" and force dynamic
await session.patch( await session.patch(
'app/server/page.js', 'app/server/page.js',
` outdent`
import { cookies } from 'next/headers'; import { cookies } from 'next/headers';
import Component from '../../index' import Component from '../../index'
...@@ -28,15 +28,14 @@ createNextDescribe( ...@@ -28,15 +28,14 @@ createNextDescribe(
export const dynamic = "error" export const dynamic = "error"
` `
) )
await session.hasRedbox(true) await session.hasRedbox(true)
console.log(await session.getRedboxDescription()) console.log(await session.getRedboxDescription())
expect(await session.getRedboxDescription()).toBe( expect(await session.getRedboxDescription()).toBe(
`Error: Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`.` `Error: Page with \`dynamic = "error"\` couldn't be rendered statically because it used \`cookies\`.`
) )
await cleanup() await cleanup()
}) })
} })
)
import { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { outdent } from 'outdent'
async function clickSourceFile(browser: any) { async function clickSourceFile(browser: any) {
await browser.waitForElementByCss( await browser.waitForElementByCss(
...@@ -24,136 +25,140 @@ async function clickImportTraceFiles(browser: any) { ...@@ -24,136 +25,140 @@ async function clickImportTraceFiles(browser: any) {
} }
} }
createNextDescribe( describe('Error overlay - editor links', () => {
'Error overlay - editor links', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should be possible to open source file on build error', async () => { it('should be possible to open source file on build error', async () => {
let editorRequestsCount = 0 let editorRequestsCount = 0
const { session, browser, cleanup } = await sandbox( const { session, browser, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from '../index' import Component from '../index'
export default function Page() { export default function Page() {
return <Component /> return <Component />
} }
`, `,
], ],
]), ]),
undefined, undefined,
{ {
beforePageLoad(page) { beforePageLoad(page) {
page.route('**/__nextjs_launch-editor**', (route) => { page.route('**/__nextjs_launch-editor**', (route) => {
editorRequestsCount += 1 editorRequestsCount += 1
route.fulfill() route.fulfill()
}) })
}, },
} }
) )
await session.patch( await session.patch(
'index.js', 'index.js',
`import { useState } from 'react' outdent`
export default () => 'hello world'` import { useState } from 'react'
) export default () => 'hello world'
`
)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
await clickSourceFile(browser) await clickSourceFile(browser)
await check(() => editorRequestsCount, /1/) await check(() => editorRequestsCount, /1/)
await cleanup() await cleanup()
}) })
it('should be possible to open import trace files on RSC parse error', async () => { it('should be possible to open import trace files on RSC parse error', async () => {
let editorRequestsCount = 0 let editorRequestsCount = 0
const { session, browser, cleanup } = await sandbox( const { session, browser, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from '../index' import Component from '../index'
export default function Page() { export default function Page() {
return <Component /> return <Component />
} }
`, `,
], ],
['mod1.js', "import './mod2.js'"], ['mod1.js', "import './mod2.js'"],
['mod2.js', '{{{{{'], ['mod2.js', '{{{{{'],
]), ]),
undefined, undefined,
{ {
beforePageLoad(page) { beforePageLoad(page) {
page.route('**/__nextjs_launch-editor**', (route) => { page.route('**/__nextjs_launch-editor**', (route) => {
editorRequestsCount += 1 editorRequestsCount += 1
route.fulfill() route.fulfill()
}) })
}, },
} }
) )
await session.patch( await session.patch(
'index.js', 'index.js',
`import './mod1' outdent`
export default () => 'hello world'` import './mod1'
) export default () => 'hello world'
`
)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
await clickImportTraceFiles(browser) await clickImportTraceFiles(browser)
await check(() => editorRequestsCount, /4/) await check(() => editorRequestsCount, /4/)
await cleanup() await cleanup()
}) })
it('should be possible to open import trace files on module not found error', async () => { it('should be possible to open import trace files on module not found error', async () => {
let editorRequestsCount = 0 let editorRequestsCount = 0
const { session, browser, cleanup } = await sandbox( const { session, browser, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from '../index' import Component from '../index'
export default function Page() { export default function Page() {
return <Component /> return <Component />
} }
`, `,
], ],
['mod1.js', "import './mod2.js'"], ['mod1.js', "import './mod2.js'"],
['mod2.js', 'import "boom"'], ['mod2.js', 'import "boom"'],
]), ]),
undefined, undefined,
{ {
beforePageLoad(page) { beforePageLoad(page) {
page.route('**/__nextjs_launch-editor**', (route) => { page.route('**/__nextjs_launch-editor**', (route) => {
editorRequestsCount += 1 editorRequestsCount += 1
route.fulfill() route.fulfill()
}) })
}, },
} }
) )
await session.patch( await session.patch(
'index.js', 'index.js',
`import './mod1' outdent`
export default () => 'hello world'` import './mod1'
) export default () => 'hello world'
`
)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
await clickImportTraceFiles(browser) await clickImportTraceFiles(browser)
await check(() => editorRequestsCount, /3/) await check(() => editorRequestsCount, /3/)
await cleanup() await cleanup()
}) })
} })
)
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { outdent } from 'outdent'
createNextDescribe( describe('Error overlay - error message urls', () => {
'Error overlay - error message urls', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should be possible to click url in build error', async () => {
const { session, browser, cleanup } = await sandbox(next)
const content = await next.readFile('app/page.js') it('should be possible to click url in build error', async () => {
const { session, browser, cleanup } = await sandbox(next)
await session.patch( const content = await next.readFile('app/page.js')
'app/page.js',
content + '\nexport function getServerSideProps() {}'
)
expect(await session.hasRedbox(true)).toBe(true) await session.patch(
'app/page.js',
content + '\nexport function getServerSideProps() {}'
)
const link = await browser.elementByCss('[data-nextjs-terminal] a') expect(await session.hasRedbox(true)).toBe(true)
const text = await link.text()
const href = await link.getAttribute('href')
expect(text).toEqual(
'https://nextjs.org/docs/app/building-your-application/data-fetching'
)
expect(href).toEqual(
'https://nextjs.org/docs/app/building-your-application/data-fetching'
)
await cleanup() const link = await browser.elementByCss('[data-nextjs-terminal] a')
}) const text = await link.text()
const href = await link.getAttribute('href')
expect(text).toEqual(
'https://nextjs.org/docs/app/building-your-application/data-fetching'
)
expect(href).toEqual(
'https://nextjs.org/docs/app/building-your-application/data-fetching'
)
it('should be possible to click url in runtime error', async () => { await cleanup()
const { session, browser, cleanup } = await sandbox( })
next,
new Map([ it('should be possible to click url in runtime error', async () => {
[ const { session, browser, cleanup } = await sandbox(
'app/page.js', next,
`'use client' new Map([
[
'app/page.js',
outdent`
'use client'
export default function Page() { export default function Page() {
return typeof window === 'undefined' ? 'HELLO' : 'WORLD' return typeof window === 'undefined' ? 'HELLO' : 'WORLD'
} }
`, `,
], ],
]) ])
) )
await session.waitForAndOpenRuntimeError() await session.waitForAndOpenRuntimeError()
const link = await browser.elementByCss( const link = await browser.elementByCss('#nextjs__container_errors_desc a')
'#nextjs__container_errors_desc a' const text = await link.text()
) const href = await link.getAttribute('href')
const text = await link.text() expect(text).toEqual(
const href = await link.getAttribute('href') 'https://nextjs.org/docs/messages/react-hydration-error'
expect(text).toEqual( )
'https://nextjs.org/docs/messages/react-hydration-error' expect(href).toEqual(
) 'https://nextjs.org/docs/messages/react-hydration-error'
expect(href).toEqual( )
'https://nextjs.org/docs/messages/react-hydration-error'
)
await cleanup() await cleanup()
}) })
} })
)
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
createNextDescribe( describe('Error Overlay invalid imports', () => {
'Error Overlay invalid imports', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
...@@ -14,25 +14,25 @@ createNextDescribe( ...@@ -14,25 +14,25 @@ createNextDescribe(
'client-only': 'latest', 'client-only': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should show error when using styled-jsx in server component', async () => { it('should show error when using styled-jsx in server component', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/comp1.js', 'app/comp1.js',
` outdent`
import { Comp2 } from './comp2' import { Comp2 } from './comp2'
export function Comp1() { export function Comp1() {
return <Comp2 /> return <Comp2 />
} }
`, `,
], ],
[ [
'app/comp2.js', 'app/comp2.js',
` outdent`
export function Comp2() { export function Comp2() {
return ( return (
<div> <div>
...@@ -44,29 +44,29 @@ createNextDescribe( ...@@ -44,29 +44,29 @@ createNextDescribe(
</div> </div>
) )
} }
`, `,
], ],
[ [
'app/page.js', 'app/page.js',
`'use client' outdent`
import { Comp1 } from './comp1' 'use client'
import { Comp1 } from './comp1'
export default function Page() {
return <Comp1 /> export default function Page() {
} return <Comp1 />
}
`, `,
], ],
]) ])
) )
const pageFile = 'app/page.js' const pageFile = 'app/page.js'
const content = await next.readFile(pageFile) const content = await next.readFile(pageFile)
const withoutUseClient = content.replace("'use client'", '') const withoutUseClient = content.replace("'use client'", '')
await session.patch(pageFile, withoutUseClient) await session.patch(pageFile, withoutUseClient)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(` expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"./app/comp2.js "./app/comp2.js
'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component. 'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component.
...@@ -78,70 +78,70 @@ createNextDescribe( ...@@ -78,70 +78,70 @@ createNextDescribe(
./app/page.js" ./app/page.js"
`) `)
await cleanup() await cleanup()
}) })
it('should show error when external package imports client-only in server component', async () => { it('should show error when external package imports client-only in server component', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'node_modules/client-only-package/index.js', 'node_modules/client-only-package/index.js',
` outdent`
require("client-only") require("client-only")
`, `,
], ],
[ [
'node_modules/client-only-package/package.json', 'node_modules/client-only-package/package.json',
` outdent`
{ {
"name": "client-only-package", "name": "client-only-package",
"main": "index.js" "main": "index.js"
} }
`, `,
], ],
[ [
'app/comp1.js', 'app/comp1.js',
` outdent`
import { Comp2 } from './comp2' import { Comp2 } from './comp2'
export function Comp1() { export function Comp1() {
return <Comp2 /> return <Comp2 />
} }
`, `,
], ],
[ [
'app/comp2.js', 'app/comp2.js',
` outdent`
import "client-only-package" import "client-only-package"
export function Comp2() { export function Comp2() {
return ( return (
<div>Hello world</div> <div>Hello world</div>
) )
} }
`, `,
], ],
[ [
'app/page.js', 'app/page.js',
`'use client' outdent`
import { Comp1 } from './comp1' 'use client'
import { Comp1 } from './comp1'
export default function Page() {
return <Comp1 /> export default function Page() {
} return <Comp1 />
}
`, `,
], ],
]) ])
) )
const pageFile = 'app/page.js' const pageFile = 'app/page.js'
const content = await next.readFile(pageFile) const content = await next.readFile(pageFile)
const withoutUseClient = content.replace("'use client'", '') const withoutUseClient = content.replace("'use client'", '')
await session.patch(pageFile, withoutUseClient) await session.patch(pageFile, withoutUseClient)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(` expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"./app/comp2.js "./app/comp2.js
'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component. 'client-only' cannot be imported from a Server Component module. It should only be used from a Client Component.
...@@ -153,68 +153,68 @@ createNextDescribe( ...@@ -153,68 +153,68 @@ createNextDescribe(
./app/page.js" ./app/page.js"
`) `)
await cleanup() await cleanup()
}) })
it('should show error when external package imports server-only in client component', async () => { it('should show error when external package imports server-only in client component', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
new Map([ new Map([
[ [
'node_modules/server-only-package/index.js', 'node_modules/server-only-package/index.js',
` outdent`
require("server-only") require("server-only")
`, `,
], ],
[ [
'node_modules/server-only-package/package.json', 'node_modules/server-only-package/package.json',
` outdent`
{ {
"name": "server-only-package", "name": "server-only-package",
"main": "index.js" "main": "index.js"
} }
`, `,
], ],
[ [
'app/comp1.js', 'app/comp1.js',
` outdent`
import { Comp2 } from './comp2' import { Comp2 } from './comp2'
export function Comp1() { export function Comp1() {
return <Comp2 /> return <Comp2 />
} }
`, `,
], ],
[ [
'app/comp2.js', 'app/comp2.js',
` outdent`
import 'server-only-package' import 'server-only-package'
export function Comp2() { export function Comp2() {
return ( return (
<div>Hello world</div> <div>Hello world</div>
) )
} }
`, `,
], ],
[ [
'app/page.js', 'app/page.js',
`import { Comp1 } from './comp1' outdent`
import { Comp1 } from './comp1'
export default function Page() {
return <Comp1 /> export default function Page() {
} return <Comp1 />
}
`, `,
], ],
]) ])
) )
const file = 'app/page.js' const file = 'app/page.js'
const content = await next.readFile(file) const content = await next.readFile(file)
await session.patch(file, "'use client'\n" + content) await session.patch(file, "'use client'\n" + content)
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(` expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"./app/comp2.js "./app/comp2.js
'server-only' cannot be imported from a Client Component module. It should only be used from a Server Component. 'server-only' cannot be imported from a Client Component module. It should only be used from a Server Component.
...@@ -226,7 +226,6 @@ createNextDescribe( ...@@ -226,7 +226,6 @@ createNextDescribe(
./app/page.js" ./app/page.js"
`) `)
await cleanup() await cleanup()
}) })
} })
)
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import path from 'path' import path from 'path'
import { outdent } from 'outdent'
describe.skip('should skip for now', () => { describe.skip('Error Overlay version staleness', () => {
createNextDescribe( const { next } = nextTestSetup({
'Error Overlay version staleness', files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
{ dependencies: {
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), react: 'latest',
dependencies: { 'react-dom': 'latest',
react: 'latest',
'react-dom': 'latest',
},
skipStart: true,
}, },
({ next }) => { skipStart: true,
it('should show version staleness in runtime error', async () => { })
// Set next to outdated version
const nextPackageJson = JSON.parse( it('should show version staleness in runtime error', async () => {
await next.readFile('node_modules/next/package.json') // Set next to outdated version
) const nextPackageJson = JSON.parse(
nextPackageJson.version = '1.0.0' await next.readFile('node_modules/next/package.json')
)
const { browser, session, cleanup } = await sandbox( nextPackageJson.version = '1.0.0'
next,
new Map([ const { browser, session, cleanup } = await sandbox(
['node_modules/next/package.json', JSON.stringify(nextPackageJson)], next,
]) new Map([
) ['node_modules/next/package.json', JSON.stringify(nextPackageJson)],
])
await session.patch( )
'app/page.js',
` await session.patch(
'app/page.js',
outdent`
"use client" "use client"
import Component from '../index' import Component from '../index'
export default function Page() { export default function Page() {
setTimeout(() => { setTimeout(() => {
throw new Error("runtime error") throw new Error("runtime error")
}, 0) }, 0)
return null return null
} }
` `
) )
await session.waitForAndOpenRuntimeError() await session.waitForAndOpenRuntimeError()
expect( expect(
await browser await browser
.waitForElementByCss('.nextjs-container-build-error-version-status') .waitForElementByCss('.nextjs-container-build-error-version-status')
.text() .text()
).toMatchInlineSnapshot(`"Next.js (1.0.0) is outdated (learn more)"`) ).toMatchInlineSnapshot(`"Next.js (1.0.0) is outdated (learn more)"`)
await cleanup() await cleanup()
}) })
it('should show version staleness in render error', async () => { it('should show version staleness in render error', async () => {
// Set next to outdated version // Set next to outdated version
const nextPackageJson = JSON.parse( const nextPackageJson = JSON.parse(
await next.readFile('node_modules/next/package.json') await next.readFile('node_modules/next/package.json')
) )
nextPackageJson.version = '2.0.0' nextPackageJson.version = '2.0.0'
const { browser, session, cleanup } = await sandbox( const { browser, session, cleanup } = await sandbox(
next, next,
new Map([ new Map([
['node_modules/next/package.json', JSON.stringify(nextPackageJson)], ['node_modules/next/package.json', JSON.stringify(nextPackageJson)],
]) ])
) )
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
export default function Page() { export default function Page() {
throw new Error("render error") throw new Error("render error")
return null return null
} }
` `
) )
expect( expect(
await browser await browser
.waitForElementByCss('.nextjs-container-build-error-version-status') .waitForElementByCss('.nextjs-container-build-error-version-status')
.text() .text()
).toMatchInlineSnapshot(`"Next.js (2.0.0) is outdated (learn more)"`) ).toMatchInlineSnapshot(`"Next.js (2.0.0) is outdated (learn more)"`)
await cleanup() await cleanup()
}) })
it('should show version staleness in build error', async () => { it('should show version staleness in build error', async () => {
// Set next to outdated version // Set next to outdated version
const nextPackageJson = JSON.parse( const nextPackageJson = JSON.parse(
await next.readFile('node_modules/next/package.json') await next.readFile('node_modules/next/package.json')
) )
nextPackageJson.version = '3.0.0' nextPackageJson.version = '3.0.0'
const { browser, session, cleanup } = await sandbox( const { browser, session, cleanup } = await sandbox(
next, next,
new Map([ new Map([
['node_modules/next/package.json', JSON.stringify(nextPackageJson)], ['node_modules/next/package.json', JSON.stringify(nextPackageJson)],
]) ])
) )
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
{{{ {{{
` `
) )
expect( expect(
await browser await browser
.waitForElementByCss('.nextjs-container-build-error-version-status') .waitForElementByCss('.nextjs-container-build-error-version-status')
.text() .text()
).toMatchInlineSnapshot(`"Next.js (3.0.0) is outdated (learn more)"`) ).toMatchInlineSnapshot(`"Next.js (3.0.0) is outdated (learn more)"`)
await cleanup() await cleanup()
}) })
}
)
}) })
/* eslint-env jest */ /* eslint-env jest */
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { createNext } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import path from 'path'
// TODO: figure out why snapshots mismatch on GitHub actions // TODO: figure out why snapshots mismatch on GitHub actions
// specifically but work in docker and locally // specifically but work in docker and locally
describe.skip('ReactRefreshLogBox', () => { describe.skip('ReactRefreshLogBox', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { skipStart: true,
next = await createNext({ dependencies: {
files: {}, sass: 'latest',
skipStart: true, },
dependencies: {
sass: 'latest',
},
})
}) })
afterAll(() => next.destroy())
test('scss syntax errors', async () => { test('scss syntax errors', async () => {
const { session, cleanup } = await sandbox(next) const { session, cleanup } = await sandbox(next)
......
import { createNext } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { sandbox } from 'development-sandbox'
import { sandbox } from './helpers' import path from 'path'
describe('ReactRefreshModule', () => { describe('ReactRefreshModule', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => { skipStart: true,
next = await createNext({
files: {},
skipStart: true,
})
}) })
afterAll(() => next.destroy())
it('should allow any variable names', async () => { it('should allow any variable names', async () => {
const { session, cleanup } = await sandbox(next, new Map([])) const { session, cleanup } = await sandbox(next)
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
const variables = [ const variables = [
......
This diff is collapsed.
This diff is collapsed.
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