Skip to content

Commit 22abdac

Browse files
committed
Use snapshot assertions
1 parent b5096a4 commit 22abdac

File tree

2 files changed

+118
-34
lines changed

2 files changed

+118
-34
lines changed

__snapshots__/test.js.snap

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`filter option should exclude some files 1`] = `
4+
Object {
5+
"css": Array [],
6+
"js": Array [],
7+
}
8+
`;
9+
10+
exports[`filter option should include some files 1`] = `
11+
Object {
12+
"css": Array [],
13+
"js": Array [
14+
"vendor/my-file.js",
15+
],
16+
}
17+
`;
18+
19+
exports[`should add file missing "/" to public path 1`] = `
20+
Object {
21+
"css": Array [],
22+
"js": Array [
23+
"vendor/my-file.js",
24+
],
25+
}
26+
`;
27+
28+
exports[`should add file using compilation's publicPath 1`] = `
29+
Object {
30+
"css": Array [],
31+
"js": Array [
32+
"vendor/my-file.js",
33+
],
34+
}
35+
`;
36+
37+
exports[`should add sourcemap to compilation 1`] = `
38+
Object {
39+
"css": Array [],
40+
"js": Array [
41+
"my-file.js",
42+
],
43+
}
44+
`;
45+
46+
exports[`should add to css if \`typeOfAsset\` is css 1`] = `
47+
Object {
48+
"css": Array [
49+
"my-file.css",
50+
],
51+
"js": Array [],
52+
}
53+
`;
54+
55+
exports[`should include hash of file content if option is set 1`] = `
56+
Object {
57+
"css": Array [],
58+
"js": Array [
59+
"my-file.js?5329c141291f07ab06c6",
60+
],
61+
}
62+
`;
63+
64+
exports[`should invoke callback on error 1`] = `
65+
Array [
66+
[Error: No filepath defined],
67+
]
68+
`;
69+
70+
exports[`should replace compilation assets key if \`outputPath\` is set 1`] = `
71+
Object {
72+
"css": Array [],
73+
"js": Array [
74+
"my-file.js",
75+
],
76+
}
77+
`;
78+
79+
exports[`should skip adding sourcemap to compilation if set to false 1`] = `
80+
Object {
81+
"css": Array [],
82+
"js": Array [
83+
"my-file.js",
84+
],
85+
}
86+
`;
87+
88+
exports[`should used passed in publicPath 1`] = `
89+
Object {
90+
"css": Array [],
91+
"js": Array [
92+
"pp/my-file.js",
93+
],
94+
}
95+
`;

test.js

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('assets should should be reversed', () => {
2222
expect(new AddAssetHtmlPlugin(['a', 'b']).assets).toEqual(['b', 'a']);
2323
});
2424

