|
1 | 1 | import { webpackPlugin } from '../src/karma-webpack';
|
2 | 2 |
|
| 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 | + |
3 | 33 | 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) => { |
9 | 84 | const compilationMock = {
|
10 | 85 | addEntry(name, dep, file, cb) {
|
11 | 86 | cb(new Error('test error'));
|
12 | 87 | },
|
13 | 88 | };
|
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) => { |
29 | 94 | 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 | + |
30 | 114 | done();
|
31 | 115 | });
|
32 | 116 | });
|
|
0 commit comments