Skip to content

Commit

Permalink
Run Tests with Ava configuration instead of babel precompile (#66)
Browse files Browse the repository at this point in the history
* First pass. not working

* working now but unbearably slow

* updating test webpack

* removing unused function

* adding webpack via babel register rather than precompile

* Committing now, doesnt work

* adding ava package json

* Cleanup
  • Loading branch information
ccpricenytimes authored and delambo committed Aug 23, 2016
1 parent e31ff0a commit 2a3458c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 28 deletions.
41 changes: 13 additions & 28 deletions cli/actions/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,50 @@ const logger = require('./../logger');
const shell = require('shelljs');
const kytConfig = require('./../../config/kyt.config');


module.exports = () => {
// Comment the following to see verbose shell ouput.
shell.config.silent = true;

const userRootPath = kytConfig.userRootPath;
const userSrc = path.join(userRootPath, 'src');
const userBuild = path.join(userRootPath, 'build/test');
const avaCLI = path.resolve(userRootPath, './node_modules/ava/cli.js');
const npath = path.resolve(userRootPath, './node_modules');
const babel = path.join(npath, '.bin/babel');
const es2015Preset = require.resolve('babel-preset-es2015');
const reactPreset = require.resolve('babel-preset-react');
const presets = `${es2015Preset},${reactPreset}`;
const babelWebpack = require.resolve('babel-plugin-webpack-loaders');
const plugins = babelWebpack;
const testConfigPath = path.resolve(__dirname, '../../config/webpack.temp.test.js');
const tempTestDir = path.join(userRootPath, './tmp-test');
const tempTestDir = path.join(userRootPath, './kyt-test');
const newConfigPath = path.join(tempTestDir, './webpack.config.js');

const avaPkgJsonPath = path.join(__dirname, '../../config/ava.package.json');
const testPkgJsonPath = path.join(tempTestDir, './package.json');
logger.start('Running Test Command...');

// Clean the build directory.
if (shell.test('-d', userBuild)) {
shell.rm('-rf', userBuild);
if (shell.test('-d', tempTestDir)) {
shell.rm('-rf', tempTestDir);
logger.task('Cleaned test folder');
}

// Create Temp Directory and move user src files there
shell.mkdir(tempTestDir);
shell.cp('-r', userSrc, tempTestDir);

// Copy ava's configuration into the root
shell.cp(avaPkgJsonPath, testPkgJsonPath);

// Copy the webpack config into the temp directory
shell.cp(testConfigPath, newConfigPath);

// Compile Code and move it into the user's root directory
shell.cd(tempTestDir);
const babelCommand = `NODE_PATH=$NODE_PATH:${npath} BABEL_DISABLE_CACHE=1 ` +
`${babel} ${tempTestDir} --ignore node_modules --presets ${presets} ` +
`--plugins ${plugins} --out-dir ${userBuild} -s inline`;

let babelExec = shell.exec(babelCommand);
if (babelExec.code !== 0) {
logger.error('Error transpiling test code', babelExec.stderr);
process.exit();
}

// Remove tmp directory
shell.rm('-rf', tempTestDir);
logger.task('Test files prepared by Babel');
// Move back to user's root directory
shell.cd(userRootPath);

// Execute the ava cli on our build.
// We add our node_modules tothe NODE_PATH so that ava can be resolved.
let command = `NODE_PATH=$NODE_PATH:${npath} ` +
`node ${avaCLI} ${userRootPath}/build/test/**/*.test.js`;
let command = `NODE_PATH=$NODE_PATH:${npath} CONFIG=${newConfigPath} BABEL_DISABLE_CACHE=1 ` +
`node ${avaCLI} ${userRootPath}/kyt-test/**/*.test.js`;

if (kytConfig.debug) command += ' --verbose';

shell.config.silent = false;
shell.exec(command);
shell.cd(userRootPath);
shell.rm('-rf', tempTestDir);
};
21 changes: 21 additions & 0 deletions config/ava.package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"ava": {
"babel": "inherit",
"require": ["babel-register"]
},
"babel": {
"presets": [
"es2015",
"react"
],
"plugins": [
[
"babel-plugin-webpack-loaders",
{
"config": "${CONFIG}",
"verbose": false
}
]
]
}
}
38 changes: 38 additions & 0 deletions config/webpack.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const clone = require('ramda').clone;
const kytConfig = require('./kyt.config');
const path = require('path');

const logger = console;
const cssStyleLoaders = [
Expand All @@ -15,11 +16,23 @@ const cssStyleLoaders = [
];

const sassStyleLoaders = clone(cssStyleLoaders).concat('sass');
const userRootPath = kytConfig.userRootPath;
const options = {
environment: 'test',
type: 'test',
userRootPath,
};

const babelrc = {};
babelrc.babelrc = false;
babelrc.presets = [];
babelrc.plugins = [];

const testConfig = {
resolve: {
},
plugins: [],
postcss: [],
module: {
loaders: [
{
Expand All @@ -30,10 +43,35 @@ const testConfig = {
test: /\.scss$/,
loaders: sassStyleLoaders,
},
{
test: /\.html$/,
loader: 'file?name=[name].[ext]',
},
{
test: /\.(jpg|jpeg|png|gif|eot|svg|ttf|woff|woff2)$/,
loader: 'url-loader',
query: {
limit: 20000,
},
},
{
test: /\.json$/,
loader: 'json-loader',
},
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: [
/node_modules/,
path.join(options.userRootPath, 'build'),
],
query: babelrc,
},
],
},
};


module.exports = () => {
// Uses kytConfig callback to merge with user webpack config
let webpackConfig = null;
Expand Down

0 comments on commit 2a3458c

Please sign in to comment.