-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathtest.js
309 lines (252 loc) · 9.58 KB
/
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const path = require('path');
const slash = require('slash');
const AddAssetHtmlPlugin = require('./src/index');
const { handleUrl } = require('./src/utils');
const testFile = slash(require.resolve('./fixture/some-file'));
const pluginMock = {
plugin: {
addFileToAssets: filename => Promise.resolve(path.basename(filename)),
},
outputName: 'index.html',
};
test('assets should always be an array', () => {
expect(new AddAssetHtmlPlugin({}).assets).toBeInstanceOf(Array);
expect(new AddAssetHtmlPlugin([]).assets).toBeInstanceOf(Array);
expect(new AddAssetHtmlPlugin().assets).toBeInstanceOf(Array);
});
test('assets should should be reversed', () => {
expect(new AddAssetHtmlPlugin(['a', 'b']).assets).toEqual(['b', 'a']);
});
test('should not reject on success', async () => {
const plugin = new AddAssetHtmlPlugin();
expect(await plugin.addAllAssetsToCompilation({}, pluginMock)).toEqual(
pluginMock,
);
});
test('should invoke callback on error', async () => {
const compilation = { errors: [] };
const plugin = new AddAssetHtmlPlugin({});
await expect(
plugin.addAllAssetsToCompilation(compilation, pluginMock),
).rejects.toThrowError();
expect(compilation.errors).toMatchSnapshot();
});
test('should reject on error', async () => {
expect.hasAssertions();
const compilation = { errors: [] };
const plugin = new AddAssetHtmlPlugin({});
try {
await plugin.addAllAssetsToCompilation(compilation, pluginMock);
} catch (e) {
expect(compilation.errors).toMatchSnapshot();
expect(compilation.errors[0]).toBe(e);
}
});
test("should add file using compilation's publicPath", async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('should used passed in publicPath', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: 'my-file.js',
publicPath: 'pp',
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
// TODO: No idea what this does, actually... Coverage currently hits it, but the logic is untested.
test.todo('should handle missing `publicPath`');
test('should add file missing "/" to public path', async () => {
const compilation = { options: { output: { publicPath: 'vendor' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({ filepath: 'my-file.js' });
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('should add sourcemap and gzipped files to compilation', async () => {
const addFileToAssetsStub = jest.fn();
const compilation = { options: { output: {} } };
const pluginData = {
assets: { js: [], css: [] },
plugin: { addFileToAssets: addFileToAssetsStub },
};
addFileToAssetsStub.mockReturnValue(Promise.resolve('my-file.js'));
const plugin = new AddAssetHtmlPlugin({ filepath: testFile });
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
expect(addFileToAssetsStub).toHaveBeenCalledTimes(3);
expect(addFileToAssetsStub.mock.calls[0]).toEqual([testFile, compilation]);
expect(addFileToAssetsStub.mock.calls[1]).toEqual([
`${testFile}.gz`,
compilation,
]);
expect(addFileToAssetsStub.mock.calls[2]).toEqual([
`${testFile}.map`,
compilation,
]);
});
test('should skip adding sourcemap and gzipped files to compilation if set to false', async () => {
const addFileToAssetsStub = jest.fn();
const compilation = { options: { output: {} } };
const pluginData = {
assets: { js: [], css: [] },
plugin: { addFileToAssets: addFileToAssetsStub },
};
addFileToAssetsStub.mockReturnValue(Promise.resolve('my-file.js'));
const plugin = new AddAssetHtmlPlugin({
filepath: 'my-file.js',
includeRelatedFiles: false,
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
expect(addFileToAssetsStub).toHaveBeenCalledTimes(1);
expect(addFileToAssetsStub).toHaveBeenCalledWith('my-file.js', compilation);
});
test('should use the hash from HtmlWebpackPlugin if option is set', async () => {
const compilation = {
hash: 'testHash',
options: { output: {} },
assets: {
'my-file.js': { source: () => 'some source code is cool to have;' },
},
};
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({ filepath: 'my-file.js', hash: true });
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('should include hash of file content if option is set', async () => {
const compilation = {
options: { output: {} },
assets: {
'my-file.js': { source: () => 'some source code is cool to have;' },
},
};
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({ filepath: 'my-file.js', hash: true });
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('should add to css if `typeOfAsset` is css', async () => {
const compilation = {
options: { output: {} },
assets: {
'my-file.js': { source: () => 'some source code is cool to have;' },
},
};
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: 'my-file.css',
typeOfAsset: 'css',
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('should replace compilation assets key if `outputPath` is set', async () => {
const source = { source: () => 'test' };
const addFileToAssetsMock = (filename, compilation) => {
const name = path.basename(filename);
compilation.assets[name] = source; // eslint-disable-line no-param-reassign
return Promise.resolve(name);
};
const compilation = {
options: { output: {} },
assets: {},
};
const pluginData = {
assets: { js: [], css: [] },
plugin: { addFileToAssets: addFileToAssetsMock },
};
const plugin = new AddAssetHtmlPlugin({
filepath: testFile,
outputPath: 'assets',
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
expect(compilation.assets).toEqual({
'assets/some-file.js': source,
'assets/some-file.js.gz': source,
'assets/some-file.js.map': source,
});
});
test('filter option should exclude some files', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
files: ['something-weird'],
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('filter option should include some files', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
files: ['index.*'],
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('filter option should include some files with string option', async () => {
const compilation = { options: { output: { publicPath: 'vendor/' } } };
const pluginData = { assets: { js: [], css: [] }, ...pluginMock };
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
files: 'index.*',
});
await plugin.addAllAssetsToCompilation(compilation, pluginData);
expect(pluginData.assets).toMatchSnapshot();
});
test('filter option should include some files with string option', async () => {
const pluginData = {
scripts: [
{
tagName: 'script',
voidTag: false,
attributes: { src: 'polyfill.js' },
},
{
tagName: 'script',
voidTag: false,
attributes: { src: 'index_bundle.js' },
},
],
styles: [],
meta: [],
};
const plugin = new AddAssetHtmlPlugin({
filepath: path.join(__dirname, 'my-file.js'),
files: 'index.*',
attributes: { nomodule: true },
});
plugin.addedAssets.push('polyfill.js');
await plugin.alterAssetsAttributes(pluginData);
expect(pluginData.scripts).toContainEqual({
tagName: 'script',
voidTag: false,
attributes: { src: 'polyfill.js', nomodule: true },
});
});
test('use globby to find multi file', async () => {
const assets = [{ glob: './src/*.js' }];
const ret = await handleUrl(assets);
expect(ret).toHaveLength(2);
});
test('filepath without globbyMagic should just return', async () => {
const assets = [{ filepath: path.join(__dirname, 'my-file.js') }];
const ret = await handleUrl(assets);
expect(ret).toHaveLength(1);
});
test('throws of both filepath and glob is defined', async () => {
const assets = [{ filepath: 'my-file.js', glob: 'my-file.js' }];
await expect(handleUrl(assets)).rejects.toThrowErrorMatchingSnapshot();
});