Unverified Commit e06d2d87 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

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.
parent 2d481584
/* eslint-env jest */ /* eslint-env jest */
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { dirname, join } from 'path'
import { nextBuild, getPageFileFromBuildManifest } from 'next-test-utils' import { nextBuild, getPageFilesFromBuildManifest } from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
const appDir = join(__dirname, '../') const appDir = join(__dirname, '../')
function runTests() { function extractSourceMappingURL(jsContent) {
it('includes sourcemaps for all browser files', async () => { // Matches both //# and //@ sourceMappingURL=...
const browserFiles = await recursiveReadDir(join(appDir, '.next', 'static')) const match = jsContent.match(/\/\/[#@] sourceMappingURL=([^\s]+)/)
const jsFiles = browserFiles.filter( return match ? match[1] : null
(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'
)
)
expect(map.sources).toContainEqual( async function checkSourceMapExistsForFile(jsFilePath) {
expect.stringMatching(/pages[/\\]static\.js/) const jsContent = await fs.readFile(jsFilePath, 'utf8')
) const sourceMappingURL = extractSourceMappingURL(jsContent)
expect(map.names).toContainEqual('StaticPage') if (!sourceMappingURL) {
}) return
}
expect(sourceMappingURL).toBeTruthy()
const mapPath = join(dirname(jsFilePath), sourceMappingURL)
expect(await fs.pathExists(mapPath)).toBe(true)
} }
describe('Production browser sourcemaps', () => { describe('Production browser sourcemaps', () => {
...@@ -47,7 +32,28 @@ describe('Production browser sourcemaps', () => { ...@@ -47,7 +32,28 @@ describe('Production browser sourcemaps', () => {
await nextBuild(appDir, [], {}) 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)
}
})
}) })
} }
) )
......
...@@ -16971,11 +16971,11 @@ ...@@ -16971,11 +16971,11 @@
"runtimeError": false "runtimeError": false
}, },
"test/integration/production-browser-sourcemaps/test/index.test.js": { "test/integration/production-browser-sourcemaps/test/index.test.js": {
"passed": [], "passed": [
"failed": [
"Production browser sourcemaps production mode Server support correctly generated the source map", "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" "Production browser sourcemaps production mode Server support includes sourcemaps for all browser files"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment