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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
...@@ -16,14 +13,12 @@ describe('ReactRefresh app', () => { ...@@ -16,14 +13,12 @@ describe('ReactRefresh app', () => {
}, },
skipStart: true, 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')),
beforeAll(async () => {
next = await createNext({
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,
}) })
})
afterAll(() => next.destroy())
// Module trace is only available with webpack 5 // Module trace is only available with webpack 5
test('Node.js builtins', async () => { test('Node.js builtins', async () => {
...@@ -30,14 +22,14 @@ for (const variant of ['default', 'turbo']) { ...@@ -30,14 +22,14 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const dns = require('dns') const dns = require('dns')
module.exports = dns module.exports = dns
`, `,
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -49,7 +41,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -49,7 +41,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import pkg from 'my-package' import pkg from 'my-package'
export default function Hello() { export default function Hello() {
...@@ -68,7 +60,8 @@ for (const variant of ['default', 'turbo']) { ...@@ -68,7 +60,8 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
`import Comp from 'b' outdent`
import Comp from 'b'
export default function Oops() { export default function Oops() {
return ( return (
<div> <div>
...@@ -92,7 +85,8 @@ for (const variant of ['default', 'turbo']) { ...@@ -92,7 +85,8 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import Comp from 'b' import Comp from 'b'
export default function Oops() { export default function Oops() {
return ( return (
...@@ -118,7 +112,8 @@ for (const variant of ['default', 'turbo']) { ...@@ -118,7 +112,8 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import './non-existent.css' import './non-existent.css'
export default function Page(props) { export default function Page(props) {
return <p>index page</p> return <p>index page</p>
...@@ -134,7 +129,8 @@ for (const variant of ['default', 'turbo']) { ...@@ -134,7 +129,8 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
export default function Page(props) { export default function Page(props) {
return <p>index page</p> return <p>index page</p>
} }
...@@ -147,5 +143,4 @@ for (const variant of ['default', 'turbo']) { ...@@ -147,5 +143,4 @@ for (const variant of ['default', 'turbo']) {
await cleanup() 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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
skipStart: true,
dependencies: { dependencies: {
sass: 'latest', sass: 'latest',
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true,
}) })
})
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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
...@@ -17,15 +14,13 @@ describe.skip('ReactRefreshLogBox app', () => { ...@@ -17,15 +14,13 @@ describe.skip('ReactRefreshLogBox app', () => {
}, },
skipStart: true, 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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
...@@ -15,8 +12,6 @@ describe('ReactRefreshModule app', () => { ...@@ -15,8 +12,6 @@ describe('ReactRefreshModule app', () => {
}, },
skipStart: true, 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`
'use client'
import { default as ${variable} } from 'next/link' import { default as ${variable} } from 'next/link'
export default function Page() { export default function Page() {
return null 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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
skipStart: true,
dependencies: { dependencies: {
'styled-components': '5.1.0', 'styled-components': '5.1.0',
'@next/mdx': 'canary', '@next/mdx': 'canary',
...@@ -19,9 +15,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -19,9 +15,8 @@ describe('ReactRefreshRegression app', () => {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true,
}) })
})
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,7 +352,8 @@ describe('ReactRefreshRegression app', () => { ...@@ -350,7 +352,8 @@ 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`
'use client'
import MDX from './content.mdx' import MDX from './content.mdx'
export default function Page() { export default function Page() {
return <div id="content"><MDX /></div> return <div id="content"><MDX /></div>
......
/* 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({
beforeAll(async () => {
next = await createNext({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')), files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
...@@ -16,8 +13,6 @@ describe('ReactRefreshRequire app', () => { ...@@ -16,8 +13,6 @@ describe('ReactRefreshRequire app', () => {
}, },
skipStart: true, 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,7 +82,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -87,7 +82,7 @@ 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');
...@@ -144,7 +139,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -144,7 +139,7 @@ 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() {};
...@@ -165,7 +160,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -165,7 +160,7 @@ 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() {};
` `
...@@ -203,7 +198,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -203,7 +198,7 @@ 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';
...@@ -215,7 +210,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -215,7 +210,7 @@ describe('ReactRefreshRequire app', () => {
) )
await session.write( await session.write(
'middleA.js', 'middleA.js',
` outdent`
log.push('init MiddleAV1'); log.push('init MiddleAV1');
import './leaf'; import './leaf';
...@@ -225,7 +220,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -225,7 +220,7 @@ describe('ReactRefreshRequire app', () => {
) )
await session.write( await session.write(
'middleB.js', 'middleB.js',
` outdent`
log.push('init MiddleBV1'); log.push('init MiddleBV1');
import './leaf'; import './leaf';
...@@ -290,7 +285,7 @@ describe('ReactRefreshRequire app', () => { ...@@ -290,7 +285,7 @@ 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';
......
...@@ -15,11 +15,10 @@ Import trace for requested module: ...@@ -15,11 +15,10 @@ 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>
...@@ -28,11 +27,10 @@ https://nextjs.org/docs/messages/module-not-found" ...@@ -28,11 +27,10 @@ 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 | }
...@@ -41,7 +39,7 @@ https://nextjs.org/docs/messages/module-not-found" ...@@ -41,7 +39,7 @@ 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,8 +17,8 @@ createNextDescribe( ...@@ -18,8 +17,8 @@ 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 () => { it('should handle successive HMR changes with errors correctly', async () => {
const browser = await retry( const browser = await retry(
() => next.browser('/2020/develop-preview-test'), () => next.browser('/2020/develop-preview-test'),
...@@ -61,5 +60,4 @@ createNextDescribe( ...@@ -61,5 +60,4 @@ createNextDescribe(
/A few years ago I tweeted/ /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 () => { it('should show a component stack on hydration error', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/component.js', 'app/component.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Component() { export default function Component() {
...@@ -30,11 +30,11 @@ createNextDescribe( ...@@ -30,11 +30,11 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from './component' import Component from './component'
export default function Mismatch() { export default function Mismatch() {
return ( return (
...@@ -43,7 +43,7 @@ createNextDescribe( ...@@ -43,7 +43,7 @@ createNextDescribe(
</main> </main>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -59,5 +59,4 @@ createNextDescribe( ...@@ -59,5 +59,4 @@ createNextDescribe(
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'
...@@ -38,5 +38,4 @@ createNextDescribe( ...@@ -38,5 +38,4 @@ createNextDescribe(
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,17 +25,16 @@ async function clickImportTraceFiles(browser: any) { ...@@ -24,17 +25,16 @@ 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(
...@@ -42,7 +42,7 @@ createNextDescribe( ...@@ -42,7 +42,7 @@ createNextDescribe(
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 />
...@@ -63,8 +63,10 @@ createNextDescribe( ...@@ -63,8 +63,10 @@ createNextDescribe(
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)
...@@ -81,7 +83,7 @@ createNextDescribe( ...@@ -81,7 +83,7 @@ createNextDescribe(
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 />
...@@ -104,8 +106,10 @@ createNextDescribe( ...@@ -104,8 +106,10 @@ createNextDescribe(
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)
...@@ -122,7 +126,7 @@ createNextDescribe( ...@@ -122,7 +126,7 @@ createNextDescribe(
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 />
...@@ -145,8 +149,10 @@ createNextDescribe( ...@@ -145,8 +149,10 @@ createNextDescribe(
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)
...@@ -155,5 +161,4 @@ createNextDescribe( ...@@ -155,5 +161,4 @@ createNextDescribe(
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 () => { it('should be possible to click url in build error', async () => {
const { session, browser, cleanup } = await sandbox(next) const { session, browser, cleanup } = await sandbox(next)
...@@ -44,7 +44,8 @@ createNextDescribe( ...@@ -44,7 +44,8 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
export default function Page() { export default function Page() {
return typeof window === 'undefined' ? 'HELLO' : 'WORLD' return typeof window === 'undefined' ? 'HELLO' : 'WORLD'
} }
...@@ -55,9 +56,7 @@ createNextDescribe( ...@@ -55,9 +56,7 @@ createNextDescribe(
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 text = await link.text()
const href = await link.getAttribute('href') const href = await link.getAttribute('href')
expect(text).toEqual( expect(text).toEqual(
...@@ -69,5 +68,4 @@ createNextDescribe( ...@@ -69,5 +68,4 @@ createNextDescribe(
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'
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference // https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
createNextDescribe(
'Error overlay for hydration errors', describe('Error overlay for hydration errors', () => {
{ 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 correct hydration error when client and server render different text', async () => { it('should show correct hydration error when client and server render different text', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
...@@ -31,7 +32,7 @@ createNextDescribe( ...@@ -31,7 +32,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -55,7 +56,7 @@ createNextDescribe( ...@@ -55,7 +56,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
...@@ -65,7 +66,7 @@ createNextDescribe( ...@@ -65,7 +66,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -88,7 +89,7 @@ createNextDescribe( ...@@ -88,7 +89,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
...@@ -100,7 +101,7 @@ createNextDescribe( ...@@ -100,7 +101,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -124,7 +125,7 @@ createNextDescribe( ...@@ -124,7 +125,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
...@@ -134,7 +135,7 @@ createNextDescribe( ...@@ -134,7 +135,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -158,13 +159,13 @@ createNextDescribe( ...@@ -158,13 +159,13 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
return <div className="parent">{!isClient && "only"}</div>; return <div className="parent">{!isClient && "only"}</div>;
} }
`, `,
], ],
]) ])
) )
...@@ -188,7 +189,7 @@ createNextDescribe( ...@@ -188,7 +189,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
'use client' 'use client'
import React from "react" import React from "react"
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
...@@ -203,7 +204,7 @@ createNextDescribe( ...@@ -203,7 +204,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -220,5 +221,4 @@ createNextDescribe( ...@@ -220,5 +221,4 @@ createNextDescribe(
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,15 +14,15 @@ createNextDescribe( ...@@ -14,15 +14,15 @@ 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() {
...@@ -32,7 +32,7 @@ createNextDescribe( ...@@ -32,7 +32,7 @@ createNextDescribe(
], ],
[ [
'app/comp2.js', 'app/comp2.js',
` outdent`
export function Comp2() { export function Comp2() {
return ( return (
<div> <div>
...@@ -44,12 +44,12 @@ createNextDescribe( ...@@ -44,12 +44,12 @@ createNextDescribe(
</div> </div>
) )
} }
`, `,
], ],
[ [
'app/page.js', 'app/page.js',
`'use client' outdent`
'use client'
import { Comp1 } from './comp1' import { Comp1 } from './comp1'
export default function Page() { export default function Page() {
...@@ -87,13 +87,13 @@ createNextDescribe( ...@@ -87,13 +87,13 @@ createNextDescribe(
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"
...@@ -102,7 +102,7 @@ createNextDescribe( ...@@ -102,7 +102,7 @@ createNextDescribe(
], ],
[ [
'app/comp1.js', 'app/comp1.js',
` outdent`
import { Comp2 } from './comp2' import { Comp2 } from './comp2'
export function Comp1() { export function Comp1() {
...@@ -112,19 +112,19 @@ createNextDescribe( ...@@ -112,19 +112,19 @@ createNextDescribe(
], ],
[ [
'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`
'use client'
import { Comp1 } from './comp1' import { Comp1 } from './comp1'
export default function Page() { export default function Page() {
...@@ -162,13 +162,13 @@ createNextDescribe( ...@@ -162,13 +162,13 @@ createNextDescribe(
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"
...@@ -177,7 +177,7 @@ createNextDescribe( ...@@ -177,7 +177,7 @@ createNextDescribe(
], ],
[ [
'app/comp1.js', 'app/comp1.js',
` outdent`
import { Comp2 } from './comp2' import { Comp2 } from './comp2'
export function Comp1() { export function Comp1() {
...@@ -187,19 +187,19 @@ createNextDescribe( ...@@ -187,19 +187,19 @@ createNextDescribe(
], ],
[ [
'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() { export default function Page() {
return <Comp1 /> return <Comp1 />
...@@ -228,5 +228,4 @@ createNextDescribe( ...@@ -228,5 +228,4 @@ createNextDescribe(
await cleanup() await cleanup()
}) })
} })
)
import { createNextDescribe, FileRef } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils' import { check } from 'next-test-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 - RSC build errors', () => {
'Error overlay - RSC build errors', const { next } = nextTestSetup({
{
files: new FileRef(path.join(__dirname, 'fixtures', 'rsc-build-errors')), files: new FileRef(path.join(__dirname, 'fixtures', 'rsc-build-errors')),
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should throw an error when getServerSideProps is used', async () => { it('should throw an error when getServerSideProps is used', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
...@@ -331,7 +331,8 @@ createNextDescribe( ...@@ -331,7 +331,8 @@ createNextDescribe(
it('should freeze parent resolved metadata to avoid mutating in generateMetadata', async () => { it('should freeze parent resolved metadata to avoid mutating in generateMetadata', async () => {
const pagePath = 'app/metadata/mutate/page.js' const pagePath = 'app/metadata/mutate/page.js'
const content = `export default function page(props) { const content = outdent`
export default function page(props) {
return <p>mutate</p> return <p>mutate</p>
} }
...@@ -341,7 +342,8 @@ createNextDescribe( ...@@ -341,7 +342,8 @@ createNextDescribe(
return { return {
...parentMetadata, ...parentMetadata,
} }
}` }
`
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
...@@ -375,7 +377,7 @@ createNextDescribe( ...@@ -375,7 +377,7 @@ createNextDescribe(
['node_modules/client-package/index.js', "import './module1.js'"], ['node_modules/client-package/index.js', "import './module1.js'"],
[ [
'node_modules/client-package/package.json', 'node_modules/client-package/package.json',
` outdent`
{ {
"name": "client-package", "name": "client-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -385,11 +387,12 @@ createNextDescribe( ...@@ -385,11 +387,12 @@ createNextDescribe(
['app/Component.js', "import 'client-package'"], ['app/Component.js', "import 'client-package'"],
[ [
'app/page.js', 'app/page.js',
` outdent`
import './Component.js' import './Component.js'
export default function Page() { export default function Page() {
return <p>Hello world</p> return <p>Hello world</p>
}`, }
`,
], ],
]) ])
) )
...@@ -420,5 +423,4 @@ createNextDescribe( ...@@ -420,5 +423,4 @@ createNextDescribe(
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 { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { outdent } from 'outdent'
createNextDescribe( describe('Error Overlay for server components', () => {
'Error Overlay for server components', 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 }) => {
describe('createContext called in Server Component', () => { describe('createContext called in Server Component', () => {
it('should show error when React.createContext is called', async () => { it('should show error when React.createContext is called', async () => {
const { browser, cleanup } = await sandbox( const { browser, cleanup } = await sandbox(
...@@ -22,7 +22,7 @@ createNextDescribe( ...@@ -22,7 +22,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import React from 'react' import React from 'react'
const Context = React.createContext() const Context = React.createContext()
export default function Page() { export default function Page() {
...@@ -33,7 +33,8 @@ createNextDescribe( ...@@ -33,7 +33,8 @@ createNextDescribe(
</Context.Provider> </Context.Provider>
</> </>
) )
}`, }
`,
], ],
]) ])
) )
...@@ -62,14 +63,14 @@ createNextDescribe( ...@@ -62,14 +63,14 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const React = require('react') const React = require('react')
module.exports = React.createContext() module.exports = React.createContext()
`, `,
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -78,7 +79,7 @@ createNextDescribe( ...@@ -78,7 +79,7 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Context from 'my-package' import Context from 'my-package'
export default function Page() { export default function Page() {
return ( return (
...@@ -88,7 +89,8 @@ createNextDescribe( ...@@ -88,7 +89,8 @@ createNextDescribe(
</Context.Provider> </Context.Provider>
</> </>
) )
}`, }
`,
], ],
]) ])
) )
...@@ -117,14 +119,14 @@ createNextDescribe( ...@@ -117,14 +119,14 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const { createContext } = require('react') const { createContext } = require('react')
module.exports = createContext() module.exports = createContext()
`, `,
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -133,7 +135,7 @@ createNextDescribe( ...@@ -133,7 +135,7 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Context from 'my-package' import Context from 'my-package'
export default function Page() { export default function Page() {
return ( return (
...@@ -143,7 +145,8 @@ createNextDescribe( ...@@ -143,7 +145,8 @@ createNextDescribe(
</Context.Provider> </Context.Provider>
</> </>
) )
}`, }
`,
], ],
]) ])
) )
...@@ -173,12 +176,13 @@ createNextDescribe( ...@@ -173,12 +176,13 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import React from 'react' import React from 'react'
export default function Page() { export default function Page() {
const ref = React.useRef() const ref = React.useRef()
return "Hello world" return "Hello world"
}`, }
`,
], ],
]) ])
) )
...@@ -207,7 +211,7 @@ createNextDescribe( ...@@ -207,7 +211,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const React = require('react') const React = require('react')
module.exports = function Component() { module.exports = function Component() {
const [state, useState] = React.useState() const [state, useState] = React.useState()
...@@ -217,7 +221,7 @@ createNextDescribe( ...@@ -217,7 +221,7 @@ createNextDescribe(
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -226,11 +230,12 @@ createNextDescribe( ...@@ -226,11 +230,12 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from 'my-package' import Component from 'my-package'
export default function Page() { export default function Page() {
return <Component /> return <Component />
}`, }
`,
], ],
]) ])
) )
...@@ -259,7 +264,7 @@ createNextDescribe( ...@@ -259,7 +264,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const { useEffect } = require('react') const { useEffect } = require('react')
module.exports = function Component() { module.exports = function Component() {
useEffect(() => {}, []) useEffect(() => {}, [])
...@@ -269,7 +274,7 @@ createNextDescribe( ...@@ -269,7 +274,7 @@ createNextDescribe(
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -278,11 +283,12 @@ createNextDescribe( ...@@ -278,11 +283,12 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from 'my-package' import Component from 'my-package'
export default function Page() { export default function Page() {
return <Component /> return <Component />
}`, }
`,
], ],
]) ])
) )
...@@ -313,7 +319,7 @@ createNextDescribe( ...@@ -313,7 +319,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import React from 'react' import React from 'react'
export default class Page extends React.Component { export default class Page extends React.Component {
render() { render() {
...@@ -349,7 +355,7 @@ createNextDescribe( ...@@ -349,7 +355,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const React = require('react') const React = require('react')
module.exports = class extends React.PureComponent { module.exports = class extends React.PureComponent {
render() { render() {
...@@ -360,7 +366,7 @@ createNextDescribe( ...@@ -360,7 +366,7 @@ createNextDescribe(
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -369,11 +375,12 @@ createNextDescribe( ...@@ -369,11 +375,12 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from 'my-package' import Component from 'my-package'
export default function Page() { export default function Page() {
return <Component /> return <Component />
}`, }
`,
], ],
]) ])
) )
...@@ -402,7 +409,7 @@ createNextDescribe( ...@@ -402,7 +409,7 @@ createNextDescribe(
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const { Component } = require('react') const { Component } = require('react')
module.exports = class extends Component { module.exports = class extends Component {
render() { render() {
...@@ -413,7 +420,7 @@ createNextDescribe( ...@@ -413,7 +420,7 @@ createNextDescribe(
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -422,11 +429,12 @@ createNextDescribe( ...@@ -422,11 +429,12 @@ createNextDescribe(
], ],
[ [
'app/page.js', 'app/page.js',
` outdent`
import Component from 'my-package' import Component from 'my-package'
export default function Page() { export default function Page() {
return <Component /> return <Component />
}`, }
`,
], ],
]) ])
) )
...@@ -465,12 +473,13 @@ createNextDescribe( ...@@ -465,12 +473,13 @@ createNextDescribe(
new Map([ new Map([
[ [
'app/page.js', 'app/page.js',
` outdent`
import { ${hook} } from 'next/navigation' import { ${hook} } from 'next/navigation'
export default function Page() { export default function Page() {
${hook}() ${hook}()
return "Hello world" return "Hello world"
}`, }
`,
], ],
]) ])
) )
...@@ -493,5 +502,4 @@ createNextDescribe( ...@@ -493,5 +502,4 @@ createNextDescribe(
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')), 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 version staleness in runtime error', async () => { it('should show version staleness in runtime error', async () => {
// Set next to outdated version // Set next to outdated version
const nextPackageJson = JSON.parse( const nextPackageJson = JSON.parse(
...@@ -31,7 +30,7 @@ describe.skip('should skip for now', () => { ...@@ -31,7 +30,7 @@ describe.skip('should skip for now', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
"use client" "use client"
import Component from '../index' import Component from '../index'
export default function Page() { export default function Page() {
...@@ -69,7 +68,7 @@ describe.skip('should skip for now', () => { ...@@ -69,7 +68,7 @@ describe.skip('should skip for now', () => {
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
...@@ -102,7 +101,7 @@ describe.skip('should skip for now', () => { ...@@ -102,7 +101,7 @@ describe.skip('should skip for now', () => {
await session.patch( await session.patch(
'app/page.js', 'app/page.js',
` outdent`
{{{ {{{
` `
) )
...@@ -115,6 +114,4 @@ describe.skip('should skip for now', () => { ...@@ -115,6 +114,4 @@ describe.skip('should skip for now', () => {
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'
import { outdent } from 'outdent'
describe('ReactRefresh', () => { describe('ReactRefresh', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, 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() {
...@@ -42,7 +38,7 @@ describe('ReactRefresh', () => { ...@@ -42,7 +38,7 @@ describe('ReactRefresh', () => {
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() {
...@@ -74,7 +70,7 @@ describe('ReactRefresh', () => { ...@@ -74,7 +70,7 @@ describe('ReactRefresh', () => {
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';
...@@ -90,7 +86,7 @@ describe('ReactRefresh', () => { ...@@ -90,7 +86,7 @@ describe('ReactRefresh', () => {
await session.write( await session.write(
'SurveyOverview.js', 'SurveyOverview.js',
` outdent`
const SurveyOverview = () => { const SurveyOverview = () => {
return 100; return 100;
}; };
...@@ -101,7 +97,7 @@ describe('ReactRefresh', () => { ...@@ -101,7 +97,7 @@ describe('ReactRefresh', () => {
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';
...@@ -117,7 +113,7 @@ describe('ReactRefresh', () => { ...@@ -117,7 +113,7 @@ describe('ReactRefresh', () => {
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';
...@@ -142,7 +138,7 @@ describe('ReactRefresh', () => { ...@@ -142,7 +138,7 @@ describe('ReactRefresh', () => {
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 = {};
...@@ -156,7 +152,7 @@ describe('ReactRefresh', () => { ...@@ -156,7 +152,7 @@ describe('ReactRefresh', () => {
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';
...@@ -175,7 +171,7 @@ describe('ReactRefresh', () => { ...@@ -175,7 +171,7 @@ describe('ReactRefresh', () => {
let didFullRefresh = !(await session.patch( let didFullRefresh = !(await session.patch(
'SurveyOverview.js', 'SurveyOverview.js',
` outdent`
const SurveyOverview = () => { const SurveyOverview = () => {
return 200; return 200;
}; };
...@@ -191,7 +187,7 @@ describe('ReactRefresh', () => { ...@@ -191,7 +187,7 @@ describe('ReactRefresh', () => {
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';
...@@ -211,7 +207,7 @@ describe('ReactRefresh', () => { ...@@ -211,7 +207,7 @@ describe('ReactRefresh', () => {
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 } from 'e2e-utils' import { FileRef, nextTestSetup } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base' import { describeVariants as describe } from 'next-test-utils'
import { getSnapshotTestDescribe } from 'next-test-utils' import { outdent } from 'outdent'
import path from 'path'
for (const variant of ['default', 'turbo']) {
getSnapshotTestDescribe(variant)(`ReactRefreshLogBox ${variant}`, () => { describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
}) })
})
afterAll(() => next.destroy())
test('empty _app shows logbox', async () => { test('empty _app shows logbox', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
new Map([ new Map([['pages/_app.js', ``]])
[
'pages/_app.js',
`
`,
],
])
) )
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot( expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
...@@ -34,7 +22,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -34,7 +22,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/_app.js', 'pages/_app.js',
` outdent`
function MyApp({ Component, pageProps }) { function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />; return <Component {...pageProps} />;
} }
...@@ -48,14 +36,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -48,14 +36,7 @@ for (const variant of ['default', 'turbo']) {
test('empty _document shows logbox', async () => { test('empty _document shows logbox', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
new Map([ new Map([['pages/_document.js', ``]])
[
'pages/_document.js',
`
`,
],
])
) )
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxDescription()).toMatchInlineSnapshot( expect(await session.getRedboxDescription()).toMatchInlineSnapshot(
...@@ -64,7 +45,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -64,7 +45,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/_document.js', 'pages/_document.js',
` outdent`
import Document, { Html, Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { class MyDocument extends Document {
...@@ -99,7 +80,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -99,7 +80,7 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'pages/_app.js', 'pages/_app.js',
` outdent`
function MyApp({ Component, pageProps }) { function MyApp({ Component, pageProps }) {
return <<Component {...pageProps} />; return <<Component {...pageProps} />;
} }
...@@ -117,24 +98,20 @@ for (const variant of ['default', 'turbo']) { ...@@ -117,24 +98,20 @@ for (const variant of ['default', 'turbo']) {
Error: Error:
x Expression expected x Expression expected
,-[TEST_DIR/pages/_app.js:1:1] ,-[TEST_DIR/pages/_app.js:1:1]
1 | 1 | function MyApp({ Component, pageProps }) {
2 | function MyApp({ Component, pageProps }) { 2 | return <<Component {...pageProps} />;
3 | return <<Component {...pageProps} />;
: ^ : ^
4 | } 3 | }
5 | export default MyApp 4 | export default MyApp
6 |
\`---- \`----
x Expression expected x Expression expected
,-[TEST_DIR/pages/_app.js:1:1] ,-[TEST_DIR/pages/_app.js:1:1]
1 | 1 | function MyApp({ Component, pageProps }) {
2 | function MyApp({ Component, pageProps }) { 2 | return <<Component {...pageProps} />;
3 | return <<Component {...pageProps} />;
: ^^^^^^^^^ : ^^^^^^^^^
4 | } 3 | }
5 | export default MyApp 4 | export default MyApp
6 |
\`---- \`----
Caused by: Caused by:
...@@ -144,7 +121,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -144,7 +121,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/_app.js', 'pages/_app.js',
` outdent`
function MyApp({ Component, pageProps }) { function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />; return <Component {...pageProps} />;
} }
...@@ -161,7 +138,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -161,7 +138,7 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'pages/_document.js', 'pages/_document.js',
` outdent`
import Document, { Html, Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {{ class MyDocument extends Document {{
...@@ -197,14 +174,13 @@ for (const variant of ['default', 'turbo']) { ...@@ -197,14 +174,13 @@ for (const variant of ['default', 'turbo']) {
Error: Error:
x Unexpected token \`{\`. Expected identifier, string literal, numeric literal or [ for the computed key x Unexpected token \`{\`. Expected identifier, string literal, numeric literal or [ for the computed key
,-[TEST_DIR/pages/_document.js:1:1] ,-[TEST_DIR/pages/_document.js:1:1]
1 | 1 | import Document, { Html, Head, Main, NextScript } from 'next/document'
2 | import Document, { Html, Head, Main, NextScript } from 'next/document' 2 |
3 | 3 | class MyDocument extends Document {{
4 | class MyDocument extends Document {{
: ^ : ^
5 | static async getInitialProps(ctx) { 4 | static async getInitialProps(ctx) {
6 | const initialProps = await Document.getInitialProps(ctx) 5 | const initialProps = await Document.getInitialProps(ctx)
7 | return { ...initialProps } 6 | return { ...initialProps }
\`---- \`----
Caused by: Caused by:
...@@ -214,7 +190,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -214,7 +190,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/_document.js', 'pages/_document.js',
` outdent`
import Document, { Html, Head, Main, NextScript } from 'next/document' import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document { class MyDocument extends Document {
...@@ -242,5 +218,4 @@ for (const variant of ['default', 'turbo']) { ...@@ -242,5 +218,4 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
await cleanup() await cleanup()
}) })
}) })
}
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 { describeVariants as describe } from 'next-test-utils'
import { getSnapshotTestDescribe } from 'next-test-utils' import { outdent } from 'outdent'
import path from 'path'
for (const variant of ['default', 'turbo']) {
getSnapshotTestDescribe(variant)(`ReactRefreshLogBox ${variant}`, () => { describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
}) })
})
afterAll(() => next.destroy())
// Module trace is only available with webpack 5 // Module trace is only available with webpack 5
test('Node.js builtins', async () => { test('Node.js builtins', async () => {
...@@ -22,14 +17,14 @@ for (const variant of ['default', 'turbo']) { ...@@ -22,14 +17,14 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'node_modules/my-package/index.js', 'node_modules/my-package/index.js',
` outdent`
const dns = require('dns') const dns = require('dns')
module.exports = dns module.exports = dns
`, `,
], ],
[ [
'node_modules/my-package/package.json', 'node_modules/my-package/package.json',
` outdent`
{ {
"name": "my-package", "name": "my-package",
"version": "0.0.1" "version": "0.0.1"
...@@ -41,7 +36,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -41,7 +36,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import pkg from 'my-package' import pkg from 'my-package'
export default function Hello() { export default function Hello() {
...@@ -60,7 +55,9 @@ for (const variant of ['default', 'turbo']) { ...@@ -60,7 +55,9 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
`import Comp from 'b' outdent`
import Comp from 'b'
export default function Oops() { export default function Oops() {
return ( return (
<div> <div>
...@@ -84,7 +81,9 @@ for (const variant of ['default', 'turbo']) { ...@@ -84,7 +81,9 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/index.js', 'pages/index.js',
`import Comp from 'b' outdent`
import Comp from 'b'
export default function Oops() { export default function Oops() {
return ( return (
<div> <div>
...@@ -109,7 +108,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -109,7 +108,7 @@ for (const variant of ['default', 'turbo']) {
new Map([ new Map([
[ [
'pages/_app.js', 'pages/_app.js',
` outdent`
import './non-existent.css' import './non-existent.css'
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }) {
...@@ -119,7 +118,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -119,7 +118,7 @@ for (const variant of ['default', 'turbo']) {
], ],
[ [
'pages/index.js', 'pages/index.js',
` outdent`
export default function Page(props) { export default function Page(props) {
return <p>index page</p> return <p>index page</p>
} }
...@@ -134,7 +133,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -134,7 +133,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'pages/_app.js', 'pages/_app.js',
` outdent`
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }) {
return <Component {...pageProps} /> return <Component {...pageProps} />
} }
...@@ -147,5 +146,4 @@ for (const variant of ['default', 'turbo']) { ...@@ -147,5 +146,4 @@ for (const variant of ['default', 'turbo']) {
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 () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
dependencies: { dependencies: {
sass: 'latest', 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)
......
/* 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 { describeVariants as describe } from 'next-test-utils'
import { getSnapshotTestDescribe } from 'next-test-utils' import path from 'path'
import { outdent } from 'outdent'
for (const variant of ['default', 'turbo']) {
getSnapshotTestDescribe(variant)(`ReactRefreshLogBox ${variant}`, () => { describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
}) })
})
afterAll(() => next.destroy())
test('should strip whitespace correctly with newline', async () => { test('should strip whitespace correctly with newline', async () => {
const { session, cleanup } = await sandbox(next) const { session, cleanup } = await sandbox(next)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default function Page() { export default function Page() {
return ( return (
<> <>
...@@ -54,7 +49,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -54,7 +49,7 @@ for (const variant of ['default', 'turbo']) {
// We start here. // We start here.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
render() { render() {
...@@ -72,7 +67,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -72,7 +67,7 @@ for (const variant of ['default', 'turbo']) {
// Add a throw in module init phase: // Add a throw in module init phase:
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
// top offset for snapshot // top offset for snapshot
import * as React from 'react'; import * as React from 'react';
throw new Error('no') throw new Error('no')
...@@ -97,7 +92,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -97,7 +92,7 @@ for (const variant of ['default', 'turbo']) {
await session.write( await session.write(
'FunctionDefault.js', 'FunctionDefault.js',
` outdent`
export default function FunctionDefault() { export default function FunctionDefault() {
return <h2>hello</h2> return <h2>hello</h2>
} }
...@@ -105,7 +100,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -105,7 +100,7 @@ for (const variant of ['default', 'turbo']) {
) )
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import FunctionDefault from './FunctionDefault.js' import FunctionDefault from './FunctionDefault.js'
import * as React from 'react' import * as React from 'react'
class ErrorBoundary extends React.Component { class ErrorBoundary extends React.Component {
...@@ -164,7 +159,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -164,7 +159,7 @@ for (const variant of ['default', 'turbo']) {
// Make a react build-time error. // Make a react build-time error.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default function FunctionNamed() { export default function FunctionNamed() {
return <div>{{}}</div> return <div>{{}}</div>
}` }`
...@@ -186,7 +181,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -186,7 +181,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
return ( return (
<div> <div>
...@@ -201,7 +196,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -201,7 +196,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
return ( return (
<div> <div>
...@@ -220,21 +215,20 @@ for (const variant of ['default', 'turbo']) { ...@@ -220,21 +215,20 @@ for (const variant of ['default', 'turbo']) {
"./index.js "./index.js
Error: Error:
x Unexpected token. Did you mean \`{'}'}\` or \`&rbrace;\`? x Unexpected token. Did you mean \`{'}'}\` or \`&rbrace;\`?
,-[TEST_DIR/index.js:5:1] ,-[TEST_DIR/index.js:4:1]
5 | <p>lol</p> 4 | <p>lol</p>
6 | div 5 | div
7 | ) 6 | )
8 | } 7 | }
: ^ : ^
9 |
\`---- \`----
x Unexpected eof x Unexpected eof
,-[TEST_DIR/index.js:6:1] ,-[TEST_DIR/index.js:4:1]
6 | div 4 | <p>lol</p>
7 | ) 5 | div
8 | } 6 | )
9 | 7 | }
\`---- \`----
Caused by: Caused by:
...@@ -255,7 +249,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -255,7 +249,7 @@ for (const variant of ['default', 'turbo']) {
await session.write( await session.write(
'Child.js', 'Child.js',
` outdent`
export default function ClickCount() { export default function ClickCount() {
return <p>hello</p> return <p>hello</p>
} }
...@@ -264,7 +258,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -264,7 +258,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Child from './Child'; import Child from './Child';
export default function Home() { export default function Home() {
...@@ -284,7 +278,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -284,7 +278,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'Child.js', 'Child.js',
` outdent`
import { Component } from 'react'; import { Component } from 'react';
export default class ClickCount extends Component { export default class ClickCount extends Component {
render() { render() {
...@@ -299,7 +293,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -299,7 +293,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'Child.js', 'Child.js',
` outdent`
import { Component } from 'react'; import { Component } from 'react';
export default class ClickCount extends Component { export default class ClickCount extends Component {
render() { render() {
...@@ -323,7 +317,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -323,7 +317,7 @@ for (const variant of ['default', 'turbo']) {
await session.write('index.module.css', `.button {}`) await session.write('index.module.css', `.button {}`)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import './index.module.css'; import './index.module.css';
export default () => { export default () => {
return ( return (
...@@ -361,7 +355,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -361,7 +355,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback } from 'react' import { useCallback } from 'react'
export default function Index() { export default function Index() {
...@@ -407,7 +401,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -407,7 +401,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback } from 'react' import { useCallback } from 'react'
export default function Index() { export default function Index() {
...@@ -453,7 +447,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -453,7 +447,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback } from 'react' import { useCallback } from 'react'
export default function Index() { export default function Index() {
...@@ -499,7 +493,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -499,7 +493,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback } from 'react' import { useCallback } from 'react'
export default function Index() { export default function Index() {
...@@ -559,7 +553,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -559,7 +553,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import { useCallback } from 'react' import { useCallback } from 'react'
export default function Index() { export default function Index() {
...@@ -625,7 +619,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -625,7 +619,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
throw {'a': 1, 'b': 'x'}; throw {'a': 1, 'b': 'x'};
return ( return (
...@@ -643,7 +637,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -643,7 +637,7 @@ for (const variant of ['default', 'turbo']) {
// fix previous error // fix previous error
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
return ( return (
<div>hello</div> <div>hello</div>
...@@ -654,7 +648,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -654,7 +648,7 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
class Hello {} class Hello {}
export default () => { export default () => {
...@@ -673,7 +667,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -673,7 +667,7 @@ for (const variant of ['default', 'turbo']) {
// fix previous error // fix previous error
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
return ( return (
<div>hello</div> <div>hello</div>
...@@ -684,7 +678,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -684,7 +678,7 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
throw "string error" throw "string error"
return ( return (
...@@ -701,7 +695,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -701,7 +695,7 @@ for (const variant of ['default', 'turbo']) {
// fix previous error // fix previous error
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
return ( return (
<div>hello</div> <div>hello</div>
...@@ -712,7 +706,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -712,7 +706,7 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
export default () => { export default () => {
throw null throw null
return ( return (
...@@ -728,5 +722,4 @@ for (const variant of ['default', 'turbo']) { ...@@ -728,5 +722,4 @@ for (const variant of ['default', 'turbo']) {
await cleanup() await cleanup()
}) })
}) })
}
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'
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', () => { describe.skip('ReactRefreshLogBox', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, 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() {
...@@ -59,7 +55,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -59,7 +55,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -75,7 +71,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -75,7 +71,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -87,7 +83,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -87,7 +83,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -111,7 +107,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -111,7 +107,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -135,7 +131,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -135,7 +131,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -159,7 +155,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -159,7 +155,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -184,7 +180,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -184,7 +180,7 @@ describe.skip('ReactRefreshLogBox', () => {
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() {
...@@ -215,7 +211,7 @@ describe.skip('ReactRefreshLogBox', () => { ...@@ -215,7 +211,7 @@ describe.skip('ReactRefreshLogBox', () => {
await session.patch( await session.patch(
'pages/index.js', 'pages/index.js',
` outdent`
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 } 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 () => {
next = await createNext({
files: {},
skipStart: true, 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 = [
......
/* 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 { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { outdent } from 'outdent'
import path from 'path'
describe('ReactRefreshRegression', () => { describe('ReactRefreshRegression', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
dependencies: { dependencies: {
'styled-components': '5.1.0', 'styled-components': '5.1.0',
...@@ -17,15 +15,13 @@ describe('ReactRefreshRegression', () => { ...@@ -17,15 +15,13 @@ describe('ReactRefreshRegression', () => {
'@mdx-js/loader': '0.18.0', '@mdx-js/loader': '0.18.0',
}, },
}) })
})
afterAll(() => next.destroy())
// https://github.com/vercel/next.js/issues/12422 // https://github.com/vercel/next.js/issues/12422
test('styled-components hydration mismatch', async () => { test('styled-components hydration mismatch', async () => {
const files = new Map() const files = new Map([
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'
...@@ -55,8 +51,9 @@ describe('ReactRefreshRegression', () => { ...@@ -55,8 +51,9 @@ describe('ReactRefreshRegression', () => {
} }
} }
} }
` `,
) ],
])
const { session, cleanup } = await sandbox(next, files) const { session, cleanup } = await sandbox(next, files)
...@@ -299,28 +296,31 @@ describe('ReactRefreshRegression', () => { ...@@ -299,28 +296,31 @@ describe('ReactRefreshRegression', () => {
// https://github.com/vercel/next.js/issues/13574 // https://github.com/vercel/next.js/issues/13574
test('custom loader (mdx) should have Fast Refresh enabled', async () => { test('custom loader (mdx) should have Fast Refresh enabled', async () => {
const files = new Map() const { session, cleanup } = await sandbox(
files.set( next,
new Map([
[
'next.config.js', 'next.config.js',
` outdent`
const withMDX = require("@next/mdx")({ const withMDX = require("@next/mdx")({
extension: /\\.mdx?$/, extension: /\\.mdx?$/,
}); });
module.exports = withMDX({ module.exports = withMDX({
pageExtensions: ["js", "mdx"], pageExtensions: ["js", "mdx"],
}); });
` `,
],
['pages/mdx.mdx', `Hello World!`],
]),
'/mdx'
) )
files.set('pages/index.mdx', `Hello World!`)
const { session, cleanup } = await sandbox(next, files, false)
expect( expect(
await session.evaluate( await session.evaluate(
() => document.querySelector('#__next').textContent () => document.querySelector('#__next').textContent
) )
).toBe('Hello World!') ).toBe('Hello World!')
let didNotReload = await session.patch('pages/index.mdx', `Hello Foo!`) let didNotReload = await session.patch('pages/mdx.mdx', `Hello Foo!`)
expect(didNotReload).toBe(true) expect(didNotReload).toBe(true)
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
expect( expect(
...@@ -329,7 +329,7 @@ describe('ReactRefreshRegression', () => { ...@@ -329,7 +329,7 @@ describe('ReactRefreshRegression', () => {
) )
).toBe('Hello Foo!') ).toBe('Hello Foo!')
didNotReload = await session.patch('pages/index.mdx', `Hello Bar!`) didNotReload = await session.patch('pages/mdx.mdx', `Hello Bar!`)
expect(didNotReload).toBe(true) expect(didNotReload).toBe(true)
expect(await session.hasRedbox(false)).toBe(false) expect(await session.hasRedbox(false)).toBe(false)
expect( expect(
......
/* 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'
describe('ReactRefreshRequire', () => { describe('ReactRefreshRequire', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, 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 () => {
......
...@@ -4,22 +4,20 @@ exports[`ReactRefreshLogBox default Module not found (empty import trace) 1`] = ...@@ -4,22 +4,20 @@ exports[`ReactRefreshLogBox default Module not found (empty import trace) 1`] =
"./pages/index.js:1:0 "./pages/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 |
3 | return ( 3 | export default function Oops() {
4 | <div> 4 | return (
https://nextjs.org/docs/messages/module-not-found" https://nextjs.org/docs/messages/module-not-found"
`; `;
exports[`ReactRefreshLogBox default Module not found (missing global CSS) 1`] = ` exports[`ReactRefreshLogBox default Module not found (missing global CSS) 1`] = `
"./pages/_app.js:2:8 "./pages/_app.js:1:0
Module not found: Can't resolve './non-existent.css' Module not found: Can't resolve './non-existent.css'
1 | > 1 | import './non-existent.css'
> 2 | import './non-existent.css' 2 |
| ^ 3 | export default function App({ Component, pageProps }) {
3 | 4 | return <Component {...pageProps} />
4 | export default function App({ Component, pageProps }) {
5 | return <Component {...pageProps} />
https://nextjs.org/docs/messages/module-not-found" https://nextjs.org/docs/messages/module-not-found"
`; `;
...@@ -28,9 +26,9 @@ exports[`ReactRefreshLogBox default Module not found 1`] = ` ...@@ -28,9 +26,9 @@ exports[`ReactRefreshLogBox 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 |
3 | return ( 3 | export default function Oops() {
4 | <div> 4 | return (
https://nextjs.org/docs/messages/module-not-found https://nextjs.org/docs/messages/module-not-found
...@@ -39,7 +37,7 @@ Import trace for requested module: ...@@ -39,7 +37,7 @@ Import trace for requested module:
`; `;
exports[`ReactRefreshLogBox default Node.js builtins 1`] = ` exports[`ReactRefreshLogBox 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
......
...@@ -8,15 +8,14 @@ exports[`ReactRefreshLogBox default boundaries 1`] = ` ...@@ -8,15 +8,14 @@ exports[`ReactRefreshLogBox default boundaries 1`] = `
`; `;
exports[`ReactRefreshLogBox default conversion to class component (1) 1`] = ` exports[`ReactRefreshLogBox 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 default css syntax errors 1`] = ` exports[`ReactRefreshLogBox default css syntax errors 1`] = `
...@@ -48,25 +47,25 @@ exports[`ReactRefreshLogBox default logbox: anchors links in error messages 11`] ...@@ -48,25 +47,25 @@ exports[`ReactRefreshLogBox default logbox: anchors links in error messages 11`]
exports[`ReactRefreshLogBox default logbox: anchors links in error messages 12`] = `"http://example.com/"`; exports[`ReactRefreshLogBox default logbox: anchors links in error messages 12`] = `"http://example.com/"`;
exports[`ReactRefreshLogBox default module init error not shown 1`] = ` exports[`ReactRefreshLogBox 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 default should strip whitespace correctly with newline 1`] = ` exports[`ReactRefreshLogBox 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>"
`; `;
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactRefreshLogBox default syntax > runtime error 1`] = ` exports[`ReactRefreshLogBox default syntax > runtime error 1`] = `
"index.js (6:16) @ Error "index.js (5:8) @ Error
4 | setInterval(() => { 3 | setInterval(() => {
5 | i++ 4 | i++
> 6 | throw Error('no ' + i) > 5 | throw Error('no ' + i)
| ^ | ^
7 | }, 1000) 6 | }, 1000)
8 | export default function FunctionNamed() { 7 | export default function FunctionNamed() {
9 | return <div />" 8 | return <div />"
`; `;
/* 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 { outdent } from 'outdent'
import path from 'path'
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: {},
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 () => { it('should show a component stack on hydration error', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
new Map([ new Map([
[ [
'component.js', 'component.js',
` outdent`
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Component() { export default function Component() {
return ( return (
...@@ -28,11 +29,11 @@ createNextDescribe( ...@@ -28,11 +29,11 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
[ [
'index.js', 'index.js',
` outdent`
import Component from './component' import Component from './component'
export default function Mismatch() { export default function Mismatch() {
return ( return (
...@@ -41,7 +42,7 @@ createNextDescribe( ...@@ -41,7 +42,7 @@ createNextDescribe(
</main> </main>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -58,5 +59,4 @@ createNextDescribe( ...@@ -58,5 +59,4 @@ createNextDescribe(
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 { check, describeVariants as describe } from 'next-test-utils'
import { check, getSnapshotTestDescribe } from 'next-test-utils' import { outdent } from 'outdent'
import path from 'path'
for (const variant of ['default', 'turbo']) {
getSnapshotTestDescribe(variant)(`ReactRefreshLogBox ${variant}`, () => { describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
let next: NextInstance const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
beforeAll(async () => {
next = await createNext({
files: {},
skipStart: true, skipStart: true,
}) })
})
afterAll(() => next.destroy())
test('logbox: can recover from a syntax error without losing state', async () => { test('logbox: can recover from a syntax error 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() {
...@@ -51,7 +46,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -51,7 +46,7 @@ for (const variant of ['default', 'turbo']) {
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() {
...@@ -82,7 +77,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -82,7 +77,7 @@ for (const variant of ['default', 'turbo']) {
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() {
...@@ -111,20 +106,20 @@ for (const variant of ['default', 'turbo']) { ...@@ -111,20 +106,20 @@ for (const variant of ['default', 'turbo']) {
expect(await session.hasRedbox(true)).toBe(true) expect(await session.hasRedbox(true)).toBe(true)
expect(await session.getRedboxSource()).toMatchInlineSnapshot(` expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (8:18) @ eval "index.js (7:10) @ eval
6 | const increment = useCallback(() => { 5 | const increment = useCallback(() => {
7 | setCount(c => c + 1) 6 | setCount(c => c + 1)
> 8 | throw new Error('oops') > 7 | throw new Error('oops')
| ^ | ^
9 | }, [setCount]) 8 | }, [setCount])
10 | return ( 9 | return (
11 | <main>" 10 | <main>"
`) `)
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() {
...@@ -160,7 +155,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -160,7 +155,7 @@ for (const variant of ['default', 'turbo']) {
await session.write( await session.write(
'child.js', 'child.js',
` outdent`
export default function Child() { export default function Child() {
return <p>Hello</p>; return <p>Hello</p>;
} }
...@@ -169,7 +164,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -169,7 +164,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import Child from './child' import Child from './child'
export default function Index() { export default function Index() {
...@@ -188,7 +183,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -188,7 +183,7 @@ for (const variant of ['default', 'turbo']) {
await session.patch( await session.patch(
'child.js', 'child.js',
` outdent`
// hello // hello
export default function Child() { export default function Child() {
throw new Error('oops') throw new Error('oops')
...@@ -203,7 +198,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -203,7 +198,7 @@ for (const variant of ['default', 'turbo']) {
const didNotReload = await session.patch( const didNotReload = await session.patch(
'child.js', 'child.js',
` outdent`
export default function Child() { export default function Child() {
return <p>Hello</p>; return <p>Hello</p>;
} }
...@@ -226,7 +221,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -226,7 +221,7 @@ for (const variant of ['default', 'turbo']) {
// Starting here: // Starting here:
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
render() { render() {
...@@ -245,7 +240,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -245,7 +240,7 @@ for (const variant of ['default', 'turbo']) {
// Break it with a syntax error: // Break it with a syntax error:
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
...@@ -262,7 +257,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -262,7 +257,7 @@ for (const variant of ['default', 'turbo']) {
// Now change the code to introduce a runtime error without fixing the syntax error: // Now change the code to introduce a runtime error without fixing the syntax error:
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
...@@ -280,7 +275,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -280,7 +275,7 @@ for (const variant of ['default', 'turbo']) {
// Now fix the syntax error: // Now fix the syntax error:
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
class ClassDefault extends React.Component { class ClassDefault extends React.Component {
...@@ -314,7 +309,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -314,7 +309,7 @@ for (const variant of ['default', 'turbo']) {
// We start here. // We start here.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
function FunctionDefault() { function FunctionDefault() {
...@@ -328,7 +323,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -328,7 +323,7 @@ for (const variant of ['default', 'turbo']) {
// We add a new file. Let's call it Foo.js. // We add a new file. Let's call it Foo.js.
await session.write( await session.write(
'Foo.js', 'Foo.js',
` outdent`
// intentionally skips export // intentionally skips export
export default function Foo() { export default function Foo() {
return React.createElement('h1', null, 'Foo'); return React.createElement('h1', null, 'Foo');
...@@ -339,7 +334,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -339,7 +334,7 @@ for (const variant of ['default', 'turbo']) {
// We edit our first file to use it. // We edit our first file to use it.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
import Foo from './Foo'; import Foo from './Foo';
function FunctionDefault() { function FunctionDefault() {
...@@ -358,7 +353,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -358,7 +353,7 @@ for (const variant of ['default', 'turbo']) {
// Let's add that to Foo. // Let's add that to Foo.
await session.patch( await session.patch(
'Foo.js', 'Foo.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
export default function Foo() { export default function Foo() {
return React.createElement('h1', null, 'Foo'); return React.createElement('h1', null, 'Foo');
...@@ -379,7 +374,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -379,7 +374,7 @@ for (const variant of ['default', 'turbo']) {
// Start here. // Start here.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
export default function FunctionNamed() { export default function FunctionNamed() {
...@@ -390,7 +385,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -390,7 +385,7 @@ for (const variant of ['default', 'turbo']) {
// TODO: this acts weird without above step // TODO: this acts weird without above step
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
let i = 0 let i = 0
setInterval(() => { setInterval(() => {
...@@ -414,7 +409,7 @@ for (const variant of ['default', 'turbo']) { ...@@ -414,7 +409,7 @@ for (const variant of ['default', 'turbo']) {
// Make a syntax error. // Make a syntax error.
await session.patch( await session.patch(
'index.js', 'index.js',
` outdent`
import * as React from 'react'; import * as React from 'react';
let i = 0 let i = 0
setInterval(() => { setInterval(() => {
...@@ -433,11 +428,11 @@ for (const variant of ['default', 'turbo']) { ...@@ -433,11 +428,11 @@ for (const variant of ['default', 'turbo']) {
"./index.js "./index.js
Error: Error:
x Expected '}', got '<eof>' x Expected '}', got '<eof>'
,-[TEST_DIR/index.js:5:1] ,-[TEST_DIR/index.js:4:1]
5 | i++ 4 | i++
6 | throw Error('no ' + i) 5 | throw Error('no ' + i)
7 | }, 1000) 6 | }, 1000)
8 | export default function FunctionNamed() { 7 | export default function FunctionNamed() {
: ^ : ^
\`---- \`----
...@@ -460,11 +455,11 @@ for (const variant of ['default', 'turbo']) { ...@@ -460,11 +455,11 @@ for (const variant of ['default', 'turbo']) {
"./index.js "./index.js
Error: Error:
x Expected '}', got '<eof>' x Expected '}', got '<eof>'
,-[TEST_DIR/index.js:5:1] ,-[TEST_DIR/index.js:4:1]
5 | i++ 4 | i++
6 | throw Error('no ' + i) 5 | throw Error('no ' + i)
7 | }, 1000) 6 | }, 1000)
8 | export default function FunctionNamed() { 7 | export default function FunctionNamed() {
: ^ : ^
\`---- \`----
...@@ -479,5 +474,4 @@ for (const variant of ['default', 'turbo']) { ...@@ -479,5 +474,4 @@ for (const variant of ['default', 'turbo']) {
await cleanup() await cleanup()
}) })
}) })
}
import {
getRedboxDescription,
getRedboxHeader,
getRedboxSource,
hasRedbox,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { NextInstance } from 'test/lib/next-modes/base'
export async function sandbox(
next: NextInstance,
initialFiles?: Map<string, string>,
defaultFiles = true
) {
await next.stop()
await next.clean()
if (defaultFiles) {
await next.patchFile(
'pages/index.js',
`export { default } from '../index';`
)
await next.patchFile('index.js', `export default () => 'new sandbox';`)
}
if (initialFiles) {
for (const [k, v] of initialFiles.entries()) {
await next.patchFile(k, v)
}
}
await next.start()
const browser = await webdriver(next.url, '/')
return {
session: {
async write(filename, content) {
// Update the file on filesystem
await next.patchFile(filename, content)
},
async patch(filename, content) {
// Register an event for HMR completion
await browser.eval(function () {
;(window as any).__HMR_STATE = 'pending'
var timeout = setTimeout(() => {
;(window as any).__HMR_STATE = 'timeout'
}, 30 * 1000)
;(window as any).__NEXT_HMR_CB = function () {
clearTimeout(timeout)
;(window as any).__HMR_STATE = 'success'
}
})
await this.write(filename, content)
for (;;) {
const status = await browser.eval(() => (window as any).__HMR_STATE)
if (!status) {
await new Promise((resolve) => setTimeout(resolve, 750))
// Wait for application to re-hydrate:
await browser.evalAsync(function () {
var callback = arguments[arguments.length - 1]
if ((window as any).__NEXT_HYDRATED) {
callback()
} else {
var timeout = setTimeout(callback, 30 * 1000)
;(window as any).__NEXT_HYDRATED_CB = function () {
clearTimeout(timeout)
callback()
}
}
})
console.log('Application re-loaded.')
// Slow down tests a bit:
await new Promise((resolve) => setTimeout(resolve, 750))
return false
}
if (status === 'success') {
console.log('Hot update complete.')
break
}
if (status !== 'pending') {
throw new Error(`Application is in inconsistent state: ${status}.`)
}
await new Promise((resolve) => setTimeout(resolve, 30))
}
// Slow down tests a bit (we don't know how long re-rendering takes):
await new Promise((resolve) => setTimeout(resolve, 750))
return true
},
async remove(filename) {
await next.deleteFile(filename)
},
async evaluate(snippet: () => any) {
if (typeof snippet === 'function') {
const result = await browser.eval(snippet)
await new Promise((resolve) => setTimeout(resolve, 30))
return result
} else {
throw new Error(
`You must pass a function to be evaluated in the browser.`
)
}
},
async hasRedbox(expected = false) {
return hasRedbox(browser, expected)
},
async getRedboxDescription() {
return getRedboxDescription(browser)
},
async getRedboxSource(includeHeader = false) {
const header = includeHeader ? await getRedboxHeader(browser) : ''
const source = await getRedboxSource(browser)
if (includeHeader) {
return `${header}\n\n${source}`
}
return source
},
async getRedboxComponentStack() {
await browser.waitForElementByCss('[data-nextjs-component-stack-frame]')
const componentStackFrameElements = await browser.elementsByCss(
'[data-nextjs-component-stack-frame]'
)
const componentStackFrameTexts = await Promise.all(
componentStackFrameElements.map((f) => f.innerText())
)
return componentStackFrameTexts.join('\n')
},
},
async cleanup() {
await browser.close()
await next.stop()
await next.clean()
},
}
}
/* 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 { outdent } from 'outdent'
import path from 'path'
createNextDescribe( describe('Error overlay for hydration errors', () => {
'Error overlay for hydration errors', const { next } = nextTestSetup({
{ files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
files: {},
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
it('should show correct hydration error when client and server render different text', async () => { it('should show correct hydration error when client and server render different text', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
new Map([ new Map([
[ [
'index.js', 'index.js',
` outdent`
const isClient = typeof window !== 'undefined' const isClient = typeof window !== 'undefined'
export default function Mismatch() { export default function Mismatch() {
return ( return (
...@@ -28,7 +29,7 @@ createNextDescribe( ...@@ -28,7 +29,7 @@ createNextDescribe(
</div> </div>
); );
} }
`, `,
], ],
]) ])
) )
...@@ -45,5 +46,4 @@ createNextDescribe( ...@@ -45,5 +46,4 @@ createNextDescribe(
await cleanup() await cleanup()
}) })
} })
)
/* eslint-env jest */ /* eslint-env jest */
import { createNextDescribe } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { sandbox } from './helpers' import { sandbox } from 'development-sandbox'
import { outdent } from 'outdent'
const initialFiles = new Map([ const initialFiles = new Map([
['next.config.js', 'module.exports = { experimental: { appDir: true } }'], ['next.config.js', 'module.exports = { experimental: { appDir: true } }'],
['app/_.js', ''], // app dir need to exists, otherwise the SWC RSC checks will not run ['app/_.js', ''], // app dir need to exists, otherwise the SWC RSC checks will not run
[ [
'pages/index.js', 'pages/index.js',
`import Comp from '../components/Comp' outdent`
import Comp from '../components/Comp'
export default function Page() { return <Comp /> }`, export default function Page() { return <Comp /> }
`,
], ],
[ [
'components/Comp.js', 'components/Comp.js',
`export default function Comp() { return <p>Hello world</p> }`, outdent`
export default function Comp() {
return <p>Hello world</p>
}
`,
], ],
]) ])
createNextDescribe( describe('Error Overlay for server components compiler errors in pages', () => {
'Error Overlay for server components compiler errors in pages', const { next } = nextTestSetup({
{
files: {}, files: {},
dependencies: { dependencies: {
react: 'latest', react: 'latest',
'react-dom': 'latest', 'react-dom': 'latest',
}, },
skipStart: true, skipStart: true,
}, })
({ next }) => {
test("importing 'next/headers' in pages", async () => { test("importing 'next/headers' in pages", async () => {
const { session, cleanup } = await sandbox(next, initialFiles, false) const { session, cleanup } = await sandbox(next, initialFiles)
await session.patch( await session.patch(
'components/Comp.js', 'components/Comp.js',
` outdent`
import { cookies } from 'next/headers' import { cookies } from 'next/headers'
export default function Page() { export default function Page() {
...@@ -58,12 +64,11 @@ createNextDescribe( ...@@ -58,12 +64,11 @@ createNextDescribe(
You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components
,-[TEST_DIR/components/Comp.js:1:1] ,-[TEST_DIR/components/Comp.js:1:1]
1 | 1 | import { cookies } from 'next/headers'
2 | import { cookies } from 'next/headers'
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 | 2 |
4 | export default function Page() { 3 | export default function Page() {
5 | return <p>hello world</p> 4 | return <p>hello world</p>
\`---- \`----
Import trace for requested module: Import trace for requested module:
...@@ -76,11 +81,11 @@ createNextDescribe( ...@@ -76,11 +81,11 @@ createNextDescribe(
}) })
test("importing 'server-only' in pages", async () => { test("importing 'server-only' in pages", async () => {
const { session, cleanup } = await sandbox(next, initialFiles, false) const { session, cleanup } = await sandbox(next, initialFiles)
await next.patchFile( await next.patchFile(
'components/Comp.js', 'components/Comp.js',
` outdent`
import 'server-only' import 'server-only'
export default function Page() { export default function Page() {
...@@ -104,12 +109,11 @@ createNextDescribe( ...@@ -104,12 +109,11 @@ createNextDescribe(
You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components You're importing a component that needs server-only. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/react-essentials#server-components
,-[TEST_DIR/components/Comp.js:1:1] ,-[TEST_DIR/components/Comp.js:1:1]
1 | 1 | import 'server-only'
2 | import 'server-only'
: ^^^^^^^^^^^^^^^^^^^^ : ^^^^^^^^^^^^^^^^^^^^
3 | 2 |
4 | export default function Page() { 3 | export default function Page() {
5 | return 'hello world' 4 | return 'hello world'
\`---- \`----
Import trace for requested module: Import trace for requested module:
...@@ -120,5 +124,4 @@ createNextDescribe( ...@@ -120,5 +124,4 @@ createNextDescribe(
await cleanup() await cleanup()
}) })
} })
)
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 '../acceptance/helpers' import path from 'path'
import { outdent } from 'outdent'
const middlewarePath = 'middleware.js' const middlewarePath = 'middleware.js'
const middlewareWarning = `A middleware can not alter response's body` const middlewareWarning = `A middleware can not alter response's body`
describe('middlewares', () => { describe('middlewares', () => {
let next: NextInstance const { next } = nextTestSetup({
let cleanup files: new FileRef(
path.join(__dirname, '..', 'acceptance', 'fixtures', 'default-template')
beforeAll(async () => { ),
next = await createNext({
files: {},
skipStart: true, skipStart: true,
}) })
})
afterAll(() => next.destroy())
let cleanup
afterEach(() => cleanup?.()) afterEach(() => cleanup?.())
it.each([ it.each([
{ {
title: 'returning response with literal string', title: 'returning response with literal string',
code: `export default function middleware() { code: outdent`
export default function middleware() {
return new Response('this is not allowed'); return new Response('this is not allowed');
}`, }
`,
}, },
{ {
title: 'returning response with literal number', title: 'returning response with literal number',
code: `export default function middleware() { code: outdent`
export default function middleware() {
return new Response(10); return new Response(10);
}`, }
`,
}, },
{ {
title: 'returning response with JSON.stringify', title: 'returning response with JSON.stringify',
code: `export default function middleware() { code: outdent`
export default function middleware() {
return new Response(JSON.stringify({ foo: 'this is not allowed' })); return new Response(JSON.stringify({ foo: 'this is not allowed' }));
}`, }
`,
}, },
{ {
title: 'populating response with a value', title: 'populating response with a value',
code: `export default function middleware(request) { code: outdent`
export default function middleware(request) {
const body = JSON.stringify({ foo: 'this should not be allowed, but hard to detect with AST' }) const body = JSON.stringify({ foo: 'this should not be allowed, but hard to detect with AST' })
return new Response(body); return new Response(body);
}`, }
`,
}, },
{ {
title: 'populating response with a function call', title: 'populating response with a function call',
code: `function buildBody() { code: outdent`
function buildBody() {
return 'this should not be allowed, but hard to detect with AST' return 'this should not be allowed, but hard to detect with AST'
} }
export default function middleware(request) { export default function middleware(request) {
return new Response(buildBody()); return new Response(buildBody());
}`, }
`,
}, },
{ {
title: 'populating response with an async function call', title: 'populating response with an async function call',
code: `export default async function middleware(request) { code: outdent`
export default async function middleware(request) {
return new Response(await fetch('https://example.vercel.sh')); return new Response(await fetch('https://example.vercel.sh'));
}`, }
`,
}, },
])('does not warn when $title', async ({ code }) => { ])('does not warn when $title', async ({ code }) => {
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]]))) ;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
...@@ -69,15 +78,19 @@ describe('middlewares', () => { ...@@ -69,15 +78,19 @@ describe('middlewares', () => {
it.each([ it.each([
{ {
title: 'returning null reponse body', title: 'returning null reponse body',
code: `export default function middleware() { code: outdent`
export default function middleware() {
return new Response(null); return new Response(null);
}`, }
`,
}, },
{ {
title: 'returning undefined response body', title: 'returning undefined response body',
code: `export default function middleware() { code: outdent`
export default function middleware() {
return new Response(undefined); return new Response(undefined);
}`, }
`,
}, },
])('does not warn when $title', async ({ code }) => { ])('does not warn when $title', async ({ code }) => {
;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]]))) ;({ cleanup } = await sandbox(next, new Map([[middlewarePath, code]])))
......
...@@ -3,9 +3,26 @@ import { ...@@ -3,9 +3,26 @@ import {
getRedboxHeader, getRedboxHeader,
getRedboxSource, getRedboxSource,
hasRedbox, hasRedbox,
} from 'next-test-utils' waitFor,
import webdriver from 'next-webdriver' } from './next-test-utils'
import { NextInstance } from 'test/lib/next-modes/base' import webdriver from './next-webdriver'
import { NextInstance } from './next-modes/base'
import { BrowserInterface } from './browsers/base'
export function waitForHydration(browser: BrowserInterface): Promise<void> {
return browser.evalAsync(function () {
var callback = arguments[arguments.length - 1]
if ((window as any).__NEXT_HYDRATED) {
callback()
} else {
var timeout = setTimeout(callback, 30 * 1000)
;(window as any).__NEXT_HYDRATED_CB = function () {
clearTimeout(timeout)
callback()
}
}
})
}
export async function sandbox( export async function sandbox(
next: NextInstance, next: NextInstance,
...@@ -21,8 +38,10 @@ export async function sandbox( ...@@ -21,8 +38,10 @@ export async function sandbox(
await next.patchFile(k, v) await next.patchFile(k, v)
} }
} }
await next.start() await next.start()
const browser = await webdriver(next.url, initialUrl, webDriverOptions) const browser = await webdriver(next.url, initialUrl, webDriverOptions)
// await waitForHydration(browser)
return { return {
browser, browser,
session: { session: {
...@@ -49,25 +68,14 @@ export async function sandbox( ...@@ -49,25 +68,14 @@ export async function sandbox(
for (;;) { for (;;) {
const status = await browser.eval(() => (window as any).__HMR_STATE) const status = await browser.eval(() => (window as any).__HMR_STATE)
if (!status) { if (!status) {
await new Promise((resolve) => setTimeout(resolve, 750)) await waitFor(750)
// Wait for application to re-hydrate: // Wait for application to re-hydrate:
await browser.evalAsync(function () { await waitForHydration(browser)
var callback = arguments[arguments.length - 1]
if ((window as any).__NEXT_HYDRATED) {
callback()
} else {
var timeout = setTimeout(callback, 30 * 1000)
;(window as any).__NEXT_HYDRATED_CB = function () {
clearTimeout(timeout)
callback()
}
}
})
console.log('Application re-loaded.') console.log('Application re-loaded.')
// Slow down tests a bit: // Slow down tests a bit:
await new Promise((resolve) => setTimeout(resolve, 750)) await waitFor(750)
return false return false
} }
if (status === 'success') { if (status === 'success') {
...@@ -78,11 +86,11 @@ export async function sandbox( ...@@ -78,11 +86,11 @@ export async function sandbox(
throw new Error(`Application is in inconsistent state: ${status}.`) throw new Error(`Application is in inconsistent state: ${status}.`)
} }
await new Promise((resolve) => setTimeout(resolve, 30)) await waitFor(30)
} }
// Slow down tests a bit (we don't know how long re-rendering takes): // Slow down tests a bit (we don't know how long re-rendering takes):
await new Promise((resolve) => setTimeout(resolve, 750)) await waitFor(750)
return true return true
}, },
async remove(filename) { async remove(filename) {
...@@ -91,7 +99,7 @@ export async function sandbox( ...@@ -91,7 +99,7 @@ export async function sandbox(
async evaluate(snippet: () => any) { async evaluate(snippet: () => any) {
if (typeof snippet === 'function') { if (typeof snippet === 'function') {
const result = await browser.eval(snippet) const result = await browser.eval(snippet)
await new Promise((resolve) => setTimeout(resolve, 30)) await waitFor(30)
return result return result
} else { } else {
throw new Error( throw new Error(
...@@ -124,7 +132,10 @@ export async function sandbox( ...@@ -124,7 +132,10 @@ export async function sandbox(
return source return source
}, },
async getRedboxComponentStack() { async getRedboxComponentStack() {
await browser.waitForElementByCss('[data-nextjs-component-stack-frame]') await browser.waitForElementByCss(
'[data-nextjs-component-stack-frame]',
30000
)
const componentStackFrameElements = await browser.elementsByCss( const componentStackFrameElements = await browser.elementsByCss(
'[data-nextjs-component-stack-frame]' '[data-nextjs-component-stack-frame]'
) )
......
...@@ -202,37 +202,40 @@ export async function createNext( ...@@ -202,37 +202,40 @@ export async function createNext(
} }
} }
export function createNextDescribe( export function nextTestSetup(
name: string,
options: Parameters<typeof createNext>[0] & { options: Parameters<typeof createNext>[0] & {
skipDeployment?: boolean skipDeployment?: boolean
dir?: string dir?: string
}, }
fn: (context: { ): {
isNextDev: boolean isNextDev: boolean
isNextDeploy: boolean isNextDeploy: boolean
isNextStart: boolean isNextStart: boolean
isTurbopack: boolean isTurbopack: boolean
next: NextInstance next: NextInstance
}) => void skipped: boolean
): void { } {
describe(name, () => { let skipped = false
if (options.skipDeployment) { if (options.skipDeployment) {
// When the environment is running for deployment tests. // When the environment is running for deployment tests.
if ((global as any).isNextDeploy) { if ((global as any).isNextDeploy) {
it('should skip next deploy', () => {}) // eslint-disable-next-line jest/no-focused-tests
it.only('should skip next deploy', () => {})
// No tests are run. // No tests are run.
return skipped = true
} }
} }
let next: NextInstance let next: NextInstance
if (!skipped) {
beforeAll(async () => { beforeAll(async () => {
next = await createNext(options) next = await createNext(options)
}) })
afterAll(async () => { afterAll(async () => {
await next.destroy() await next.destroy()
}) })
}
const nextProxy = new Proxy<NextInstance>({} as NextInstance, { const nextProxy = new Proxy<NextInstance>({} as NextInstance, {
get: function (_target, property) { get: function (_target, property) {
...@@ -240,7 +243,8 @@ export function createNextDescribe( ...@@ -240,7 +243,8 @@ export function createNextDescribe(
return typeof prop === 'function' ? prop.bind(next) : prop return typeof prop === 'function' ? prop.bind(next) : prop
}, },
}) })
fn({
return {
get isNextDev(): boolean { get isNextDev(): boolean {
return Boolean((global as any).isNextDev) return Boolean((global as any).isNextDev)
}, },
...@@ -261,6 +265,31 @@ export function createNextDescribe( ...@@ -261,6 +265,31 @@ export function createNextDescribe(
get next() { get next() {
return nextProxy return nextProxy
}, },
}) skipped,
}
}
export function createNextDescribe(
name: string,
options: Parameters<typeof createNext>[0] & {
skipDeployment?: boolean
dir?: string
},
fn: (context: {
isNextDev: boolean
isNextDeploy: boolean
isNextStart: boolean
isTurbopack: boolean
next: NextInstance
}) => void
): void {
describe(name, () => {
const context = nextTestSetup(options)
if (context.skipped) {
return
}
fn(context)
}) })
} }
...@@ -937,7 +937,9 @@ export function shouldRunTurboDevTest() { ...@@ -937,7 +937,9 @@ export function shouldRunTurboDevTest() {
export function getSnapshotTestDescribe(variant) { export function getSnapshotTestDescribe(variant) {
const runningEnv = variant ?? 'default' const runningEnv = variant ?? 'default'
if (runningEnv !== 'default' && runningEnv !== 'turbo') { if (runningEnv !== 'default' && runningEnv !== 'turbo') {
throw new Error(`Check if test env passed correctly ${variant}`) throw new Error(
`An invalid test env was passed: ${variant} (only "default" and "turbo" are valid options)`
)
} }
const shouldRunTurboDev = shouldRunTurboDevTest() const shouldRunTurboDev = shouldRunTurboDevTest()
...@@ -947,3 +949,25 @@ export function getSnapshotTestDescribe(variant) { ...@@ -947,3 +949,25 @@ export function getSnapshotTestDescribe(variant) {
return shouldSkip ? describe.skip : describe return shouldSkip ? describe.skip : describe
} }
/**
* For better editor support, pass in the variants this should run on (`default` and/or `turbo`) as cases.
*
* This is necessary if separate snapshots are needed for next.js with webpack vs turbopack.
*
* @type {Pick<import("jest").Describe, "each">}
*/
export const describeVariants = {
each: (variants) => (name, fn) => {
if (
!Array.isArray(variants) ||
!variants.every((val) => typeof val === 'string')
) {
throw new Error('variants need to be an array of strings')
}
for (const variant of variants) {
getSnapshotTestDescribe(variant).each([variant])(name, fn)
}
},
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
"baseUrl": ".", "baseUrl": ".",
"types": ["react", "jest", "node", "trusted-types"], "types": ["react", "jest", "node", "trusted-types"],
"paths": { "paths": {
"development-sandbox": ["test/lib/development-sandbox"],
"next-test-utils": ["test/lib/next-test-utils"], "next-test-utils": ["test/lib/next-test-utils"],
"amp-test-utils": ["test/lib/amp-test-utils"], "amp-test-utils": ["test/lib/amp-test-utils"],
"next-webdriver": ["test/lib/next-webdriver"], "next-webdriver": ["test/lib/next-webdriver"],
......
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