Skip to content

Commit

Permalink
Add Ecmascript features tests (#65613)
Browse files Browse the repository at this point in the history
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
timneutkens committed May 11, 2024
1 parent 96a96d2 commit b0ab0fe
Show file tree
Hide file tree
Showing 16 changed files with 343 additions and 0 deletions.
@@ -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' }
@@ -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>
</>
)
}
@@ -0,0 +1,3 @@
let x = 1

export { x as abc }
3 changes: 3 additions & 0 deletions test/e2e/app-dir/ecmascript-features/turbopack/app/file.json
@@ -0,0 +1,3 @@
{
"message": "Hello World"
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/ecmascript-features/turbopack/app/layout.tsx
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
71 changes: 71 additions & 0 deletions test/e2e/app-dir/ecmascript-features/turbopack/app/page.tsx
@@ -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>
</>
)
}
@@ -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 test/e2e/app-dir/ecmascript-features/turbopack/next.config.js
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
@@ -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>
</>
)
}
@@ -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' }
@@ -0,0 +1,3 @@
let x = 1

export { x as abc }
3 changes: 3 additions & 0 deletions test/e2e/app-dir/ecmascript-features/webpack/app/file.json
@@ -0,0 +1,3 @@
{
"message": "Hello World"
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/ecmascript-features/webpack/app/layout.tsx
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
73 changes: 73 additions & 0 deletions test/e2e/app-dir/ecmascript-features/webpack/app/page.tsx
@@ -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>
</>
)
}
@@ -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,
// })
})
}
)
6 changes: 6 additions & 0 deletions test/e2e/app-dir/ecmascript-features/webpack/next.config.js
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig

0 comments on commit b0ab0fe

Please sign in to comment.