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,96 +2,96 @@ 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) {
output: { return {
path: path.resolve(process.cwd(), 'dist'), output: {
publicPath: '/dist/', path: path.resolve(process.cwd(), 'dist'),
filename: '[name].js', publicPath: '/dist/',
chunkFilename: '[id].js', filename: '[name].js',
libraryTarget: 'umd' chunkFilename: '[id].js',
}, libraryTarget: 'umd'
plugins: [ },
new ProgressBarPlugin(), plugins: [
new webpack.LoaderOptionsPlugin({ new ProgressBarPlugin(),
minimize: true, new webpack.LoaderOptionsPlugin({
options: { minimize: true,
babel: { options: {
presets: ['env'], babel: {
plugins: ['transform-runtime', 'transform-vue-jsx'] presets: ['env'],
}, plugins: ['transform-runtime', 'transform-vue-jsx']
vue: { },
autoprefixer: false, vue: {
preserveWhitespace: false autoprefixer: false,
preserveWhitespace: false
}
} }
} }),
}) new webpack.DefinePlugin({
], 'process.env': {
stats: 'errors-only', TEST_FILE: `"${testFileName}"`
resolve: { }
modules: [ })
path.resolve(process.cwd(), 'node_modules'),
'node_modules'
], ],
extensions: ['.js', '.json', '.vue'], stats: 'errors-only',
alias: { resolve: {
src: path.resolve(process.cwd(), 'src'), modules: [path.resolve(process.cwd(), 'node_modules'), 'node_modules'],
packages: path.resolve(process.cwd(), 'packages'), extensions: ['.js', '.json', '.vue'],
examples: path.resolve(process.cwd(), 'examples'), alias: {
vue$: 'vue/dist/vue.common.js' src: path.resolve(process.cwd(), 'src'),
} packages: path.resolve(process.cwd(), 'packages'),
}, examples: path.resolve(process.cwd(), 'examples'),
module: { vue$: 'vue/dist/vue.common.js'
rules: [
{
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules|vue-router\/|vue-loader\/|docs|test|src\/index|src\/utils|src\/mixins|packages\/swipe/,
use: ['isparta-loader']
},
{
test: /\.js$/,
exclude: /node_modules|vue-router\/|vue-loader\//,
use: ['babel-loader']
},
{
test: /\.(css|pcss)$/,
use: [
'style-loader',
'css-loader',
'postcss-loader'
]
},
{
test: /\.(gif|png|jpe?g)(\?\S*)?$/,
use: [{
loader: 'url-loader',
options: {
query: {
limit: 10000,
name: 'static/[name].[hash:7].[ext]'
}
}
}]
},
{
test: /\.vue$/,
use: [{
loader: 'vue-loader',
options: {
loaders: {
css: [
'style-loader',
'css-loader',
'postcss-loader'
],
js: ['isparta-loader']
}
}
}]
} }
] },
}, module: {
devtool: '#inline-source-map' rules: [
}; {
enforce: 'pre',
test: /\.js$/,
exclude: /node_modules|vue-router\/|vue-loader\/|docs|test|src\/index|src\/utils|src\/mixins|packages\/swipe/,
use: ['isparta-loader']
},
{
test: /\.js$/,
exclude: /node_modules|vue-router\/|vue-loader\//,
use: ['babel-loader']
},
{
test: /\.(css|pcss)$/,
use: ['style-loader', 'css-loader', 'postcss-loader']
},
{
test: /\.(gif|png|jpe?g)(\?\S*)?$/,
use: [
{
loader: 'url-loader',
options: {
query: {
limit: 10000,
name: 'static/[name].[hash:7].[ext]'
}
}
}
]
},
{
test: /\.vue$/,
use: [
{
loader: 'vue-loader',
options: {
loaders: {
css: ['style-loader', 'css-loader', 'postcss-loader'],
js: ['isparta-loader']
}
}
}
]
}
]
},
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$/);
testsReq.keys().forEach(testsReq); if (process.env.TEST_FILE) {
testsReq.keys().forEach((file) => {
if (file.indexOf(process.env.TEST_FILE) !== -1) {
testsReq(file);
}
});
} else {
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);
});