Skip to content

Commit

Permalink
Add tests for webpack config files
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKVal committed May 19, 2015
1 parent 2bedb70 commit 98eb4c2
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 1 deletion.
1 change: 1 addition & 0 deletions babelhook.js
@@ -0,0 +1 @@
require('babel/register');
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"build": "babel-node tools/build-cli.js",
"test-watch": "karma start",
"test": "npm run lint && npm run build && karma start --single-run",
"test": "npm run lint && npm run webpack-check && npm run build && karma start --single-run",
"webpack-check": "mocha --require babelhook webpack/tests",
"lint": "eslint ./",
"docs-build": "babel-node tools/build-cli.js --docs-only",
"docs": "docs/dev-run",
Expand Down
47 changes: 47 additions & 0 deletions webpack/tests/buildConfSpec.js
@@ -0,0 +1,47 @@
export default {
configFile: 'webpack.config.js',
tests: [
// npm run build | `webpack --bail`
{
args: '',
expected: {
entry: { 'react-bootstrap': './src/index.js' },
output:
{ path: './dist',
filename: '[name].js',
library: 'ReactBootstrap',
libraryTarget: 'umd' },
externals:
[ { react:
{ root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react' } } ],
module: { loaders: [ { test: /\.js/, loader: 'babel', exclude: /node_modules/ } ] },
plugins: [ { definitions: { 'process.env': { NODE_ENV: '"development"' } } } ]
}
},

// npm run build | `webpack --bail -p`
{
args: 'p',
expected: {
entry: { 'react-bootstrap': './src/index.js' },
output:
{ path: './dist',
filename: '[name].min.js',
library: 'ReactBootstrap',
libraryTarget: 'umd' },
externals:
[ { react:
{ root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react' } } ],
module: { loaders: [ { test: /\.js/, loader: 'babel', exclude: /node_modules/ } ] },
plugins: [ { definitions: { 'process.env': { NODE_ENV: '"production"' } } } ],
devtool: 'source-map'
}
}
]
};
112 changes: 112 additions & 0 deletions webpack/tests/docsConfSpec.js
@@ -0,0 +1,112 @@
import path from 'path';
const root = path.join(__dirname, '../../');

export default {
configFile: 'webpack.docs.js',
tests: [

// npm run docs
{
args: 'd',
expected: {
entry:
{ bundle:
[ 'webpack/hot/dev-server',
'webpack-dev-server/client?http://localhost:8080',
'./docs/client.js' ] },
output:
{ filename: '[name].js',
path: './docs-built/assets',
publicPath: 'http://localhost:8080/assets/' },
externals: undefined,
module:
{ noParse: /babel-core\/browser/,
loaders:
[ { test: /\.js/,
loader: 'react-hot!babel',
exclude: /node_modules|Samples\.js/ },
{ test: /Samples.js/, loader: 'react-hot!transform?brfs!babel' },
{ test: /\.css/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css?sourceMap` },
{ test: /\.less$/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css?sourceMap!less?sourceMap` },
{ test: /\.json$/, loader: 'json' },
{ test: /\.jpe?g$|\.gif$|\.png|\.ico$/,
loader: 'file?name=[name].[ext]' },
{ test: /\.eot$|\.ttf$|\.svg$|\.woff2?$/,
loader: 'file?name=[name].[ext]' } ] },
plugins:
[ { definitions: { 'process.env': { NODE_ENV: '"development"' } } },
{ filename: '[name].css', options: {}, id: 1 },
{} ]
}
},

// npm run build
// npm run docs-build
// npm run docs-prod
{
args: 'p',
expected: {
entry: { bundle: './docs/client.js' },
output:
{ filename: '[name].js',
path: './docs-built/assets',
publicPath: '/assets/' },
externals: undefined,
module:
{ noParse: /babel-core\/browser/,
loaders:
[ { test: /\.js/,
loader: 'babel',
exclude: /node_modules|Samples\.js/ },
{ test: /Samples.js/, loader: 'transform?brfs!babel' },
{ test: /\.css/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css` },
{ test: /\.less$/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css!less` },
{ test: /\.json$/, loader: 'json' },
{ test: /\.jpe?g$|\.gif$|\.png|\.ico$/,
loader: 'file?name=[name].[ext]' },
{ test: /\.eot$|\.ttf$|\.svg$|\.woff2?$/,
loader: 'file?name=[name].[ext]' } ] },
plugins:
[ { definitions: { 'process.env': { NODE_ENV: '"production"' } } },
{ filename: '[name].css', options: {}, id: 1 } ],
devtool: 'source-map'
}
},

// for extra testing
{
args: '',
expected: {
entry: { bundle: './docs/client.js' },
output:
{ filename: '[name].js',
path: './docs-built/assets',
publicPath: '/assets/' },
externals: undefined,
module:
{ noParse: /babel-core\/browser/,
loaders:
[ { test: /\.js/,
loader: 'babel',
exclude: /node_modules|Samples\.js/ },
{ test: /Samples.js/, loader: 'transform?brfs!babel' },
{ test: /\.css/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css` },
{ test: /\.less$/,
loader: `${root}node_modules/extract-text-webpack-plugin/loader.js?{"omit":1,"extract":true,"remove":true}!style!css!less` },
{ test: /\.json$/, loader: 'json' },
{ test: /\.jpe?g$|\.gif$|\.png|\.ico$/,
loader: 'file?name=[name].[ext]' },
{ test: /\.eot$|\.ttf$|\.svg$|\.woff2?$/,
loader: 'file?name=[name].[ext]' } ] },
plugins:
[ { definitions: { 'process.env': { NODE_ENV: '"development"' } } },
{ filename: '[name].css', options: {}, id: 1 } ]
}
}
]
};
37 changes: 37 additions & 0 deletions webpack/tests/index.js
@@ -0,0 +1,37 @@
/* eslint-env mocha */

import path from 'path';
import { inspect } from 'util';
import { spawn } from 'child-process-promise';

import buildConfCases from './buildConfSpec.js';
import testConfCases from './testConfSpec.js';
import docsConfCases from './docsConfSpec.js';

import { should } from 'chai';
should();

const webpackMock = path.join(__dirname, 'webpack-mock');

function runTests({ configFile, tests }) {
describe('Webpack config', () => {
tests.forEach((test) => {

it(`should be equal to expected for "${configFile} ${test.args}"`, (done) => {
spawn(webpackMock, [configFile, test.args], {capture: ['stdout', 'stderr']})
.then(({ stdout }) => {
inspect(test.expected, {depth: null}).should.equal(stdout.trim());
done();
})
.fail(err => {
done(err);
});
});

});
});
}

runTests(buildConfCases);
runTests(testConfCases);
runTests(docsConfCases);
17 changes: 17 additions & 0 deletions webpack/tests/testConfSpec.js
@@ -0,0 +1,17 @@
export default {
configFile: 'webpack/test.config.js',
tests: [
// npm run test
{
args: '',
expected: {
entry: undefined,
output: { pathinfo: true },
externals: undefined,
module: { loaders: [ { test: /\.js/, loader: 'babel', exclude: /node_modules/ } ] },
plugins: [ { definitions: { 'process.env': { NODE_ENV: '"development"' } } } ],
devtool: 'eval'
}
}
]
};
35 changes: 35 additions & 0 deletions webpack/tests/webpack-mock
@@ -0,0 +1,35 @@
#!/usr/bin/env babel-node
/* eslint no-process-exit: 0 */
import { inspect } from 'util';
import path from 'path';

/**
* -p and --debug cannot be used by itself,
* because they are legitimate options for `babel-node`
*/

if (!process.argv[2]) {
console.log('webpack-mock path_to_config <p, d>');
process.exit(0);
}

const pathOption = process.argv[2];

let argv;
switch (process.argv[3]) {
case 'p':
argv = ['-p'];
break;
case 'd':
argv = ['--debug'];
break;
default:
argv = [];
}

/* clean args for sure */
process.argv = process.argv.slice(0, 2).concat(argv);

const confFile = path.join(__dirname, '../../', pathOption);
const configObject = require(confFile);
console.log(inspect(configObject, { depth: null }));

0 comments on commit 98eb4c2

Please sign in to comment.