Skip to content

Commit

Permalink
add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed May 6, 2024
1 parent e13df6d commit 3c9be21
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
47 changes: 34 additions & 13 deletions test/production/deterministic-build/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto'
import { nextTestSetup } from 'e2e-utils'
import { NextInstance, nextTestSetup } from 'e2e-utils'
import { nextBuild } from 'next-test-utils'

function generateMD5(text: string) {
Expand All @@ -8,41 +8,62 @@ function generateMD5(text: string) {
return hash.digest('hex')
}

const filePaths = [
const nodeFilePaths = [
'app/app-page/page',
'app/app-page/edge/page',
'app/app-route/route',
'app/app-route/edge/route',
'pages/api/pages-api',
'pages/api/pages-api/edge',
'pages/pages-page',
'pages/pages-page/edge',
]

async function getEdgeRouteFilesFromManifest(next: NextInstance) {
const manifest: any = JSON.parse(
await next.readFile('.next/server/middleware-manifest.json')
)
const routeKeys = Object.keys(manifest.functions)
const md5Map: Record<string, string[]> = {}
for (const route of routeKeys) {
const files: string[] = manifest.functions[route].files
const filesMd5Promises = files.map(async (filePath: string) => {
const content = await next.readFile(`.next/${filePath}`)
return generateMD5(content)
})
const md5s = await Promise.all(filesMd5Promises)
md5Map[route] = md5s
}
return md5Map
}

describe('deterministic build', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
})

const firstBuildFileMd5Hashes: Record<string, string> = {}
const secondBuildFileMd5Hashes: Record<string, string> = {}
// Edge - { [route]: [file md5s] }
const edgeBuildFileMd5Hashes: Record<string, string[]>[] = []
// Node - { [route]: page.js or route.js md5 }
const nodeBuildFileMd5Hashes: Record<string, string>[] = [{}, {}]

beforeAll(async () => {
// First build
await nextBuild(__dirname, [])
for (const file of filePaths) {
edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next))
for (const file of nodeFilePaths) {
const content = await next.readFile(`.next/server/${file}.js`)
firstBuildFileMd5Hashes[file] = generateMD5(content)
nodeBuildFileMd5Hashes[0][file] = generateMD5(content)
}

// Second build
await nextBuild(__dirname, [])
for (const file of filePaths) {
edgeBuildFileMd5Hashes.push(await getEdgeRouteFilesFromManifest(next))
for (const file of nodeFilePaths) {
const content = await next.readFile(`.next/server/${file}.js`)
secondBuildFileMd5Hashes[file] = generateMD5(content)
nodeBuildFileMd5Hashes[1][file] = generateMD5(content)
}
})

it('should have same md5 file across build', async () => {
expect(firstBuildFileMd5Hashes).toEqual(secondBuildFileMd5Hashes)
expect(edgeBuildFileMd5Hashes[0]).toEqual(edgeBuildFileMd5Hashes[1])
expect(nodeBuildFileMd5Hashes[0]).toEqual(nodeBuildFileMd5Hashes[1])
})
})
4 changes: 1 addition & 3 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14307,9 +14307,7 @@
},
"test/production/deterministic-build/index.test.ts": {
"passed": [],
"failed": [
"deterministic build > should have same md5 file across build"
],
"failed": ["deterministic build should have same md5 file across build"],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit 3c9be21

Please sign in to comment.