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:coverage": "open test/unit/coverage/lcov-report/index.html",
"test:watch": "karma start test/unit/karma.conf.js",
"test:single": "node ./test/unit/selector.js",
"release": "npm run bootstrap && sh build/release.sh"
},
"repository": {

View File

@ -2,96 +2,96 @@ const path = require('path');
const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const webpackConfig = {
output: {
path: path.resolve(process.cwd(), 'dist'),
publicPath: '/dist/',
filename: '[name].js',
chunkFilename: '[id].js',
libraryTarget: 'umd'
},
plugins: [
new ProgressBarPlugin(),
new webpack.LoaderOptionsPlugin({
minimize: true,
options: {
babel: {
presets: ['env'],
plugins: ['transform-runtime', 'transform-vue-jsx']
},
vue: {
autoprefixer: false,
preserveWhitespace: false
function getWebpackConfig(testFileName) {
return {
output: {
path: path.resolve(process.cwd(), 'dist'),
publicPath: '/dist/',
filename: '[name].js',
chunkFilename: '[id].js',
libraryTarget: 'umd'
},
plugins: [
new ProgressBarPlugin(),
new webpack.LoaderOptionsPlugin({
minimize: true,
options: {
babel: {
presets: ['env'],
plugins: ['transform-runtime', 'transform-vue-jsx']
},
vue: {
autoprefixer: false,
preserveWhitespace: false
}
}
}
})
],
stats: 'errors-only',
resolve: {
modules: [
path.resolve(process.cwd(), 'node_modules'),
'node_modules'
}),
new webpack.DefinePlugin({
'process.env': {
TEST_FILE: `"${testFileName}"`
}
})
],
extensions: ['.js', '.json', '.vue'],
alias: {
src: path.resolve(process.cwd(), 'src'),
packages: path.resolve(process.cwd(), 'packages'),
examples: path.resolve(process.cwd(), 'examples'),
vue$: 'vue/dist/vue.common.js'
}
},
module: {
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']
}
}
}]
stats: 'errors-only',
resolve: {
modules: [path.resolve(process.cwd(), 'node_modules'), 'node_modules'],
extensions: ['.js', '.json', '.vue'],
alias: {
src: path.resolve(process.cwd(), 'src'),
packages: path.resolve(process.cwd(), 'packages'),
examples: path.resolve(process.cwd(), 'examples'),
vue$: 'vue/dist/vue.common.js'
}
]
},
devtool: '#inline-source-map'
};
},
module: {
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 all test files (files that ends with .spec.js)
// 读取配置文件,判断运行单个测试文件还是所有测试文件
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')]
});
var webpackConfig = require('./get-webpack-conf');
var getWebpackConfig = require('./get-webpack-conf');
var travis = process.env.TRAVIS;
module.exports = function(config) {
@ -16,7 +16,7 @@ module.exports = function(config) {
preprocessors: {
'./index.js': ['webpack', 'sourcemap']
},
webpack: webpackConfig,
webpack: getWebpackConfig(getTestFileName()),
webpackMiddleware: {
noInfo: true
},
@ -30,3 +30,8 @@ module.exports = function(config) {
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);
});