-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
basic.dev.test.js
206 lines (184 loc) · 6.37 KB
/
basic.dev.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import path from 'path'
import jsdom from 'jsdom'
import consola from 'consola'
import { Builder, BundleBuilder, getPort, loadFixture, Nuxt, rp, waitFor } from '../utils'
let port
const url = route => 'http://localhost:' + port + route
let nuxt = null
let builder = null
let transpile = null
let output = null
let loadersOptions
let vueLoader
let postcssLoader
describe('basic dev', () => {
beforeAll(async () => {
const config = await loadFixture('basic', {
dev: true,
debug: true,
buildDir: '.nuxt-dev',
build: {
filenames: {
app: ({ isDev }) => {
return isDev ? 'test-app.js' : 'test-app.[contenthash].js'
},
chunk: 'test-[name].[contenthash].js'
},
transpile: [
'@scoped/packageA',
'@scoped\\packageB',
'vue.test.js',
/vue-test/,
({ isModern }) => isModern ? 'modern-test' : 'normal-test'
],
loaders: {
cssModules: {
modules: {
localIdentName: '[hash:base64:6]'
}
}
},
extend ({ module: { rules }, output: wpOutput }, { isClient, loaders }) {
if (isClient) {
const babelLoader = rules.find(loader => loader.test.test('.jsx'))
transpile = file => !babelLoader.exclude(file)
output = wpOutput
loadersOptions = loaders
vueLoader = rules.find(loader => loader.test.test('.vue'))
const cssLoaders = rules.find(loader => loader.test.test('.css')).oneOf[0].use
postcssLoader = cssLoaders[cssLoaders.length - 1]
}
}
},
hooks: {
'vue-renderer:ssr:context': ({ nuxt }) => {
nuxt.logs = [{ type: 'log', args: ['This is a test ssr log'] }]
}
},
render: {
ssrLog: 'collapsed'
}
})
nuxt = new Nuxt(config)
await nuxt.ready()
builder = new Builder(nuxt, BundleBuilder)
await builder.build()
await waitFor(2000) // TODO: Find a better way
port = await getPort()
await nuxt.server.listen(port, 'localhost')
})
test('Check build:done hook called', () => {
expect(builder.__hook_built_called__).toBe(true)
})
test('Config: build.transpile', () => {
expect(transpile('vue-test')).toBe(true)
expect(transpile(path.normalize('node_modules/test.js'))).toBe(false)
expect(transpile(path.normalize('node_modules/vue-test'))).toBe(true)
expect(transpile(path.normalize('node_modules/vue.test.js'))).toBe(true)
expect(transpile(path.normalize('node_modules/test.vue.js'))).toBe(true)
expect(transpile(path.normalize('node_modules/@scoped/packageA/src/index.js'))).toBe(true)
expect(transpile(path.normalize('node_modules/@scoped/packageB/src/index.js'))).toBe(true)
expect(transpile(path.normalize('node_modules/normal-test'))).toBe(true)
})
test('Config: build.filenames', () => {
expect(output.filename).toBe('test-app.js')
expect(output.chunkFilename).toBe('test-[name].[contenthash].js')
expect(consola.warn).toBeCalledWith(
'Notice: Please do not use contenthash in dev mode to prevent memory leak'
)
})
test('Config: build.loaders', () => {
expect(Object.keys(loadersOptions)).toHaveLength(12)
expect(loadersOptions).toHaveProperty(
'file', 'fontUrl', 'imgUrl', 'pugPlain', 'vue',
'css', 'cssModules', 'less', 'sass', 'scss', 'stylus', 'vueStyle'
)
const { cssModules, vue } = loadersOptions
expect(cssModules.modules.localIdentName).toBe('[hash:base64:6]')
expect(vueLoader.options).toBe(vue)
})
test('Config: preset-env and cssnano are at then end of postcss plugins', () => {
const plugins = postcssLoader.options.plugins.map((plugin) => {
return plugin.postcssPlugin
})
expect(plugins).toEqual([
'postcss-import',
'postcss-url',
'nuxt-test',
'postcss-preset-env',
'cssnano'
])
})
test('/stateless', async () => {
const window = await nuxt.server.renderAndGetWindow(url('/stateless'))
const html = window.document.body.innerHTML
expect(html).toContain('<h1>My component!</h1>')
// Check render:routeDone hook called
expect(nuxt.__hook_render_routeDone__).toBe('/stateless')
window.close()
})
// test('/_nuxt/test.hot-update.json should returns empty html', async t => {
// try {
// await rp(url('/_nuxt/test.hot-update.json'))
// } catch (err) {
// t.is(err.statusCode, 404)
// t.is(err.response.body, '')
// }
// })
test('/__open-in-editor (open-in-editor)', async () => {
const { body } = await rp(
url('/__open-in-editor?file=pages/index.vue'),
{ resolveWithFullResponse: true }
)
expect(body).toBe('')
})
test('/__open-in-editor should return error (open-in-editor)', async () => {
await expect(rp(url('/__open-in-editor?file='))).rejects.toMatchObject({
statusCode: 500,
error: 'launch-editor-middleware: required query param "file" is missing.'
})
})
test('/error should return error stack trace (Youch)', async () => {
await expect(nuxt.server.renderAndGetWindow(url('/error'))).rejects.toMatchObject({
statusCode: 500
})
})
test('/error should return json format error (Youch)', async () => {
const opts = {
headers: {
accept: 'application/json'
},
resolveWithFullResponse: true
}
await expect(rp(url('/error'), opts)).rejects.toMatchObject({
statusCode: 500,
response: {
headers: {
'content-type': 'text/json; charset=utf-8'
}
}
})
})
test('/ should display ssr log in collapsed group', async () => {
const virtualConsole = new jsdom.VirtualConsole()
const groupCollapsed = jest.fn()
const groupEnd = jest.fn()
const log = jest.fn()
virtualConsole.on('groupCollapsed', groupCollapsed)
virtualConsole.on('groupEnd', groupEnd)
virtualConsole.on('log', log)
await nuxt.server.renderAndGetWindow(url('/'), {
virtualConsole
})
expect(groupCollapsed).toHaveBeenCalledWith(
'%cNuxt SSR',
'background: #2E495E;border-radius: 0.5em;color: white;font-weight: bold;padding: 2px 0.5em;'
)
expect(groupEnd).toHaveBeenCalled()
expect(log).toHaveBeenCalledWith('This is a test ssr log')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})
})