Repository: vercel/next.js
Pull request: Turbopack build: Fix production-browser-sourcemaps test

## What?

The test assumes that the sourcemap files live at a certain place and match the js filename. In Turbopack they don't because they're content hashed.

This fixes the tests by reading the sourcemap path and reading using that path instead.

Closes PACK-4639

Changed files:
filename: test/integration/production-browser-sourcemaps/test/index.test.js
patch: @@ -1,41 +1,26 @@
 /* eslint-env jest */
 import fs from 'fs-extra'
-import { join } from 'path'
-import { nextBuild, getPageFileFromBuildManifest } from 'next-test-utils'
+import { dirname, join } from 'path'
+import { nextBuild, getPageFilesFromBuildManifest } from 'next-test-utils'
 import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
 
 const appDir = join(__dirname, '../')
 
-function runTests() {
-  it('includes sourcemaps for all browser files', async () => {
-    const browserFiles = await recursiveReadDir(join(appDir, '.next', 'static'))
-    const jsFiles = browserFiles.filter(
-      (file) => file.endsWith('.js') && file.includes('/pages/')
-    )
-    expect(jsFiles).not.toBeEmpty()
-
-    jsFiles.forEach((file) => {
-      expect(browserFiles.includes(`${file}.map`)).toBe(true)
-    })
-  })
-
-  it('correctly generated the source map', async () => {
-    const map = JSON.parse(
-      await fs.readFile(
-        join(
-          appDir,
-          '.next',
-          (await getPageFileFromBuildManifest(appDir, '/static')) + '.map'
-        ),
-        'utf8'
-      )
-    )
+function extractSourceMappingURL(jsContent) {
+  // Matches both //# and //@ sourceMappingURL=...
+  const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
+  return match ? match[1] : null
+}
 
-    expect(map.sources).toContainEqual(
-      expect.stringMatching(/pages[/\\]static\.js/)
-    )
-    expect(map.names).toContainEqual('StaticPage')
-  })
+async function checkSourceMapExistsForFile(jsFilePath) {
+  const jsContent = await fs.readFile(jsFilePath, 'utf8')
+  const sourceMappingURL = extractSourceMappingURL(jsContent)
+  if (!sourceMappingURL) {
+    return
+  }
+  expect(sourceMappingURL).toBeTruthy()
+  const mapPath = join(dirname(jsFilePath), sourceMappingURL)
+  expect(await fs.pathExists(mapPath)).toBe(true)
 }
 
 describe('Production browser sourcemaps', () => {
@@ -47,7 +32,28 @@ describe('Production browser sourcemaps', () => {
           await nextBuild(appDir, [], {})
         })
 
-        runTests()
+        it('includes sourcemaps for all browser files', async () => {
+          const staticDir = join(appDir, '.next', 'static')
+          const browserFiles = await recursiveReadDir(staticDir)
+          const jsFiles = browserFiles.filter(
+            (file) => file.endsWith('.js') && file.includes('/pages/')
+          )
+          expect(jsFiles).not.toBeEmpty()
+
+          for (const file of jsFiles) {
+            const jsPath = join(staticDir, file)
+            checkSourceMapExistsForFile(jsPath)
+          }
+        })
+
+        it('correctly generated the source map', async () => {
+          const dotNextDir = join(appDir, '.next')
+          const jsFiles = getPageFilesFromBuildManifest(appDir, '/static')
+          for (const file of jsFiles) {
+            const jsPath = join(dotNextDir, file)
+            checkSourceMapExistsForFile(jsPath)
+          }
+        })
       })
     }
   )

filename: test/turbopack-build-tests-manifest.json
patch: @@ -16971,11 +16971,11 @@
     "runtimeError": false
   },
   "test/integration/production-browser-sourcemaps/test/index.test.js": {
-    "passed": [],
-    "failed": [
+    "passed": [
       "Production browser sourcemaps production mode Server support correctly generated the source map",
       "Production browser sourcemaps production mode Server support includes sourcemaps for all browser files"
     ],
+    "failed": [],
     "pending": [],
     "flakey": [],
     "runtimeError": false