forked from vercel/next.js
/
index.test.js
167 lines (141 loc) · 4.32 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* eslint-env jest */
/* global jasmine */
import fs from 'fs-extra'
import { join } from 'path'
import {
nextBuild,
findPort,
killApp,
launchApp,
renderViaHTTP,
initNextServerScript,
} from 'next-test-utils'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../')
const nextConfigPath = join(appDir, 'next.config.js')
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const cleanUp = () => fs.remove(nextConfigPath)
const nextStart = async (appDir, appPort) => {
const scriptPath = join(appDir, 'server.js')
const env = Object.assign({ ...process.env }, { PORT: `${appPort}` })
return initNextServerScript(
scriptPath,
/ready on/i,
env,
/ReferenceError: options is not defined/
)
}
describe('Serverless runtime configs', () => {
beforeAll(() => cleanUp())
afterAll(() => cleanUp())
it('should not error on usage of publicRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
publicRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr, code } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(0)
expect(stderr).not.toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
it('should not error on usage of serverRuntimeConfig', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
serverRuntimeConfig: {
hello: 'world'
}
}`
)
const { stderr, code } = await nextBuild(appDir, undefined, {
stderr: true,
})
expect(code).toBe(0)
expect(stderr).not.toMatch(
/Cannot use publicRuntimeConfig or serverRuntimeConfig/
)
})
const testRuntimeConfig = async (app, appPort) => {
const browser = await webdriver(appPort, '/config')
const clientHTML = await browser.eval(`document.documentElement.innerHTML`)
const ssrHTML = await renderViaHTTP(appPort, '/config')
const apiJson = await renderViaHTTP(appPort, '/api/config')
await killApp(app)
await fs.remove(nextConfigPath)
const ssr$ = cheerio.load(ssrHTML)
const client$ = cheerio.load(clientHTML)
const ssrConfig = ssr$('#config').text()
const clientConfig = client$('#config').text()
const expectedSsrConfig = {
publicRuntimeConfig: {
another: 'thing',
},
serverRuntimeConfig: {
hello: 'world',
},
}
const expectedClientConfig = {
publicRuntimeConfig: {
another: 'thing',
},
serverRuntimeConfig: {},
}
expect(JSON.parse(ssrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(clientConfig)).toEqual(expectedClientConfig)
const appSsrConfig = ssr$('#app-config').text()
const appClientConfig = client$('#app-config').text()
expect(JSON.parse(appSsrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(appClientConfig)).toEqual(expectedClientConfig)
const docSsrConfig = ssr$('#doc-config').text()
const docClientConfig = client$('#doc-config').text()
// _document doesn't update on client so should be the same
expect(JSON.parse(docSsrConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(docClientConfig)).toEqual(expectedSsrConfig)
expect(JSON.parse(apiJson)).toEqual(expectedSsrConfig)
}
it('should support runtime configs in serverless mode (production)', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
serverRuntimeConfig: {
hello: 'world'
},
publicRuntimeConfig: {
another: 'thing'
}
}`
)
await nextBuild(appDir, [], { stderr: true, stdout: true })
const appPort = await findPort()
const app = await nextStart(appDir, appPort)
await testRuntimeConfig(app, appPort)
})
it('should support runtime configs in serverless mode (dev)', async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = {
target: 'serverless',
serverRuntimeConfig: {
hello: 'world'
},
publicRuntimeConfig: {
another: 'thing'
}
}`
)
const appPort = await findPort()
const app = await launchApp(appDir, appPort)
await testRuntimeConfig(app, appPort)
})
})