Skip to content

Commit e06d2d8

Browse files
authored
Turbopack build: Fix production-browser-sourcemaps test (#79374)
## 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.
1 parent 2d48158 commit e06d2d8

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

test/integration/production-browser-sourcemaps/test/index.test.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,26 @@
11
/* eslint-env jest */
22
import fs from 'fs-extra'
3-
import { join } from 'path'
4-
import { nextBuild, getPageFileFromBuildManifest } from 'next-test-utils'
3+
import { dirname, join } from 'path'
4+
import { nextBuild, getPageFilesFromBuildManifest } from 'next-test-utils'
55
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
66

77
const appDir = join(__dirname, '../')
88

9-
function runTests() {
10-
it('includes sourcemaps for all browser files', async () => {
11-
const browserFiles = await recursiveReadDir(join(appDir, '.next', 'static'))
12-
const jsFiles = browserFiles.filter(
13-
(file) => file.endsWith('.js') && file.includes('/pages/')
14-
)
15-
expect(jsFiles).not.toBeEmpty()
16-
17-
jsFiles.forEach((file) => {
18-
expect(browserFiles.includes(`${file}.map`)).toBe(true)
19-
})
20-
})
21-
22-
it('correctly generated the source map', async () => {
23-
const map = JSON.parse(
24-
await fs.readFile(
25-
join(
26-
appDir,
27-
'.next',
28-
(await getPageFileFromBuildManifest(appDir, '/static')) + '.map'
29-
),
30-
'utf8'
31-
)
32-
)
9+
function extractSourceMappingURL(jsContent) {
10+
// Matches both //# and //@ sourceMappingURL=...
11+
const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
12+
return match ? match[1] : null
13+
}
3314

34-
expect(map.sources).toContainEqual(
35-
expect.stringMatching(/pages[/\\]static\.js/)
36-
)
37-
expect(map.names).toContainEqual('StaticPage')
38-
})
15+
async function checkSourceMapExistsForFile(jsFilePath) {
16+
const jsContent = await fs.readFile(jsFilePath, 'utf8')
17+
const sourceMappingURL = extractSourceMappingURL(jsContent)
18+
if (!sourceMappingURL) {
19+
return
20+
}
21+
expect(sourceMappingURL).toBeTruthy()
22+
const mapPath = join(dirname(jsFilePath), sourceMappingURL)
23+
expect(await fs.pathExists(mapPath)).toBe(true)
3924
}
4025

4126
describe('Production browser sourcemaps', () => {
@@ -47,7 +32,28 @@ describe('Production browser sourcemaps', () => {
4732
await nextBuild(appDir, [], {})
4833
})
4934

50-
runTests()
35+
it('includes sourcemaps for all browser files', async () => {
36+
const staticDir = join(appDir, '.next', 'static')
37+
const browserFiles = await recursiveReadDir(staticDir)
38+
const jsFiles = browserFiles.filter(
39+
(file) => file.endsWith('.js') && file.includes('/pages/')
40+
)
41+
expect(jsFiles).not.toBeEmpty()
42+
43+
for (const file of jsFiles) {
44+
const jsPath = join(staticDir, file)
45+
checkSourceMapExistsForFile(jsPath)
46+
}
47+
})
48+
49+
it('correctly generated the source map', async () => {
50+
const dotNextDir = join(appDir, '.next')
51+
const jsFiles = getPageFilesFromBuildManifest(appDir, '/static')
52+
for (const file of jsFiles) {
53+
const jsPath = join(dotNextDir, file)
54+
checkSourceMapExistsForFile(jsPath)
55+
}
56+
})
5157
})
5258
}
5359
)

test/turbopack-build-tests-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16971,11 +16971,11 @@
1697116971
"runtimeError": false
1697216972
},
1697316973
"test/integration/production-browser-sourcemaps/test/index.test.js": {
16974-
"passed": [],
16975-
"failed": [
16974+
"passed": [
1697616975
"Production browser sourcemaps production mode Server support correctly generated the source map",
1697716976
"Production browser sourcemaps production mode Server support includes sourcemaps for all browser files"
1697816977
],
16978+
"failed": [],
1697916979
"pending": [],
1698016980
"flakey": [],
1698116981
"runtimeError": false

0 commit comments

Comments
 (0)