Skip to content

Commit 3cc35b3

Browse files
author
Ryan Clark
authored
test(plugin): add tests to prevent some regressions (#419)
1 parent 48878eb commit 3cc35b3

File tree

1 file changed

+104
-20
lines changed

1 file changed

+104
-20
lines changed

test/plugin.test.js

+104-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,116 @@
11
import { webpackPlugin } from '../src/karma-webpack';
22

3+
const emitterMock = {
4+
on() {},
5+
};
6+
7+
function initPlugin(
8+
webpackOptions = {},
9+
webpackServerOptions = {},
10+
webpackMiddlewareOptions = {},
11+
basePath = '',
12+
files = [],
13+
frameworks = [],
14+
singleRun = true,
15+
colors = true,
16+
customFileHandlers = [],
17+
emitter = emitterMock
18+
) {
19+
return new webpackPlugin[1](
20+
webpackOptions,
21+
webpackServerOptions,
22+
webpackMiddlewareOptions,
23+
basePath,
24+
files,
25+
frameworks,
26+
singleRun,
27+
colors,
28+
customFileHandlers,
29+
emitter
30+
);
31+
}
32+
333
describe('Plugin', () => {
4-
describe('#make()', () => {
5-
test('Defaults', (done) => {
6-
const emitterMock = {
7-
on() {},
8-
};
34+
describe('Configuration', () => {
35+
it('should initialize correctly with defaults', () => {
36+
initPlugin();
37+
});
38+
39+
it('should add a default webpack entry value if not provided', () => {
40+
const plugin = initPlugin({});
41+
42+
// this is the only place we can access the complete webpack config
43+
const { options } = plugin.middleware.context.compiler;
44+
45+
expect(typeof options.entry).toEqual('function');
46+
expect(options.entry()).toEqual({});
47+
});
48+
49+
it('should set the output filename correctly', () => {
50+
const plugin = initPlugin({});
51+
52+
// this is the only place we can access the complete webpack config
53+
const { options } = plugin.middleware.context.compiler;
54+
55+
expect(options.output.filename).toEqual('[name].js');
56+
});
57+
58+
it('should set splitChunks and runtimeChunk to false if provided', () => {
59+
const plugin = initPlugin({
60+
optimization: {
61+
splitChunks: true,
62+
runtimeChunk: true,
63+
},
64+
});
65+
66+
// this is the only place we can access the complete webpack config
67+
const { options } = plugin.middleware.context.compiler;
68+
69+
expect(options.optimization.splitChunks).toEqual(false);
70+
expect(options.optimization.runtimeChunk).toEqual(false);
71+
});
72+
73+
it('should initialize correctly without webpackMiddleware configuration', () => {
74+
initPlugin({}, null, null);
75+
});
76+
77+
it('should allow a multi compiler webpack configuration', () => {
78+
initPlugin([{}, {}]);
79+
});
80+
});
81+
82+
describe('Adding Files', () => {
83+
it('should error when adding an entry to the compilation fails', (done) => {
984
const compilationMock = {
1085
addEntry(name, dep, file, cb) {
1186
cb(new Error('test error'));
1287
},
1388
};
14-
const Plugin = new webpackPlugin[1](
15-
{},
16-
{},
17-
{},
18-
'',
19-
[],
20-
[],
21-
true,
22-
true,
23-
[],
24-
emitterMock
25-
);
26-
27-
Plugin.addFile('test.js');
28-
Plugin.make(compilationMock, (err) => {
89+
90+
const plugin = initPlugin();
91+
92+
plugin.addFile('test.js');
93+
plugin.make(compilationMock, (err) => {
2994
expect(err.message).toBe('test error');
95+
96+
done();
97+
});
98+
});
99+
100+
it('should store files as an entry file when added', (done) => {
101+
const compilationMock = {
102+
addEntry(name, dep, file, cb) {
103+
cb();
104+
},
105+
};
106+
107+
const plugin = initPlugin();
108+
109+
plugin.addFile('test.js');
110+
plugin.make(compilationMock, () => {
111+
expect(plugin.entries.size).toBe(1);
112+
expect(plugin.entries.get('test')).toEqual('test.js');
113+
30114
done();
31115
});
32116
});

0 commit comments

Comments
 (0)