Skip to content

Commit e597da2

Browse files
committed
feat(tests): make karma optional
use mocha by default add babel-register fix regex of test files add glob
1 parent 1dbdc08 commit e597da2

File tree

8 files changed

+76
-28
lines changed

8 files changed

+76
-28
lines changed

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
process.env.DEBUG = 'pixore:front-scripts, pixore:error'
44

5-
const {input} = require('parse-cmd-args')()
5+
const { input } = require('parse-cmd-args')()
66
const debug = require('debug')('pixore:front-scripts')
77

88
switch (input) {

jsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=759670
3+
// for the documentation about the jsconfigon format
4+
"compilerOptions": {
5+
"module": "es6",
6+
"target": "es6",
7+
"noLib": true
8+
},
9+
"exclude": [
10+
"node_modules",
11+
"bower_components",
12+
"jspm_packages",
13+
"tmp",
14+
"temp"
15+
]
16+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"babel-plugin-transform-object-rest-spread": "^6.23.0",
2424
"babel-preset-es2015": "^6.24.1",
2525
"babel-preset-react": "^6.24.1",
26+
"babel-register": "^6.24.1",
2627
"bluebird": "^3.5.0",
2728
"chalk": "^1.1.3",
2829
"connect-history-api-fallback": "^1.3.0",
@@ -33,6 +34,7 @@
3334
"exports-loader": "^0.6.4",
3435
"extract-text-webpack-plugin": "^2.1.2",
3536
"file-loader": "^0.11.2",
37+
"glob": "^7.1.2",
3638
"html-webpack-plugin": "^2.28.0",
3739
"image-webpack-loader": "^3.3.1",
3840
"imports-loader": "^0.7.1",

src/config/babelrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
const { isTest } = require('./environment')
3+
24
module.exports = {
35
plugins: [
46
require.resolve('babel-plugin-transform-object-rest-spread'),
@@ -8,7 +10,7 @@ module.exports = {
810
[
911
require.resolve('babel-preset-es2015'),
1012
{
11-
'modules': false
13+
'modules': isTest ? 'commonjs' : true
1214
}
1315
],
1416
require.resolve('babel-preset-react')

src/config/karma.conf.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const webpackConfig = require('./webpack.config')
44

55
webpackConfig.devtool = 'cheap-module-source-map'
66
webpackConfig.watch = true
7+
Object.assign(webpackConfig.externals, {
8+
'react/lib/ExecutionEnvironment': true,
9+
'react/addons': true,
10+
'react/lib/ReactContext': 'window'
11+
})
712

813
const testPath = require.resolve('./tests.webpack.js')
914
module.exports = config => {

src/config/tests.webpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
const context = require.context(process.env.pwd, true, /.+(__tests__\/).+\.spec\.js?$/)
2+
const context = require.context(process.env.pwd, true, /src\/.+(\/__tests__\/).+\.spec\.js?$/)
33

44
context.keys().forEach(context)

src/config/webpack.config.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@ const plugins = [
2121
let devtool
2222
let entry
2323
let devServer
24-
const externals = []
24+
const externals = {}
2525

2626
const modules = {
2727
rules: [{
28-
test: /\.js$/,
29-
loader: 'eslint-loader',
30-
enforce: 'pre',
31-
exclude: /node_modules|webpackHotDevClient/
32-
}, {
3328
test: /\.js$/,
3429
exclude: /node_modules/,
3530
loader: 'babel-loader',
@@ -83,6 +78,28 @@ const output = {
8378
filename: isProd ? '[name].[chunkhash].js' : 'build.js'
8479
}
8580

81+
if (isDev) {
82+
modules.rules = [{
83+
test: /\.js$/,
84+
loader: 'eslint-loader',
85+
enforce: 'pre',
86+
exclude: /node_modules|webpackHotDevClient/
87+
}].concat(modules.rules)
88+
plugins.push(
89+
new webpack.LoaderOptionsPlugin({
90+
test: /\.js$/,
91+
options: {
92+
eslint: {
93+
options: {
94+
cacheDirectory: true,
95+
configFile: ESLINT_PATH
96+
}
97+
}
98+
}
99+
})
100+
)
101+
}
102+
86103
if (isProd) {
87104
entry = {
88105
index: APP_PATH
@@ -115,17 +132,6 @@ if (isProd) {
115132
]
116133
devtool = 'source-map'
117134
plugins.push(
118-
new webpack.LoaderOptionsPlugin({
119-
test: /\.js$/,
120-
options: {
121-
eslint: {
122-
options: {
123-
cacheDirectory: true,
124-
configFile: ESLINT_PATH
125-
}
126-
}
127-
}
128-
}),
129135
new HtmlWebpackPlugin({
130136
title: 'Pixore',
131137
filename: 'index.html',

src/scripts/test.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
process.env.NODE_ENV = 'test'
2-
2+
const glob = require('glob')
3+
const { flags } = require('parse-cmd-args')()
4+
const Mocha = require('mocha')
35
const Server = require('karma').Server
46
const debug = require('debug')('pixore:front-scripts')
57

8+
const babelConfig = require('../config/babelrc.js')
69
const { validStructure } = require('../utils')
710
const karmaConfig = require.resolve('../config/karma.conf.js')
811

912
validStructure()
1013

11-
const server = new Server({
12-
configFile: karmaConfig
13-
}, (exitCode) => {
14-
debug('Karma has exited with ' + exitCode)
15-
process.exit(exitCode)
16-
})
14+
const withKarma = flags['--karma']
1715

18-
server.start()
16+
if (withKarma) {
17+
const server = new Server({
18+
configFile: karmaConfig
19+
}, (exitCode) => {
20+
debug('Karma has exited with ' + exitCode)
21+
process.exit(exitCode)
22+
})
23+
server.start()
24+
} else {
25+
require('babel-register')(babelConfig)
26+
const mocha = new Mocha()
27+
glob('src/**/__tests__/*.spec.js', function (err, files) {
28+
if (err) {
29+
debug(err)
30+
process.exit(1)
31+
}
32+
files.forEach((file) => mocha.addFile(file))
33+
mocha.run(failures => process.on('exit', () => process.exit(failures)))
34+
})
35+
}

0 commit comments

Comments
 (0)