25-
test.concurrent('should invoke callback on success', async () => {
25+
test('should invoke callback on success', async () => {
2626
const callback = jest.fn();
2727

2828
await addAllAssetsToCompilation([], {}, pluginMock, callback);
@@ -31,20 +31,19 @@ test.concurrent('should invoke callback on success', async () => {
3131
expect(callback).toHaveBeenCalledWith(null, pluginMock);
3232
});
3333

34-
test.concurrent('should invoke callback on error', async () => {
34+
test('should invoke callback on error', async () => {
3535
const callback = jest.fn();
3636
const compilation = { errors: [] };
3737

3838
await addAllAssetsToCompilation([{}], compilation, pluginMock, callback);
3939

40-
expect(compilation.errors).toHaveLength(1);
41-
expect(compilation.errors[0].message).toBe('No filepath defined');
40+
expect(compilation.errors).toMatchSnapshot();
4241

4342
expect(callback).toHaveBeenCalledTimes(1);
4443
expect(callback).toHaveBeenCalledWith(compilation.errors[0], pluginMock);
4544
});
4645

47-
test.concurrent("should add file using compilation's publicPath", async () => {
46+
test("should add file using compilation's publicPath", async () => {
4847
const callback = jest.fn();
4948
const compilation = { options: { output: { publicPath: 'vendor/' } } };
5049
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
@@ -56,22 +55,20 @@ test.concurrent("should add file using compilation's publicPath", async () => {
5655
callback
5756
);
5857

59-
expect(pluginData.assets.css).toEqual([]);
60-
expect(pluginData.assets.js).toEqual(['vendor/my-file.js']);
58+
expect(pluginData.assets).toMatchSnapshot();
6159

6260
expect(callback).toHaveBeenCalledTimes(1);
6361
expect(callback).toHaveBeenCalledWith(null, pluginData);
6462
});
6563

66-
test.concurrent('should used passed in publicPath', async () => {
64+
test('should used passed in publicPath', async () => {
6765
const callback = jest.fn();
6866
const compilation = { options: { output: { publicPath: 'vendor/' } } };
6967
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
7068

7169
await addAllAssetsToCompilation([{ filepath: 'my-file.js', publicPath: 'pp' }], compilation, pluginData, callback);
7270

73-
expect(pluginData.assets.css).toEqual([]);
74-
expect(pluginData.assets.js).toEqual(['pp/my-file.js']);
71+
expect(pluginData.assets).toMatchSnapshot();
7572

7673
expect(callback).toHaveBeenCalledTimes(1);
7774
expect(callback).toHaveBeenCalledWith(null, pluginData);
@@ -80,21 +77,20 @@ test.concurrent('should used passed in publicPath', async () => {
8077
// TODO: No idea what this does, actually... Coverage currently hits it, but the logic is untested.
8178
test('should handle missing `publicPath`');
8279

83-
test.concurrent('should add file missing "/" to public path', async () => {
80+
test('should add file missing "/" to public path', async () => {
8481
const callback = jest.fn();
8582
const compilation = { options: { output: { publicPath: 'vendor' } } };
8683
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
8784

8885
await addAllAssetsToCompilation([{ filepath: 'my-file.js' }], compilation, pluginData, callback);
8986

90-
expect(pluginData.assets.css).toEqual([]);
91-
expect(pluginData.assets.js).toEqual(['vendor/my-file.js']);
87+
expect(pluginData.assets).toMatchSnapshot();
9288

9389
expect(callback).toHaveBeenCalledTimes(1);
9490
expect(callback).toHaveBeenCalledWith(null, pluginData);
9591
});
9692

97-
test.concurrent('should add sourcemap to compilation', async () => {
93+
test('should add sourcemap to compilation', async () => {
9894
const callback = jest.fn();
9995
const addFileToAssetsStub = jest.fn();
10096
const compilation = { options: { output: {} } };
@@ -103,8 +99,7 @@ test.concurrent('should add sourcemap to compilation', async () => {
10399

104100
await addAllAssetsToCompilation([{ filepath: 'my-file.js' }], compilation, pluginData, callback);
105101

106-
expect(pluginData.assets.css).toEqual([]);
107-
expect(pluginData.assets.js).toEqual(['my-file.js']);
102+
expect(pluginData.assets).toMatchSnapshot();
108103

109104
expect(callback).toHaveBeenCalledTimes(1);
110105
expect(callback).toHaveBeenCalledWith(null, pluginData);
@@ -114,7 +109,7 @@ test.concurrent('should add sourcemap to compilation', async () => {
114109
expect(addFileToAssetsStub.mock.calls[1]).toEqual(['my-file.js.map', compilation]);
115110
});
116111

117-
test.concurrent('should skip adding sourcemap to compilation if set to false', async () => {
112+
test('should skip adding sourcemap to compilation if set to false', async () => {
118113
const callback = jest.fn();
119114
const addFileToAssetsStub = jest.fn();
120115
const compilation = { options: { output: {} } };
@@ -128,8 +123,7 @@ test.concurrent('should skip adding sourcemap to compilation if set to false', a
128123
callback
129124
);
130125

131-
expect(pluginData.assets.css).toEqual([]);
132-
expect(pluginData.assets.js).toEqual(['my-file.js']);
126+
expect(pluginData.assets).toMatchSnapshot();
133127

134128
expect(callback).toHaveBeenCalledTimes(1);
135129
expect(callback).toHaveBeenCalledWith(null, pluginData);
@@ -138,7 +132,7 @@ test.concurrent('should skip adding sourcemap to compilation if set to false', a
138132
expect(addFileToAssetsStub).toHaveBeenCalledWith('my-file.js', compilation);
139133
});
140134

141-
test.concurrent('should include hash of file content if option is set', async () => {
135+
test('should include hash of file content if option is set', async () => {
142136
const callback = jest.fn();
143137
const compilation = {
144138
options: { output: {} },
@@ -148,14 +142,13 @@ test.concurrent('should include hash of file content if option is set', async ()
148142

149143
await addAllAssetsToCompilation([{ filepath: 'my-file.js', hash: true }], compilation, pluginData, callback);
150144

151-
expect(pluginData.assets.css).toEqual([]);
152-
expect(pluginData.assets.js).toEqual(['my-file.js?5329c141291f07ab06c6']);
145+
expect(pluginData.assets).toMatchSnapshot();
153146

154147
expect(callback).toHaveBeenCalledTimes(1);
155148
expect(callback).toHaveBeenCalledWith(null, pluginData);
156149
});
157150

158-
test.concurrent('should add to css if `typeOfAsset` is css', async () => {
151+
test('should add to css if `typeOfAsset` is css', async () => {
159152
const callback = jest.fn();
160153
const compilation = {
161154
options: { output: {} },
@@ -165,14 +158,13 @@ test.concurrent('should add to css if `typeOfAsset` is css', async () => {
165158

166159
await addAllAssetsToCompilation([{ filepath: 'my-file.css', typeOfAsset: 'css' }], compilation, pluginData, callback);
167160

168-
expect(pluginData.assets.css).toEqual(['my-file.css']);
169-
expect(pluginData.assets.js).toEqual([]);
161+
expect(pluginData.assets).toMatchSnapshot();
170162

171163
expect(callback).toHaveBeenCalledTimes(1);
172164
expect(callback).toHaveBeenCalledWith(null, pluginData);
173165
});
174166

175-
test.concurrent('should replace compilation assets key if `outputPath` is set', async () => {
167+
test('should replace compilation assets key if `outputPath` is set', async () => {
176168
const callback = jest.fn();
177169
const source = { source: () => 'test' };
178170
const addFileToAssetsMock = (filename, compilation) => {
@@ -193,16 +185,15 @@ test.concurrent('should replace compilation assets key if `outputPath` is set',
193185
callback
194186
);
195187

196-
expect(pluginData.assets.css).toEqual([]);
197-
expect(pluginData.assets.js).toEqual(['my-file.js']);
188+
expect(pluginData.assets).toMatchSnapshot();
198189

199190
expect(compilation.assets['my-file.js']).toBeUndefined();
200191
expect(compilation.assets['assets/my-file.js']).toEqual(source);
201192
expect(compilation.assets['my-file.js.map']).toBeUndefined();
202193
expect(compilation.assets['assets/my-file.js.map']).toEqual(source);
203194
});
204195

205-
test.concurrent('filter option should exclude some files', async () => {
196+
test('filter option should exclude some files', async () => {
206197
const callback = jest.fn();
207198
const compilation = { options: { output: { publicPath: 'vendor/' } } };
208199
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
@@ -214,14 +205,13 @@ test.concurrent('filter option should exclude some files', async () => {
214205
callback
215206
);
216207

217-
expect(pluginData.assets.css).toEqual([]);
218-
expect(pluginData.assets.js).toEqual([]);
208+
expect(pluginData.assets).toMatchSnapshot();
219209

220210
expect(callback).toHaveBeenCalledTimes(1);
221211
expect(callback).toHaveBeenCalledWith(null, pluginData);
222212
});
223213

224-
test.concurrent('filter option should include some files', async () => {
214+
test('filter option should include some files', async () => {
225215
const callback = jest.fn();
226216
const compilation = { options: { output: { publicPath: 'vendor/' } } };
227217
const pluginData = Object.assign({ assets: { js: [], css: [] } }, pluginMock);
@@ -233,8 +223,7 @@ test.concurrent('filter option should include some files', async () => {
233223
callback
234224
);
235225

236-
expect(pluginData.assets.css).toEqual([]);
237-
expect(pluginData.assets.js).toEqual(['vendor/my-file.js']);
226+
expect(pluginData.assets).toMatchSnapshot();
238227

239228
expect(callback).toHaveBeenCalledTimes(1);
240229
expect(callback).toHaveBeenCalledWith(null, pluginData);

0 commit comments

Comments
 (0)