Skip to content

Commit

Permalink
Merge branch 'master' into stdin-option
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jul 7, 2019
2 parents 5c20c65 + 461b876 commit 245e9cf
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/multi-compiler-2-config/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log('Hey.');
22 changes: 22 additions & 0 deletions test/fixtures/multi-compiler-2-config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

module.exports = [
{
mode: 'development',
context: __dirname,
entry: './foo.js',
output: {
path: '/',
},
node: false,
},
{
mode: 'development',
context: __dirname,
entry: './foo.js',
output: {
path: '/',
},
node: false,
},
];
47 changes: 47 additions & 0 deletions test/server/utils/__snapshots__/updateCompiler.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`updateCompiler multi compiler config, hot and inline should apply plugins 1`] = `
Array [
HotModuleReplacementPlugin {
"fullBuildTimeout": 200,
"multiStep": undefined,
"options": Object {},
"requestTimeout": 10000,
},
]
`;

exports[`updateCompiler multi compiler config, hot and inline should apply plugins 2`] = `
Array [
HotModuleReplacementPlugin {
"fullBuildTimeout": 200,
"multiStep": undefined,
"options": Object {},
"requestTimeout": 10000,
},
]
`;

exports[`updateCompiler simple config with HMR already, hot and inline should apply plugins 1`] = `
Array [
HotModuleReplacementPlugin {
"fullBuildTimeout": 200,
"multiStep": undefined,
"options": Object {},
"requestTimeout": 10000,
},
]
`;

exports[`updateCompiler simple config, hot and inline should apply plugins 1`] = `
Array [
HotModuleReplacementPlugin {
"fullBuildTimeout": 200,
"multiStep": undefined,
"options": Object {},
"requestTimeout": 10000,
},
]
`;

exports[`updateCompiler simple config, inline should apply plugins without HMR 1`] = `undefined`;
162 changes: 162 additions & 0 deletions test/server/utils/updateCompiler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
'use strict';

const webpack = require('webpack');
const updateCompiler = require('../../../lib/utils/updateCompiler');

describe('updateCompiler', () => {
describe('simple config, inline', () => {
let compiler;
beforeAll(() => {
// eslint-disable-next-line global-require
const webpackConfig = require('../../fixtures/simple-config/webpack.config');
compiler = webpack(webpackConfig);
});

it('should apply plugins without HMR', () => {
const spy = jest.spyOn(compiler.hooks.entryOption, 'call');

updateCompiler(compiler, {
serverMode: 'sockjs',
clientMode: 'sockjs',
inline: true,
});

let tapsByHMR = 0;
let tapsByProvidePlugin = 0;
compiler.hooks.compilation.taps.forEach((tap) => {
if (tap.name === 'HotModuleReplacementPlugin') {
tapsByHMR += 1;
} else if (tap.name === 'ProvidePlugin') {
tapsByProvidePlugin += 1;
}
});

expect(spy).toHaveBeenCalledTimes(1);
expect(tapsByHMR).toEqual(0);
expect(tapsByProvidePlugin).toEqual(1);
// should be empty
expect(compiler.options.plugins).toMatchSnapshot();

spy.mockRestore();
});
});

describe('simple config, hot and inline', () => {
let compiler;
beforeAll(() => {
// eslint-disable-next-line global-require
const webpackConfig = require('../../fixtures/simple-config/webpack.config');
compiler = webpack(webpackConfig);
});

it('should apply plugins', () => {
const spy = jest.spyOn(compiler.hooks.entryOption, 'call');

updateCompiler(compiler, {
serverMode: 'sockjs',
clientMode: 'sockjs',
inline: true,
hot: true,
});

let tapsByHMR = 0;
let tapsByProvidePlugin = 0;
compiler.hooks.compilation.taps.forEach((tap) => {
if (tap.name === 'HotModuleReplacementPlugin') {
tapsByHMR += 1;
} else if (tap.name === 'ProvidePlugin') {
tapsByProvidePlugin += 1;
}
});

expect(spy).toHaveBeenCalledTimes(1);
expect(tapsByHMR).toEqual(1);
expect(tapsByProvidePlugin).toEqual(1);
expect(compiler.options.plugins).toMatchSnapshot();

spy.mockRestore();
});
});

describe('simple config with HMR already, hot and inline', () => {
let compiler;
beforeAll(() => {
// eslint-disable-next-line global-require
const webpackConfig = require('../../fixtures/simple-config/webpack.config');
webpackConfig.plugins = [new webpack.HotModuleReplacementPlugin()];
compiler = webpack(webpackConfig);
});

it('should apply plugins', () => {
const spy = jest.spyOn(compiler.hooks.entryOption, 'call');

updateCompiler(compiler, {
serverMode: 'sockjs',
clientMode: 'sockjs',
inline: true,
hot: true,
});

let tapsByHMR = 0;
let tapsByProvidePlugin = 0;
compiler.hooks.compilation.taps.forEach((tap) => {
if (tap.name === 'HotModuleReplacementPlugin') {
tapsByHMR += 1;
} else if (tap.name === 'ProvidePlugin') {
tapsByProvidePlugin += 1;
}
});

expect(spy).toHaveBeenCalledTimes(1);
expect(tapsByHMR).toEqual(1);
expect(tapsByProvidePlugin).toEqual(1);
expect(compiler.options.plugins).toMatchSnapshot();

spy.mockRestore();
});
});

describe('multi compiler config, hot and inline', () => {
let multiCompiler;
beforeAll(() => {
// eslint-disable-next-line global-require
const webpackConfig = require('../../fixtures/multi-compiler-2-config/webpack.config');
webpackConfig[1].plugins = [new webpack.HotModuleReplacementPlugin()];
multiCompiler = webpack(webpackConfig);
});

it('should apply plugins', () => {
const spies = [];
multiCompiler.compilers.forEach((compiler) => {
spies.push(jest.spyOn(compiler.hooks.entryOption, 'call'));
});

updateCompiler(multiCompiler, {
serverMode: 'sockjs',
clientMode: 'sockjs',
inline: true,
hot: true,
});

spies.forEach((spy) => {
expect(spy).toHaveBeenCalledTimes(1);
spy.mockRestore();
});

multiCompiler.compilers.forEach((compiler) => {
let tapsByHMR = 0;
let tapsByProvidePlugin = 0;
compiler.hooks.compilation.taps.forEach((tap) => {
if (tap.name === 'HotModuleReplacementPlugin') {
tapsByHMR += 1;
} else if (tap.name === 'ProvidePlugin') {
tapsByProvidePlugin += 1;
}
});
expect(tapsByHMR).toEqual(1);
expect(tapsByProvidePlugin).toEqual(1);
expect(compiler.options.plugins).toMatchSnapshot();
});
});
});
});

0 comments on commit 245e9cf

Please sign in to comment.