Repository: vercel/next.js
Pull request: test: fix failures caused by format discrepancies between rspack and other bundlers

This pull request updates error recovery tests to improve compatibility with the Rspack bundler by introducing an `isRspack` flag and modifying test cases to handle Rspack-specific error formats. The changes ensure that runtime and build errors are correctly detected and displayed when using Rspack.

### Rspack Compatibility Updates:

* Introduced the `isRspack` flag to determine if Rspack is being used, based on the `NEXT_RSPACK` environment variable. This flag replaces direct checks of `process.env.NEXT_RSPACK` for better readability and maintainability. (`test/development/acceptance-app/error-recovery.test.ts` [[1]](diffhunk://#diff-ed42d009889881804f76e500465e132e4aa1df75c8659119e1b5cff4d230b7cdR8-R9) `test/development/acceptance/error-recovery.test.ts` [[2]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91R9)

* Updated test cases in `error-recovery.test.ts` and `acceptance-app/error-recovery.test.ts` to handle Rspack-specific error messages. This includes adapting error expectations for syntax errors, runtime errors, and build errors with the Rspack error format. (`test/development/acceptance-app/error-recovery.test.ts` [[1]](diffhunk://#diff-ed42d009889881804f76e500465e132e4aa1df75c8659119e1b5cff4d230b7cdR58-R79) [[2]](diffhunk://#diff-ed42d009889881804f76e500465e132e4aa1df75c8659119e1b5cff4d230b7cdR574-R588) [[3]](diffhunk://#diff-ed42d009889881804f76e500465e132e4aa1df75c8659119e1b5cff4d230b7cdR845-R872); `test/development/acceptance/error-recovery.test.ts` [[4]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91L58-R74) [[5]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91L372-R394) [[6]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91L755-R766)

* Replaced outdated error message formats in Rspack-related test cases with updated and more precise error descriptions, including detailed stack traces and error causes. (`test/development/acceptance/error-recovery.test.ts` [[1]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91L815-R835) [[2]](diffhunk://#diff-18cc654bcba9dbffcf77a6157fec144f9d0b1a0b3c59d46b33f6a2b95f543a91L883-R903)

These changes improve test coverage for Rspack and ensure robust error handling across different bundlers.

Changed files:
filename: test/development/acceptance-app/error-recovery.test.ts
patch: @@ -5,6 +5,8 @@ import { check } from 'next-test-utils'
 import path from 'path'
 import { outdent } from 'outdent'
 
+const isRspack = !!process.env.NEXT_RSPACK
+
 describe('Error recovery app', () => {
   const { next, isTurbopack } = nextTestSetup({
     files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
@@ -53,6 +55,28 @@ describe('Error recovery app', () => {
          "stack": [],
        }
       `)
+    } else if (isRspack) {
+      await expect({ browser, next }).toDisplayRedbox(`
+       {
+         "description": "  × Module build failed:",
+         "environmentLabel": null,
+         "label": "Build Error",
+         "source": "./index.js
+         × Module build failed:
+         ╰─▶   × Error:   x Unexpected eof
+               │    ,---- 
+               │  1 | export default () => <div/
+               │    \`----
+               │ 
+               │ 
+               │ Caused by:
+               │     Syntax Error
+       Import trace for requested module:
+       ./index.js
+       ./app/page.js",
+         "stack": [],
+       }
+      `)
     } else {
       await expect(browser).toDisplayRedbox(`
        {
@@ -547,19 +571,35 @@ describe('Error recovery app', () => {
     )
 
     await browser.eval('window.triggerError()')
-    await expect(browser).toDisplayCollapsedRedbox(`
-     {
-       "description": "no 1",
-       "environmentLabel": null,
-       "label": "Runtime Error",
-       "source": "index.js (7:11) @ eval
-     >  7 |     throw Error('no ' + i)
-          |           ^",
-       "stack": [
-         "eval index.js (7:11)",
-       ],
-     }
-    `)
+    if (isRspack) {
+      await expect(browser).toDisplayCollapsedRedbox(`
+         {
+           "description": "no 1",
+           "environmentLabel": null,
+           "label": "Runtime Error",
+           "source": "index.js (7:11) @ eval
+         > 7 |     throw Error('no ' + i)
+             |           ^",
+           "stack": [
+             "eval index.js (7:11)",
+           ],
+         }
+        `)
+    } else {
+      await expect(browser).toDisplayCollapsedRedbox(`
+       {
+         "description": "no 1",
+         "environmentLabel": null,
+         "label": "Runtime Error",
+         "source": "index.js (7:11) @ eval
+       >  7 |     throw Error('no ' + i)
+            |           ^",
+         "stack": [
+           "eval index.js (7:11)",
+         ],
+       }
+      `)
+    }
 
     // Make a syntax error.
     await session.patch(
@@ -802,6 +842,34 @@ describe('Error recovery app', () => {
          "stack": [],
        }
       `)
+    } else if (isRspack) {
+      await expect({ browser, next }).toDisplayRedbox(`
+       {
+         "description": "  × Module build failed:",
+         "environmentLabel": null,
+         "label": "Build Error",
+         "source": "./index.js
+         × Module build failed:
+         ╰─▶   × Error:   x Expected '{', got 'return'
+               │    ,-[5:1]
+               │  2 |
+               │  3 | class ClassDefault extends React.Component {
+               │  4 |   render()
+               │  5 |     return <h1>Default Export</h1>;
+               │    :     ^^^^^^
+               │  6 |   }
+               │  7 | }
+               │    \`----
+               │ 
+               │ 
+               │ Caused by:
+               │     Syntax Error
+       Import trace for requested module:
+       ./index.js
+       ./app/page.js",
+         "stack": [],
+       }
+      `)
     } else {
       await expect(browser).toDisplayRedbox(`
        {

filename: test/development/acceptance/error-recovery.test.ts
patch: @@ -6,6 +6,7 @@ import { outdent } from 'outdent'
 import path from 'path'
 
 const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18
+const isRspack = !!process.env.NEXT_RSPACK
 
 describe('pages/ error recovery', () => {
   const { next, isTurbopack } = nextTestSetup({
@@ -55,7 +56,7 @@ describe('pages/ error recovery', () => {
          "stack": [],
        }
       `)
-    } else if (process.env.NEXT_RSPACK) {
+    } else if (isRspack) {
       await expect({ browser, next }).toDisplayRedbox(`
        {
          "description": "  × Module build failed:",
@@ -369,7 +370,7 @@ describe('pages/ error recovery', () => {
          "stack": [],
        }
       `)
-    } else if (process.env.NEXT_RSPACK) {
+    } else if (isRspack) {
       await expect({ browser, next }).toDisplayRedbox(`
        {
          "description": "  × Module build failed:",
@@ -454,7 +455,7 @@ describe('pages/ error recovery', () => {
          "stack": [],
        }
       `)
-    } else if (process.env.NEXT_RSPACK) {
+    } else if (isRspack) {
       await expect({ browser, next }).toDisplayRedbox(`
        {
          "description": "  × Module build failed:",
@@ -570,7 +571,7 @@ describe('pages/ error recovery', () => {
        ]
       `)
     } else {
-      if (process.env.NEXT_RSPACK) {
+      if (isRspack) {
         await expect(browser).toDisplayRedbox(`
          {
            "description": "nooo",
@@ -752,17 +753,17 @@ describe('pages/ error recovery', () => {
     )
     await new Promise((resolve) => setTimeout(resolve, 1000))
 
-    if (process.env.NEXT_RSPACK) {
+    if (isRspack) {
       await expect(browser).toDisplayRedbox(`
             {
               "description": "no 1",
               "environmentLabel": null,
               "label": "Runtime Error",
-              "source": "index.js (5:9) @ <unknown>
+              "source": "index.js (5:9) @ eval
             > 5 |   throw Error('no ' + i)
                 |         ^",
               "stack": [
-                "<unknown> index.js (5:9)",
+                "eval index.js (5:9)",
               ],
             }
           `)
@@ -812,7 +813,7 @@ describe('pages/ error recovery', () => {
          "stack": [],
        }
       `)
-    } else if (process.env.NEXT_RSPACK) {
+    } else if (isRspack) {
       await expect({ browser, next }).toDisplayRedbox(`
        {
          "description": "  × Module build failed:",
@@ -880,7 +881,7 @@ describe('pages/ error recovery', () => {
          "stack": [],
        }
       `)
-    } else if (process.env.NEXT_RSPACK) {
+    } else if (isRspack) {
       await expect({ browser, next }).toDisplayRedbox(`
        {
          "description": "  × Module build failed:",