forked from nuxt/nuxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder.plugin.test.js
More file actions
189 lines (162 loc) · 6.21 KB
/
builder.plugin.test.js
File metadata and controls
189 lines (162 loc) · 6.21 KB
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
import Glob from 'glob'
import consola from 'consola'
import { isIndexFileAndFolder } from '@nuxt/utils'
import { BundleBuilder } from '@nuxt/webpack'
import Builder from '../src/builder'
import { createNuxt } from './__utils__'
jest.mock('glob')
jest.mock('pify', () => fn => fn)
jest.mock('hash-sum', () => src => `hash(${src})`)
jest.mock('@nuxt/utils')
jest.mock('../src/ignore')
jest.mock('@nuxt/webpack')
describe('builder: builder plugins', () => {
beforeEach(() => {
jest.clearAllMocks()
})
test('should normalize plugins', async () => {
const nuxt = createNuxt()
nuxt.options.plugins = [
'/var/nuxt/plugins/test.js',
'/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js',
{ src: '/var/nuxt/plugins/test.server', mode: 'server' },
{ src: '/var/nuxt/plugins/test.client', ssr: false }
]
const builder = new Builder(nuxt, BundleBuilder)
const plugins = await builder.normalizePlugins()
expect(nuxt.callHook).toBeCalledTimes(1)
expect(nuxt.callHook).toBeCalledWith('builder:extendPlugins', nuxt.options.plugins)
expect(plugins).toEqual([
{
mode: 'all',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)',
src: 'resolveAlias(/var/nuxt/plugins/test.js)'
},
{
mode: 'client',
name: 'nuxt_plugin_foobarpluginclient530b6c6a_hash(/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js)',
src: 'resolveAlias(/var/nuxt/.nuxt/foo-bar.plugin.client.530b6c6a.js)'
},
{
mode: 'server',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)',
src: 'resolveAlias(/var/nuxt/plugins/test.server)'
},
{
mode: 'client',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)',
src: 'resolveAlias(/var/nuxt/plugins/test.client)'
}
])
})
test('should overwrite plugins from options', async () => {
const nuxt = createNuxt()
nuxt.options.plugins = ['/var/nuxt/plugins/foo-bar.js']
nuxt.options.extendPlugins = jest.fn().mockReturnValue([
'/var/nuxt/plugins/fizz-fuzz.js'
])
const builder = new Builder(nuxt, BundleBuilder)
const plugins = await builder.normalizePlugins()
expect(nuxt.options.extendPlugins).toHaveBeenCalledTimes(1)
expect(nuxt.options.extendPlugins).toHaveBeenCalledWith([
'/var/nuxt/plugins/foo-bar.js'
])
expect(plugins).toEqual([
{
mode: 'all',
name: 'nuxt_plugin_fizzfuzz_hash(/var/nuxt/plugins/fizz-fuzz.js)',
src: 'resolveAlias(/var/nuxt/plugins/fizz-fuzz.js)'
}
])
})
test('should warning and fallback invalid mode when normalize plugins', async () => {
const nuxt = createNuxt()
nuxt.options.plugins = [
{ src: '/var/nuxt/plugins/test', mode: 'abc' }
]
const builder = new Builder(nuxt, BundleBuilder)
const plugins = await builder.normalizePlugins()
expect(plugins).toEqual([
{
mode: 'all',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test)',
src: 'resolveAlias(/var/nuxt/plugins/test)'
}
])
expect(consola.warn).toBeCalledTimes(1)
expect(consola.warn).toBeCalledWith('Invalid plugin mode (server/client/all): \'abc\'. Falling back to \'all\'')
})
test('should resolve plugins', async () => {
const nuxt = createNuxt()
const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client', mode: 'client' },
{ src: '/var/nuxt/plugins/test.server', mode: 'server' }
]
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
for (let step = 0; step < builder.plugins.length; step++) {
Glob.mockImplementationOnce(src => [`${src.replace(/\{.*\}/, '')}.js`])
}
await builder.resolvePlugins()
expect(Glob).toBeCalledTimes(3)
expect(Glob).nthCalledWith(1, '/var/nuxt/plugins/test.js{?(.+([^.])),/index.+([^.])}')
expect(Glob).nthCalledWith(2, '/var/nuxt/plugins/test.client{?(.+([^.])),/index.+([^.])}')
expect(Glob).nthCalledWith(3, '/var/nuxt/plugins/test.server{?(.+([^.])),/index.+([^.])}')
expect(builder.plugins).toEqual([
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test.js)' },
{ mode: 'client', src: 'relative(/var/nuxt/plugins/test.client)' },
{ mode: 'server', src: 'relative(/var/nuxt/plugins/test.server)' }
])
})
test('should throw error if plugin no existed', async () => {
const nuxt = createNuxt()
const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' }
]
Glob.mockImplementationOnce(() => [])
await expect(builder.resolvePlugins()).rejects.toThrow('Plugin not found: /var/nuxt/plugins/test.js')
})
test('should warn if there are multiple files and not index', async () => {
const nuxt = createNuxt()
const builder = new Builder(nuxt, BundleBuilder)
builder.plugins = [
{ src: '/var/nuxt/plugins/test', mode: 'all' }
]
builder.relativeToBuild = jest.fn(src => `relative(${src})`)
Glob.mockImplementationOnce(src => [`${src}.js`])
isIndexFileAndFolder.mockReturnValueOnce(false)
await builder.resolvePlugins()
expect(builder.plugins).toEqual([
{ mode: 'all', src: 'relative(/var/nuxt/plugins/test)' }
])
})
test('should detect plugin mode for client/server plugins', async () => {
const nuxt = createNuxt()
const builder = new Builder(nuxt, BundleBuilder)
builder.options.plugins = [
{ src: '/var/nuxt/plugins/test.js', mode: 'all' },
{ src: '/var/nuxt/plugins/test.client' },
{ src: '/var/nuxt/plugins/test.server' }
]
const plugins = await builder.normalizePlugins()
expect(plugins).toEqual([
{
mode: 'all',
src: 'resolveAlias(/var/nuxt/plugins/test.js)',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.js)'
},
{
mode: 'client',
src: 'resolveAlias(/var/nuxt/plugins/test.client)',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.client)'
},
{
mode: 'server',
src: 'resolveAlias(/var/nuxt/plugins/test.server)',
name: 'nuxt_plugin_test_hash(/var/nuxt/plugins/test.server)'
}
])
})
})