Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(plugin): add tests to prevent some regressions #419

Merged
merged 1 commit into from Jun 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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