Skip to content

Commit

Permalink
add test for non-custom-app treeview
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Mar 29, 2022
1 parent b3290c3 commit c283925
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 94 deletions.
Expand Up @@ -65,4 +65,11 @@ export default async function basic(context, { env }) {
expect(content).toMatchInlineSnapshot('"foo.clientbar.client"')
expect(dynamicIds).toBe(undefined)
})

if (env === 'prod') {
it(`should not display custom _app or _app.server in treeview if there's not any`, () => {
const { stdout } = context
expect(stdout).not.toMatch(/\s\/_app(\.server)?/)
})
}
}
Expand Up @@ -77,106 +77,83 @@ describe('Edge runtime - errors', () => {
})
})

describe('Edge runtime - prod', () => {
const context = { appDir }

beforeAll(async () => {
error500Page.write(page500)
context.appPort = await findPort()
const { stderr } = await nextBuild(context.appDir)
context.stderr = stderr
context.server = await nextStart(context.appDir, context.appPort)
})
afterAll(async () => {
error500Page.delete()
await killApp(context.server)
})

it('should warn user for experimental risk with edge runtime and server components', async () => {
const edgeRuntimeWarning =
'You are using the experimental Edge Runtime with `experimental.runtime`.'
const rscWarning = `You have experimental React Server Components enabled. Continue at your own risk.`
expect(context.stderr).toContain(edgeRuntimeWarning)
expect(context.stderr).toContain(rscWarning)
})
const edgeRuntimeBasicSuite = {
runTests: (context, env) => {
const options = { runtime: 'edge', env }
basic(context, options)
streaming(context, options)
rsc(context, options)
runtime(context, options)

it('should generate middleware SSR manifests for edge runtime', async () => {
const distServerDir = join(distDir, 'server')
const files = [
'middleware-build-manifest.js',
'middleware-ssr-runtime.js',
'middleware-flight-manifest.js',
'middleware-flight-manifest.json',
'middleware-manifest.json',
]

const requiredServerFiles = (
await fs.readJSON(join(distDir, 'required-server-files.json'))
).files

files.forEach((file) => {
const filepath = join(distServerDir, file)
expect(fs.existsSync(filepath)).toBe(true)
})
if (env === 'dev') {
it('should have content-type and content-encoding headers', async () => {
const res = await fetchViaHTTP(context.appPort, '/')
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
expect(res.headers.get('content-encoding')).toBe('gzip')
})
}
if (env === 'prod') {
it('should warn user for experimental risk with edge runtime and server components', async () => {
const edgeRuntimeWarning =
'You are using the experimental Edge Runtime with `experimental.runtime`.'
const rscWarning = `You have experimental React Server Components enabled. Continue at your own risk.`
expect(context.stderr).toContain(edgeRuntimeWarning)
expect(context.stderr).toContain(rscWarning)
})

requiredServerFiles.forEach((file) => {
const requiredFilePath = join(appDir, file)
expect(fs.existsSync(requiredFilePath)).toBe(true)
})
})
it('should generate middleware SSR manifests for edge runtime', async () => {
const distServerDir = join(distDir, 'server')
const files = [
'middleware-build-manifest.js',
'middleware-ssr-runtime.js',
'middleware-flight-manifest.js',
'middleware-flight-manifest.json',
'middleware-manifest.json',
]

it('should have clientInfo in middleware manifest', async () => {
const middlewareManifestPath = join(
distDir,
'server',
'middleware-manifest.json'
)
const content = JSON.parse(
await fs.readFile(middlewareManifestPath, 'utf8')
)
for (const item of [
['/', true],
['/next-api/image', true],
['/next-api/link', true],
['/routes/[dynamic]', true],
]) {
expect(content.clientInfo).toContainEqual(item)
}
expect(content.clientInfo).not.toContainEqual([['/404', true]])
})
const requiredServerFiles = (
await fs.readJSON(join(distDir, 'required-server-files.json'))
).files

const options = { runtime: 'edge', env: 'prod' }
basic(context, options)
streaming(context, options)
rsc(context, options)
runtime(context, options)
})
files.forEach((file) => {
const filepath = join(distServerDir, file)
expect(fs.existsSync(filepath)).toBe(true)
})

describe('Edge runtime - dev', () => {
const context = { appDir }
requiredServerFiles.forEach((file) => {
const requiredFilePath = join(appDir, file)
expect(fs.existsSync(requiredFilePath)).toBe(true)
})
})

beforeAll(async () => {
it('should have clientInfo in middleware manifest', async () => {
const middlewareManifestPath = join(
distDir,
'server',
'middleware-manifest.json'
)
const content = JSON.parse(
await fs.readFile(middlewareManifestPath, 'utf8')
)
for (const item of [
['/', true],
['/next-api/image', true],
['/next-api/link', true],
['/routes/[dynamic]', true],
]) {
expect(content.clientInfo).toContainEqual(item)
}
expect(content.clientInfo).not.toContainEqual([['/404', true]])
})
}
},
beforeAll: () => {
error500Page.write(page500)
context.appPort = await findPort()
context.server = await nextDev(context.appDir, context.appPort)
})
afterAll(async () => {
},
afterAll: () => {
error500Page.delete()
await killApp(context.server)
})

it('should have content-type and content-encoding headers', async () => {
const res = await fetchViaHTTP(context.appPort, '/')
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
expect(res.headers.get('content-encoding')).toBe('gzip')
})

const options = { runtime: 'edge', env: 'dev' }
basic(context, options)
streaming(context, options)
rsc(context, options)
runtime(context, options)
})
},
}

const nodejsRuntimeBasicSuite = {
runTests: (context, env) => {
Expand Down Expand Up @@ -253,6 +230,8 @@ const documentSuite = {

runSuite('Node.js runtime', 'dev', nodejsRuntimeBasicSuite)
runSuite('Node.js runtime', 'prod', nodejsRuntimeBasicSuite)
runSuite('Edge runtime', 'dev', edgeRuntimeBasicSuite)
runSuite('Edge runtime', 'prod', edgeRuntimeBasicSuite)

runSuite('Custom App', 'dev', customAppPageSuite)
runSuite('Custom App', 'prod', customAppPageSuite)
Expand All @@ -270,7 +249,9 @@ function runSuite(suiteName, env, options) {
options.beforeAll?.()
if (env === 'prod') {
context.appPort = await findPort()
await nextBuild(context.appDir)
const { stdout, stderr } = await nextBuild(context.appDir)
context.stdout = stdout
context.stderr = stderr
context.server = await nextStart(context.appDir, context.appPort)
}
if (env === 'dev') {
Expand Down
Expand Up @@ -15,7 +15,6 @@ export default function (context, { runtime, env }) {

// should have only 1 DOCTYPE
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)

expect(homeHTML).toContain('component:index.server')
expect(homeHTML).toContain('env:env_var_test')
expect(homeHTML).toContain('header:test-util')
Expand Down

0 comments on commit c283925

Please sign in to comment.