From 057049c15aaad61dd35fa25fe2216963fb49a0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=98=89=E6=B6=B5?= Date: Thu, 24 Aug 2017 20:36:42 +0800 Subject: [PATCH] feat: support run single test --- package.json | 1 + test/unit/get-webpack-conf.js | 178 +++++++++++++++++----------------- test/unit/index.js | 12 ++- test/unit/karma.conf.js | 9 +- test/unit/selector.js | 19 ++++ 5 files changed, 126 insertions(+), 93 deletions(-) create mode 100644 test/unit/selector.js diff --git a/package.json b/package.json index f51ff75c3..954d349c8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/unit/get-webpack-conf.js b/test/unit/get-webpack-conf.js index 1c57dd658..6d0818692 100644 --- a/test/unit/get-webpack-conf.js +++ b/test/unit/get-webpack-conf.js @@ -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; diff --git a/test/unit/index.js b/test/unit/index.js index fc003e86e..998623e9d 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -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); +} \ No newline at end of file diff --git a/test/unit/karma.conf.js b/test/unit/karma.conf.js index e94bd9532..7c8a89d96 100644 --- a/test/unit/karma.conf.js +++ b/test/unit/karma.conf.js @@ -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] : ''; +} diff --git a/test/unit/selector.js b/test/unit/selector.js new file mode 100644 index 000000000..ea3b633eb --- /dev/null +++ b/test/unit/selector.js @@ -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); +});