From 2a3458cefa7f16c4ed856ffa65a44afdf64ef4e0 Mon Sep 17 00:00:00 2001 From: ccpricenytimes Date: Tue, 23 Aug 2016 15:50:36 -0400 Subject: [PATCH] Run Tests with Ava configuration instead of babel precompile (#66) * 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 --- cli/actions/test.js | 41 +++++++++++++---------------------------- config/ava.package.json | 21 +++++++++++++++++++++ config/webpack.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 28 deletions(-) create mode 100644 config/ava.package.json diff --git a/cli/actions/test.js b/cli/actions/test.js index 0a79206b2..9ed5d2b82 100644 --- a/cli/actions/test.js +++ b/cli/actions/test.js @@ -6,30 +6,25 @@ 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'); } @@ -37,34 +32,24 @@ module.exports = () => { 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); }; diff --git a/config/ava.package.json b/config/ava.package.json new file mode 100644 index 000000000..e7d3a7219 --- /dev/null +++ b/config/ava.package.json @@ -0,0 +1,21 @@ +{ + "ava": { + "babel": "inherit", + "require": ["babel-register"] + }, + "babel": { + "presets": [ + "es2015", + "react" + ], + "plugins": [ + [ + "babel-plugin-webpack-loaders", + { + "config": "${CONFIG}", + "verbose": false + } + ] + ] + } +} \ No newline at end of file diff --git a/config/webpack.test.js b/config/webpack.test.js index 7abb3836d..1856a6890 100644 --- a/config/webpack.test.js +++ b/config/webpack.test.js @@ -3,6 +3,7 @@ const clone = require('ramda').clone; const kytConfig = require('./kyt.config'); +const path = require('path'); const logger = console; const cssStyleLoaders = [ @@ -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: [ { @@ -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;