Skip to content

Commit

Permalink
Add simple app unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jvanbruegge committed Oct 7, 2018
1 parent b63ba93 commit 4350309
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 150 deletions.
38 changes: 36 additions & 2 deletions configs/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const nodeExternals = require('webpack-node-externals');

const path = require('path');
const fs = require('fs');

const appPath = (...names) => path.join(process.cwd(), ...names);
const userConfig = require(appPath('webpack.config.js'));
const packageJson = require(appPath('package.json'));

module.exports = webpackMerge(
createConfig([
setMode(process.env.NODE_ENV || 'development'),
setMode(
process.env.NODE_ENV === 'production' ? 'production' : 'development'
),
typescript({
useCache: true,
cacheDirectory: 'node_modules/.cache/at-loader'
Expand Down Expand Up @@ -105,5 +109,35 @@ module.exports = webpackMerge(
])
])
]),
userConfig
userConfig,
createConfig([
env('test', [
customConfig({
target: 'node',
externals: [nodeExternals()],
output: {
// use absolute paths in sourcemaps (important for debugging via IDE)
devtoolModuleFilenameTemplate: '[absolute-resource-path]',
devtoolFallbackModuleFilenameTemplate:
'[absolute-resource-path]?[hash]'
},
module: {
rules: [
{
test: /\.(jsx?|tsx?)/,
include: packageJson.nyc.include.map(p =>
path.resolve(appPath(p))
),
use: {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true }
},
enforce: 'post'
}
]
}
}),
sourceMaps('inline-cheap-module-source-map')
])
])
);
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
"html-webpack-harddisk-plugin": "^0.2.0",
"html-webpack-plugin": "^3.2.0",
"husky": "^1.1.1",
"istanbul-instrumenter-loader": "^3.0.1",
"lint-staged": "^7.3.0",
"mocha": "^5.2.0",
"mocha-webpack": "^2.0.0-beta.0",
"node-sass": "^4.9.3",
"nyc": "^13.0.1",
"prettier": "^1.14.3",
Expand All @@ -77,13 +79,14 @@
"@cycle/history": "^6.10.0",
"@cycle/isolate": "^3.4.0",
"@cycle/run": "^4.4.0",
"@cycle/time": "^0.15.0",
"@types/mocha": "^5.2.5",
"cycle-onionify": "^6.1.0",
"cyclejs-test-helpers": "^2.0.0",
"cyclejs-utils": "^3.2.0",
"cyclic-router": "^5.1.7",
"release-it": "^7.6.1",
"snabbdom-looks-like": "^1.0.1",
"snabbdom-looks-like": "^1.0.4",
"snabbdom-pragma": "^2.7.0",
"switch-path": "^1.2.0",
"xstream": "^11.7.0"
Expand Down
10 changes: 7 additions & 3 deletions scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ module.exports = function init(appPath, appName, verboseOpts) {
}
};
appPackage.nyc = {
instrument: true,
sourceMap: true,
instrument: false,
sourceMap: false,
include: ['src/components'],
reporter: ['html', 'text-summary']
reporter: ['html', 'text']
};

appPackage['mocha-webpack'] = {
include: ['test/**/*.test.{js,jsx,ts,tsx}']
};

const basicDependencies = Object.keys(ownPackage.devDependencies)
Expand Down
15 changes: 9 additions & 6 deletions scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ const packageJson = require(path.join(process.cwd(), 'package.json'));
let env = Object.create(process.env);
env.NODE_ENV = 'test';

const mocha = path.resolve(process.cwd(), 'node_modules', '.bin', 'nyc');
const mochaArgs = '{' + packageJson['mocha-webpack'].include.join(',') + '}';
const nycAPI = require.resolve('nyc');
const nyc = path.resolve(
nycAPI.slice(0, nycAPI.lastIndexOf('/')),
'bin',
'nyc.js'
);

const args = [
'mocha-webpack',
'--timeout=100000',
'--colors',
'--webpack-config',
path.join(__dirname, '..', 'configs', 'webpack.config.test.js'),
mochaArgs
path.join(__dirname, '..', 'configs', 'webpack.config.js'),
...packageJson['mocha-webpack'].include
].filter(Boolean);

const result = spawn.sync(mocha, args, { env: env, stdio: 'inherit' });
const result = spawn.sync(nyc, args, { env: env, stdio: 'inherit' });

process.exit(result.status);
Loading

0 comments on commit 4350309

Please sign in to comment.