Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ecmascript features tests (#65613)
Adding some tests based on this Twitter thread: https://twitter.com/NicoloRibaudo/status/1788919867002785984 Here's what I found: - Added a new test suite testing for: - Private fields - Turbopack: Works - Webpack: Works on latest - Earlier versions of Next.js with Webpack you'd see an error failing to parse coming from `acorn`, which webpack uses internally. - Verified it's not failing on `next@latest` in [this sandbox](https://codesandbox.io/p/devbox/brave-mcnulty-gszh3q?file=%2Fapp%2Fpage.tsx%3A59%2C32). - `import` `with { type: 'json' }` - Turbopack: Works - Webpack: Works - `export as` I.e. `export { x as abc }` - Turbopack: Works - Webpack: Works - RegExp with `/v` - Node.js:⚠️ Not enabled in the test because Node.js does not support it currently in 18.x which we run tests against. - Turbopack: Works - Webpack: Fails in Webpack currently on `acorn` parsing the JavaScript ## Results In case you want to play with the test here's a codesandbox with the same code that is in the tests: - `next@latest`: https://codesandbox.io/p/devbox/brave-mcnulty-gszh3q - Url: https://gszh3q-3000.csb.app/ Dev: Webpack ``` PASS test/e2e/app-dir/ecmascript-features/ecmascript-features.test.ts ecmascript-features ✓ should work using cheerio ✓ should work using browser ``` Build: Webpack ``` PASS test/e2e/app-dir/ecmascript-features/ecmascript-features.test.ts ecmascript-features ✓ should work using cheerio ✓ should work using browser ``` Dev: Turbopack ``` PASS test/e2e/app-dir/ecmascript-features/ecmascript-features.test.ts ecmascript-features ✓ should work using cheerio ✓ should work using browser ``` Build: Turbopack ``` PASS test/e2e/app-dir/ecmascript-features/ecmascript-features.test.ts ecmascript-features ✓ should work using cheerio ✓ should work using browser ``` <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
- Loading branch information
1 parent
96a96d2
commit b0ab0fe
Showing
16 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/ecmascript-features/turbopack/app/export-as-string/export-as-string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// `.js` file as TypeScript does not support re-exporting with a string literal. | ||
// Details: https://github.com/microsoft/TypeScript/issues/40594 | ||
let x = 1 | ||
|
||
export { x as 'abc' } |
21 changes: 21 additions & 0 deletions
21
test/e2e/app-dir/ecmascript-features/turbopack/app/export-as-string/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client' | ||
// Test both server and client compilation of ECMAScript features. | ||
|
||
import { 'abc' as x } from './export-as-string' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<h1>Ecmascript features test</h1> | ||
<pre id="values-to-check"> | ||
{JSON.stringify( | ||
{ | ||
exportAsString: x, | ||
}, | ||
null, | ||
2 | ||
)} | ||
</pre> | ||
</> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/ecmascript-features/turbopack/app/export-as.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let x = 1 | ||
|
||
export { x as abc } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"message": "Hello World" | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/ecmascript-features/turbopack/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
71 changes: 71 additions & 0 deletions
71
test/e2e/app-dir/ecmascript-features/turbopack/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use client' | ||
// Test both server and client compilation of ECMAScript features. | ||
import { abc } from './export-as' | ||
import json from './file.json' with { type: 'json' } | ||
|
||
class ClassWithPrivate { | ||
#privateField | ||
#privateFieldWithInitializer = 11 | ||
|
||
#privateMethod() { | ||
this.#privateField = 10 | ||
} | ||
|
||
static #privateStaticFieldWithInitializer = 12 | ||
|
||
static #privateStaticMethod() { | ||
return this.#privateStaticFieldWithInitializer | ||
} | ||
|
||
constructor() { | ||
this.#privateMethod() | ||
} | ||
|
||
getPrivateField() { | ||
return this.#privateField | ||
} | ||
getPrivateFieldWithInitializer() { | ||
return this.#privateFieldWithInitializer | ||
} | ||
getPrivateStaticFieldWithInitializer() { | ||
return ClassWithPrivate.#privateStaticFieldWithInitializer | ||
} | ||
getPrivateStaticMethod() { | ||
return ClassWithPrivate.#privateStaticMethod() | ||
} | ||
isPrivateMethodAvailable() { | ||
return #privateField in this | ||
} | ||
} | ||
|
||
// Not supported in Node.js yet. | ||
// let regex = /abc/v | ||
|
||
export default function Page() { | ||
const instance = new ClassWithPrivate() | ||
|
||
return ( | ||
<> | ||
<h1>Ecmascript features test</h1> | ||
<pre id="values-to-check"> | ||
{JSON.stringify( | ||
{ | ||
privateField: instance.getPrivateField(), | ||
privateFieldWithInitializer: | ||
instance.getPrivateFieldWithInitializer(), | ||
privateStaticFieldWithInitializer: | ||
instance.getPrivateStaticFieldWithInitializer(), | ||
privateStaticMethod: instance.getPrivateStaticMethod(), | ||
privateMethodInThis: instance.isPrivateMethodAvailable(), | ||
exportAs: abc, | ||
importWith: json.message, | ||
// Not supported in Node.js yet. | ||
// regex: regex instanceof RegExp | ||
}, | ||
null, | ||
2 | ||
)} | ||
</pre> | ||
</> | ||
) | ||
} |
53 changes: 53 additions & 0 deletions
53
test/e2e/app-dir/ecmascript-features/turbopack/ecmascript-features-turbopack.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
;(process.env.TURBOPACK ? describe : describe.skip)( | ||
'ecmascript-features turbopack', | ||
() => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API. | ||
it('should work using cheerio', async () => { | ||
const $ = await next.render$('/') | ||
expect(JSON.parse($('#values-to-check').text())).toEqual({ | ||
privateField: 10, | ||
privateFieldWithInitializer: 11, | ||
privateStaticFieldWithInitializer: 12, | ||
privateStaticMethod: 12, | ||
privateMethodInThis: true, | ||
exportAs: 1, | ||
// regex: true, | ||
importWith: 'Hello World', | ||
}) | ||
|
||
const $1 = await next.render$('/export-as-string') | ||
expect(JSON.parse($1('#values-to-check').text())).toEqual({ | ||
exportAsString: 1, | ||
}) | ||
}) | ||
|
||
// Recommended for tests that need a full browser | ||
it('should work using browser', async () => { | ||
const browser = await next.browser('/') | ||
expect( | ||
JSON.parse(await browser.elementByCss('#values-to-check').text()) | ||
).toEqual({ | ||
privateField: 10, | ||
privateFieldWithInitializer: 11, | ||
privateStaticFieldWithInitializer: 12, | ||
privateStaticMethod: 12, | ||
privateMethodInThis: true, | ||
exportAs: 1, | ||
// regex: true, | ||
importWith: 'Hello World', | ||
}) | ||
|
||
const browser2 = await next.browser('/export-as-string') | ||
expect( | ||
JSON.parse(await browser2.elementByCss('#values-to-check').text()) | ||
).toEqual({ | ||
exportAsString: 1, | ||
}) | ||
}) | ||
} | ||
) |
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/ecmascript-features/turbopack/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |
21 changes: 21 additions & 0 deletions
21
test/e2e/app-dir/ecmascript-features/webpack/app/export-as-string/_todo-page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client' | ||
// Test both server and client compilation of ECMAScript features. | ||
|
||
import { 'abc' as x } from './export-as-string' | ||
|
||
export default function Page() { | ||
return ( | ||
<> | ||
<h1>Ecmascript features test</h1> | ||
<pre id="values-to-check"> | ||
{JSON.stringify( | ||
{ | ||
exportAsString: x, | ||
}, | ||
null, | ||
2 | ||
)} | ||
</pre> | ||
</> | ||
) | ||
} |
6 changes: 6 additions & 0 deletions
6
test/e2e/app-dir/ecmascript-features/webpack/app/export-as-string/export-as-string.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// `.js` file as TypeScript does not support re-exporting with a string literal. | ||
// Details: https://github.com/microsoft/TypeScript/issues/40594 | ||
|
||
let x = 1 | ||
|
||
export { x as 'abc' } |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/ecmascript-features/webpack/app/export-as.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
let x = 1 | ||
|
||
export { x as abc } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"message": "Hello World" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use client' | ||
// Test both server and client compilation of ECMAScript features. | ||
import { abc } from './export-as' | ||
import json from './file.json' with { type: 'json' } | ||
|
||
class ClassWithPrivate { | ||
#privateField | ||
#privateFieldWithInitializer = 11 | ||
|
||
#privateMethod() { | ||
this.#privateField = 10 | ||
} | ||
|
||
static #privateStaticFieldWithInitializer = 12 | ||
|
||
static #privateStaticMethod() { | ||
return this.#privateStaticFieldWithInitializer | ||
} | ||
|
||
constructor() { | ||
this.#privateMethod() | ||
} | ||
|
||
getPrivateField() { | ||
return this.#privateField | ||
} | ||
getPrivateFieldWithInitializer() { | ||
return this.#privateFieldWithInitializer | ||
} | ||
getPrivateStaticFieldWithInitializer() { | ||
return ClassWithPrivate.#privateStaticFieldWithInitializer | ||
} | ||
getPrivateStaticMethod() { | ||
return ClassWithPrivate.#privateStaticMethod() | ||
} | ||
// TODO: Not supported in webpack yet. | ||
// isPrivateMethodAvailable() { | ||
// return #privateField in this | ||
// } | ||
} | ||
|
||
// Not supported in Node.js yet. | ||
// let regex = /abc/v | ||
|
||
export default function Page() { | ||
const instance = new ClassWithPrivate() | ||
|
||
return ( | ||
<> | ||
<h1>Ecmascript features test</h1> | ||
<pre id="values-to-check"> | ||
{JSON.stringify( | ||
{ | ||
privateField: instance.getPrivateField(), | ||
privateFieldWithInitializer: | ||
instance.getPrivateFieldWithInitializer(), | ||
privateStaticFieldWithInitializer: | ||
instance.getPrivateStaticFieldWithInitializer(), | ||
privateStaticMethod: instance.getPrivateStaticMethod(), | ||
// TODO: Not supported in webpack yet. | ||
// privateMethodInThis: instance.isPrivateMethodAvailable(), | ||
exportAs: abc, | ||
importWith: json.message, | ||
// Not supported in Node.js yet. | ||
// regex: regex instanceof RegExp | ||
}, | ||
null, | ||
2 | ||
)} | ||
</pre> | ||
</> | ||
) | ||
} |
55 changes: 55 additions & 0 deletions
55
test/e2e/app-dir/ecmascript-features/webpack/ecmascript-features-webpack.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
;(process.env.TURBOPACK ? describe.skip : describe)( | ||
'ecmascript-features webpack', | ||
() => { | ||
const { next } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API. | ||
it('should work using cheerio', async () => { | ||
const $ = await next.render$('/') | ||
expect(JSON.parse($('#values-to-check').text())).toEqual({ | ||
privateField: 10, | ||
privateFieldWithInitializer: 11, | ||
privateStaticFieldWithInitializer: 12, | ||
privateStaticMethod: 12, | ||
// privateMethodInThis: true, | ||
exportAs: 1, | ||
// regex: true, | ||
importWith: 'Hello World', | ||
}) | ||
|
||
// TODO: `export { x as "abc" }` (export as string) is not supported in webpack yet. | ||
// const $1 = await next.render$('/export-as-string') | ||
// expect(JSON.parse($1('#values-to-check').text())).toEqual({ | ||
// exportAsString: 1, | ||
// }) | ||
}) | ||
|
||
// Recommended for tests that need a full browser | ||
it('should work using browser', async () => { | ||
const browser = await next.browser('/') | ||
expect( | ||
JSON.parse(await browser.elementByCss('#values-to-check').text()) | ||
).toEqual({ | ||
privateField: 10, | ||
privateFieldWithInitializer: 11, | ||
privateStaticFieldWithInitializer: 12, | ||
privateStaticMethod: 12, | ||
// privateMethodInThis: true, | ||
exportAs: 1, | ||
// regex: true, | ||
importWith: 'Hello World', | ||
}) | ||
|
||
// TODO: `export { x as "abc" }` (export as string) is not supported in webpack yet. | ||
// const browser2 = await next.browser('/export-as-string') | ||
// expect( | ||
// JSON.parse(await browser2.elementByCss('#values-to-check').text()) | ||
// ).toEqual({ | ||
// exportAsString: 1, | ||
// }) | ||
}) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |