Skip to content

Commit

Permalink
test(plugin): add tests to prevent some regressions (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Clark committed Jun 10, 2019
1 parent 48878eb commit 3cc35b3
Showing 1 changed file with 104 additions and 20 deletions.
124 changes: 104 additions & 20 deletions test/plugin.test.js
@@ -1,32 +1,116 @@
import { webpackPlugin } from '../src/karma-webpack';

const emitterMock = {
on() {},
};

function initPlugin(
webpackOptions = {},
webpackServerOptions = {},
webpackMiddlewareOptions = {},
basePath = '',
files = [],
frameworks = [],
singleRun = true,
colors = true,
customFileHandlers = [],
emitter = emitterMock
) {
return new webpackPlugin[1](
webpackOptions,
webpackServerOptions,
webpackMiddlewareOptions,
basePath,
files,
frameworks,
singleRun,
colors,
customFileHandlers,
emitter
);
}

describe('Plugin', () => {
describe('#make()', () => {
test('Defaults', (done) => {
const emitterMock = {
on() {},
};
describe('Configuration', () => {
it('should initialize correctly with defaults', () => {
initPlugin();
});

it('should add a default webpack entry value if not provided', () => {
const plugin = initPlugin({});

// this is the only place we can access the complete webpack config
const { options } = plugin.middleware.context.compiler;

expect(typeof options.entry).toEqual('function');
expect(options.entry()).toEqual({});
});

it('should set the output filename correctly', () => {
const plugin = initPlugin({});

// this is the only place we can access the complete webpack config
const { options } = plugin.middleware.context.compiler;

expect(options.output.filename).toEqual('[name].js');
});

it('should set splitChunks and runtimeChunk to false if provided', () => {
const plugin = initPlugin({
optimization: {
splitChunks: true,
runtimeChunk: true,
},
});

// this is the only place we can access the complete webpack config
const { options } = plugin.middleware.context.compiler;

expect(options.optimization.splitChunks).toEqual(false);
expect(options.optimization.runtimeChunk).toEqual(false);
});

it('should initialize correctly without webpackMiddleware configuration', () => {
initPlugin({}, null, null);
});

it('should allow a multi compiler webpack configuration', () => {
initPlugin([{}, {}]);
});
});

describe('Adding Files', () => {
it('should error when adding an entry to the compilation fails', (done) => {
const compilationMock = {
addEntry(name, dep, file, cb) {
cb(new Error('test error'));
},
};
const Plugin = new webpackPlugin[1](
{},
{},
{},
'',
[],
[],
true,
true,
[],
emitterMock
);

Plugin.addFile('test.js');
Plugin.make(compilationMock, (err) => {

const plugin = initPlugin();

plugin.addFile('test.js');
plugin.make(compilationMock, (err) => {
expect(err.message).toBe('test error');

done();
});
});

it('should store files as an entry file when added', (done) => {
const compilationMock = {
addEntry(name, dep, file, cb) {
cb();
},
};

const plugin = initPlugin();

plugin.addFile('test.js');
plugin.make(compilationMock, () => {
expect(plugin.entries.size).toBe(1);
expect(plugin.entries.get('test')).toEqual('test.js');

done();
});
});
Expand Down

0 comments on commit 3cc35b3

Please sign in to comment.