Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve top level await coverage #64508

Merged
merged 8 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ test/development/basic/hmr/components/parse-error.js
packages/next-swc/docs/assets/**/*
test/lib/amp-validator-wasm.js
test/production/pages-dir/production/fixture/amp-validator-wasm.js
test/e2e/async-modules/amp-validator-wasm.js
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ bench/nested-deps/components/**/*
**/.tina/__generated__/**
test/lib/amp-validator-wasm.js
test/production/pages-dir/production/fixture/amp-validator-wasm.js
test/e2e/async-modules/amp-validator-wasm.js
2 changes: 1 addition & 1 deletion packages/next/src/build/webpack/config/blocks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const base = curry(function base(
? 'node18.17' // Same version defined in packages/next/package.json#engines
: ctx.isEdgeRuntime
? ['web', 'es6']
: ['web', 'es5']
: ['web', 'es6']

// https://webpack.js.org/configuration/devtool/#development
if (ctx.isDevelopment) {
Expand Down
14 changes: 12 additions & 2 deletions packages/next/src/lib/typescript/writeConfigurationDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as Log from '../../build/output/log'

type DesiredCompilerOptionsShape = {
[K in keyof CompilerOptions]:
| { suggested: any }
| { suggested: any; reason?: string }
| {
parsedValue?: any
parsedValues?: Array<any>
Expand All @@ -23,6 +23,11 @@ function getDesiredCompilerOptions(
tsOptions?: CompilerOptions
): DesiredCompilerOptionsShape {
const o: DesiredCompilerOptionsShape = {
target: {
suggested: 'ES2017',
reason:
'For top-level `await`. Note: Next.js only polyfills for the esmodules target.',
},
// These are suggested values and will be set when not present in the
// tsconfig.json
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
Expand Down Expand Up @@ -168,7 +173,12 @@ export async function writeConfigurationDefaults(
}
userTsConfig.compilerOptions[optionKey] = check.suggested
suggestedActions.push(
cyan(optionKey) + ' was set to ' + bold(check.suggested)
cyan(optionKey) +
' was set to ' +
bold(check.suggested) +
check.reason
? ` (${check.reason})`
: ''
)
}
} else if ('value' in check) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use client'
const appValue = await Promise.resolve('hello')

export default function Page() {
return <p id="app-router-client-component-value">{appValue}</p>
}
5 changes: 5 additions & 0 deletions test/e2e/async-modules-app/app/app-router/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const appValue = await Promise.resolve('hello')

export default function Page() {
return <p id="app-router-value">{appValue}</p>
}
16 changes: 16 additions & 0 deletions test/e2e/async-modules-app/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
21 changes: 21 additions & 0 deletions test/e2e/async-modules-app/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'

describe('Async modules', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('app router server component async module', async () => {
const browser = await next.browser('/app-router')
expect(await browser.elementByCss('#app-router-value').text()).toBe('hello')
})

// TODO: Investigate/fix issue with React loading async modules failing.
// Rename app/app-router/client-component/skipped-page.tsx to app/app-router/client-component/page.tsx to run this test.
it.skip('app router client component async module', async () => {
const browser = await next.browser('/app-router/client')
expect(
await browser.elementByCss('#app-router-client-component-value').text()
).toBe('hello')
})
})
1 change: 1 addition & 0 deletions test/e2e/async-modules-app/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we drop this file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could but most tests have one so it shouldn't matter much 👍

2,254 changes: 2,254 additions & 0 deletions test/e2e/async-modules/amp-validator-wasm.js

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions test/e2e/async-modules/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env jest */
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'

describe('Async modules', () => {
const { next, isNextDev: dev } = nextTestSetup({
files: __dirname,
})

it('ssr async page modules', async () => {
const $ = await next.render$('/')
expect($('#app-value').text()).toBe('hello')
expect($('#page-value').text()).toBe('42')
})

it('csr async page modules', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('#app-value').text()).toBe('hello')
expect(await browser.elementByCss('#page-value').text()).toBe('42')
expect(await browser.elementByCss('#doc-value').text()).toBe('doc value')
})

it('works on async api routes', async () => {
const res = await next.fetch('/api/hello')
expect(res.status).toBe(200)
const result = await res.json()
expect(result).toHaveProperty('value', 42)
})

it('works with getServerSideProps', async () => {
const browser = await next.browser('/gssp')
expect(await browser.elementByCss('#gssp-value').text()).toBe('42')
})

it('works with getStaticProps', async () => {
const browser = await next.browser('/gsp')
expect(await browser.elementByCss('#gsp-value').text()).toBe('42')
})

it('can render async 404 pages', async () => {
const browser = await next.browser('/dhiuhefoiahjeoij')
expect(await browser.elementByCss('#content-404').text()).toBe("hi y'all")
})

// TODO: investigate this test flaking
it.skip('can render async AMP pages', async () => {
const browser = await next.browser('/config')
await check(
() => browser.elementByCss('#amp-timeago').text(),
'just now',
true
)
})
;(dev ? it.skip : it)('can render async error page', async () => {
const browser = await next.browser('/make-error')
expect(await browser.elementByCss('#content-error').text()).toBe(
'hello error'
)
})
})
7 changes: 7 additions & 0 deletions test/e2e/async-modules/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
experimental: {
amp: {
validator: require.resolve('./amp-validator-wasm.js'),
},
},
}
1 change: 1 addition & 0 deletions test/e2e/tsconfig-module-preserve/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('tsconfig module: preserve', () => {
"{
"compilerOptions": {
"module": "preserve",
"target": "ES2017",
"lib": [
"dom",
"dom.iterable",
Expand Down
12 changes: 0 additions & 12 deletions test/integration/async-modules/next.config.js

This file was deleted.

136 changes: 0 additions & 136 deletions test/integration/async-modules/test/index.test.js

This file was deleted.