feat: support run single test

This commit is contained in:
陈嘉涵 2017-08-24 20:36:42 +08:00
parent c6b8de3541
commit 057049c15a
5 changed files with 126 additions and 93 deletions

View File

@ -26,6 +26,7 @@
"test": "karma start test/unit/karma.conf.js --single-run", "test": "karma start test/unit/karma.conf.js --single-run",
"test:coverage": "open test/unit/coverage/lcov-report/index.html", "test:coverage": "open test/unit/coverage/lcov-report/index.html",
"test:watch": "karma start test/unit/karma.conf.js", "test:watch": "karma start test/unit/karma.conf.js",
"test:single": "node ./test/unit/selector.js",
"release": "npm run bootstrap && sh build/release.sh" "release": "npm run bootstrap && sh build/release.sh"
}, },
"repository": { "repository": {

View File

@ -2,7 +2,8 @@ const path = require('path');
const webpack = require('webpack'); const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin'); const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const webpackConfig = { function getWebpackConfig(testFileName) {
return {
output: { output: {
path: path.resolve(process.cwd(), 'dist'), path: path.resolve(process.cwd(), 'dist'),
publicPath: '/dist/', publicPath: '/dist/',
@ -24,14 +25,16 @@ const webpackConfig = {
preserveWhitespace: false preserveWhitespace: false
} }
} }
}),
new webpack.DefinePlugin({
'process.env': {
TEST_FILE: `"${testFileName}"`
}
}) })
], ],
stats: 'errors-only', stats: 'errors-only',
resolve: { resolve: {
modules: [ modules: [path.resolve(process.cwd(), 'node_modules'), 'node_modules'],
path.resolve(process.cwd(), 'node_modules'),
'node_modules'
],
extensions: ['.js', '.json', '.vue'], extensions: ['.js', '.json', '.vue'],
alias: { alias: {
src: path.resolve(process.cwd(), 'src'), src: path.resolve(process.cwd(), 'src'),
@ -55,15 +58,12 @@ const webpackConfig = {
}, },
{ {
test: /\.(css|pcss)$/, test: /\.(css|pcss)$/,
use: [ use: ['style-loader', 'css-loader', 'postcss-loader']
'style-loader',
'css-loader',
'postcss-loader'
]
}, },
{ {
test: /\.(gif|png|jpe?g)(\?\S*)?$/, test: /\.(gif|png|jpe?g)(\?\S*)?$/,
use: [{ use: [
{
loader: 'url-loader', loader: 'url-loader',
options: { options: {
query: { query: {
@ -71,27 +71,27 @@ const webpackConfig = {
name: 'static/[name].[hash:7].[ext]' name: 'static/[name].[hash:7].[ext]'
} }
} }
}] }
]
}, },
{ {
test: /\.vue$/, test: /\.vue$/,
use: [{ use: [
{
loader: 'vue-loader', loader: 'vue-loader',
options: { options: {
loaders: { loaders: {
css: [ css: ['style-loader', 'css-loader', 'postcss-loader'],
'style-loader',
'css-loader',
'postcss-loader'
],
js: ['isparta-loader'] js: ['isparta-loader']
} }
} }
}] }
]
} }
] ]
}, },
devtool: '#inline-source-map' devtool: '#inline-source-map'
}; };
}
module.exports = webpackConfig; module.exports = getWebpackConfig;

View File

@ -1,5 +1,13 @@
require('packages/vant-css/src/index.css'); require('packages/vant-css/src/index.css');
// require all test files (files that ends with .spec.js) // 读取配置文件,判断运行单个测试文件还是所有测试文件
const testsReq = require.context('./specs', true, /\.spec$/); const testsReq = require.context('./specs', true, /\.spec$/);
if (process.env.TEST_FILE) {
testsReq.keys().forEach((file) => {
if (file.indexOf(process.env.TEST_FILE) !== -1) {
testsReq(file);
}
});
} else {
testsReq.keys().forEach(testsReq); testsReq.keys().forEach(testsReq);
}

View File

@ -4,7 +4,7 @@ require('babel-core/register')({
presets: [require('babel-preset-env')] presets: [require('babel-preset-env')]
}); });
var webpackConfig = require('./get-webpack-conf'); var getWebpackConfig = require('./get-webpack-conf');
var travis = process.env.TRAVIS; var travis = process.env.TRAVIS;
module.exports = function(config) { module.exports = function(config) {
@ -16,7 +16,7 @@ module.exports = function(config) {
preprocessors: { preprocessors: {
'./index.js': ['webpack', 'sourcemap'] './index.js': ['webpack', 'sourcemap']
}, },
webpack: webpackConfig, webpack: getWebpackConfig(getTestFileName()),
webpackMiddleware: { webpackMiddleware: {
noInfo: true noInfo: true
}, },
@ -30,3 +30,8 @@ module.exports = function(config) {
singleRun: false singleRun: false
}); });
}; };
function getTestFileName() {
const flagIndex = process.argv.indexOf('--file');
return flagIndex !== -1 ? process.argv[flagIndex + 1] : '';
}

19
test/unit/selector.js Normal file
View File

@ -0,0 +1,19 @@
/**
* 运行单个测试文件
*/
const fs = require('fs');
const inquirer = require('inquirer');
const path = require('path');
const shell = require('shelljs');
const files = fs.readdirSync(path.resolve(__dirname, './specs'));
inquirer.prompt([{
type: 'list',
name: 'select',
message: '请选择要运行的测试文件:',
choices: files
}], (result) => {
const file = result.select.replace('.spec.js', '');
shell.exec('karma start test/unit/karma.conf.js --color alway --file ' + file);
});