forked from vercel/next.js
/
index.test.js
166 lines (149 loc) · 4.26 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
/* eslint-env jest */
import fs from 'fs-extra'
import { join } from 'path'
import cheerio from 'cheerio'
import webdriver from 'next-webdriver'
import {
findPort,
launchApp,
killApp,
nextBuild,
nextStart,
renderViaHTTP,
} from 'next-test-utils'
jest.setTimeout(1000 * 60 * 2)
let app
let appPort
let stdout
let stderr
const appDir = join(__dirname, '../app')
const nextConfigPath = join(appDir, 'next.config.js')
function runTests() {
it('should render tha page without error', async () => {
const html = await renderViaHTTP(appPort, '/')
expect(html).toMatch(/home/)
})
it('should apply headTags from plugin correctly', async () => {
const html = await renderViaHTTP(appPort, '/')
const $ = cheerio.load(html)
const found = Array.from($('head').children()).find((el) => {
return (el.attribs.src || '').match(/googletagmanager.*?my-tracking-id/)
})
expect(found).toBeTruthy()
})
it('should call clientInit from plugin correctly', async () => {
const browser = await webdriver(appPort, '/')
expect(await browser.eval('window.didClientInit')).toBe(true)
})
it('should list loaded plugins', async () => {
expect(stdout).toMatch(/loaded plugin: @next\/plugin-google-analytics/i)
})
it('should ignore directories in plugins', async () => {
expect(stderr).not.toMatch(/listed invalid middleware/i)
})
}
describe('Next.js plugins', () => {
describe('dev mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { env: { GA_TRACKING_ID: 'my-tracking-id' }, experimental: { plugins: true } }`
)
appPort = await findPort()
app = await launchApp(appDir, appPort, {
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
})
})
afterAll(() => killApp(app))
runTests(true)
describe('with plugin config', () => {
beforeAll(async () => {
await killApp(app)
await fs.writeFile(
nextConfigPath,
`
module.exports = {
experimental: {
plugins: true
},
plugins: [
{
name: '@next/plugin-google-analytics',
config: { hello: 'world' }
}
],
env: {
GA_TRACKING_ID: 'my-tracking-id'
}
}`
)
appPort = await findPort()
stdout = ''
stderr = ''
app = await launchApp(appDir, appPort, {
onStdout(msg) {
stdout += msg
},
onStderr(msg) {
stderr += msg
},
})
})
afterAll(() => killApp(app))
it('should disable auto detecting plugins when plugin config is used', async () => {
expect(stdout).toMatch(/loaded plugin: @next\/plugin-google-analytics/i)
})
it('should expose a plugins config', async () => {
const browser = await webdriver(appPort, '/')
expect(await browser.eval('window.initClientConfig')).toBe('world')
})
})
})
describe('production mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { env: { GA_TRACKING_ID: 'my-tracking-id' }, experimental: { plugins: true } }`
)
const results = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
stdout = results.stdout
stderr = results.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfigPath)
})
runTests()
})
describe('serverless mode', () => {
beforeAll(async () => {
await fs.writeFile(
nextConfigPath,
`module.exports = { target: 'serverless', env: { GA_TRACKING_ID: 'my-tracking-id' }, experimental: { plugins: true } }`
)
const results = await nextBuild(appDir, undefined, {
stdout: true,
stderr: true,
})
stdout = results.stdout
stderr = results.stderr
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(async () => {
await killApp(app)
await fs.remove(nextConfigPath)
})
runTests()
})
})