Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Mar 11, 2022
1 parent 63f1f3d commit 90f4e79
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const withReact18 = require('../../react-18/test/with-react-18')

module.exports = withReact18({
reactStrictMode: true,
experimental: {
serverComponents: true,
// runtime: 'edge',
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"scripts": {
"lnext": "node -r ../../react-18/test/require-hook.js ../../../../packages/next/dist/bin/next",
"dev": "yarn lnext dev",
"build": "yarn lnext build",
"start": "yarn lnext start"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"/_app": "pages/_app.js",
"/_error": "pages/_error.js",
"/edge-rsc": "pages/edge-rsc.js",
"/static": "pages/static.js",
"/node-rsc": "pages/node-rsc.js",
"/node": "pages/node.js",
"/edge": "pages/edge.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getRuntime from '../utils/runtime'
import getTime from '../utils/time'

export default function Page() {
return (
<div>
This is a SSR RSC page.
<br />
{'Runtime: ' + getRuntime()}
<br />
{'Time: ' + getTime()}
</div>
)
}

export const config = {
runtime: 'edge',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getRuntime from '../utils/runtime'
import getTime from '../utils/time'

export default function Page() {
return (
<div>
This is a SSR page.
<br />
{'Runtime: ' + getRuntime()}
<br />
{'Time: ' + getTime()}
</div>
)
}

export const config = {
runtime: 'edge',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getRuntime from '../utils/runtime'
import getTime from '../utils/time'

export default function Page() {
return (
<div>
This is a static RSC page.
<br />
{'Runtime: ' + getRuntime()}
<br />
{'Time: ' + getTime()}
</div>
)
}

export const config = {
runtime: 'nodejs',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import getRuntime from '../utils/runtime'
import getTime from '../utils/time'

export default function Page() {
return (
<div>
This is a static page.
<br />
{'Runtime: ' + getRuntime()}
<br />
{'Time: ' + getTime()}
</div>
)
}

export const config = {
runtime: 'nodejs',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import getRuntime from '../utils/runtime'
import getTime from '../utils/time'

export default function Page() {
return (
<div>
This is a static page.
<br />
{'Runtime: ' + getRuntime()}
<br />
{'Time: ' + getTime()}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getRuntime() {
return process.version ? `Node.js ${process.version}` : 'Edge/Browser'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getTime() {
return Date.now()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-env jest */

import { join } from 'path'
import {
File,
nextBuild as _nextBuild,
nextStart as _nextStart,
} from 'next-test-utils'

import { findPort, killApp, renderViaHTTP } from 'next-test-utils'

const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')]

const appDir = join(__dirname, '../switchable-runtime')
// const nextConfig = new File(join(appDir, 'next.config.js'))

async function nextBuild(dir, options) {
return await _nextBuild(dir, [], {
...options,
stdout: true,
stderr: true,
nodeArgs,
})
}

async function nextStart(dir, port) {
return await _nextStart(dir, port, {
stdout: true,
stderr: true,
nodeArgs,
})
}

async function testRoute(appPort, url, { isStatic, isEdge }) {
const html1 = await renderViaHTTP(appPort, url)
const renderedAt1 = +html1.match(/Time: (\d+)/)[1]
expect(html1).toContain(`Runtime: ${isEdge ? 'Edge' : 'Node.js'}`)

const html2 = await renderViaHTTP(appPort, url)
const renderedAt2 = +html2.match(/Time: (\d+)/)[1]
expect(html2).toContain(`Runtime: ${isEdge ? 'Edge' : 'Node.js'}`)

if (isStatic) {
// Should not be re-rendered, some timestamp should be returned.
expect(renderedAt1).toBe(renderedAt2)
} else {
// Should be re-rendered.
expect(renderedAt1).toBeLessThan(renderedAt2)
}
}

describe('Without global runtime configuration', () => {
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 build /static as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/static', {
isStatic: true,
isEdge: false,
})
})

it('should build /node as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node', {
isStatic: true,
isEdge: false,
})
})

it('should build /node-rsc as a static page with the nodejs runtime', async () => {
await testRoute(context.appPort, '/node-rsc', {
isStatic: true,
isEdge: false,
})
})

it('should build /edge as a dynamic page with the edge runtime', async () => {
await testRoute(context.appPort, '/edge', {
isStatic: false,
isEdge: true,
})
})

it('should build /edge-rsc as a dynamic page with the edge runtime', async () => {
await testRoute(context.appPort, '/edge-rsc', {
isStatic: false,
isEdge: true,
})
})
})

0 comments on commit 90f4e79

Please sign in to comment.