From 64453103d6375a4f012ba72fa32bbea9913e7570 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Fri, 1 Jul 2016 09:15:13 -0700 Subject: [PATCH] feat(config): add support for TypeScript - Add TypeScript support for `karma.conf.ts` --- config.tpl.ts | 67 ++++++++++++++++++++++++++++ docs/config/01-configuration-file.md | 13 ++++++ lib/cli.js | 4 ++ lib/config.js | 9 ++++ lib/init/formatters.js | 17 +++++++ test/unit/cli.spec.js | 10 ++++- 6 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 config.tpl.ts diff --git a/config.tpl.ts b/config.tpl.ts new file mode 100644 index 000000000..67a0f22e2 --- /dev/null +++ b/config.tpl.ts @@ -0,0 +1,67 @@ +// Karma configuration +// Generated on %DATE% + +module.exports = (config) => { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: '%BASE_PATH%', + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: [%FRAMEWORKS%], + + + // list of files / patterns to load in the browser + files: [%FILES% + ], + + + // list of files to exclude + exclude: [%EXCLUDE% + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: %PREPROCESSORS%, + + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ['progress'], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: %AUTO_WATCH%, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: [%BROWSERS%], + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false, + + // Concurrency level + // how many browser should be started simultaneous + concurrency: Infinity + }) +} diff --git a/docs/config/01-configuration-file.md b/docs/config/01-configuration-file.md index 4e8af6e3b..b95b3f44c 100644 --- a/docs/config/01-configuration-file.md +++ b/docs/config/01-configuration-file.md @@ -12,8 +12,10 @@ Unless provided as argument, the Karma CLI will look for a configuration file at * `./karma.conf.js` * `./karma.conf.coffee` +* `./karma.conf.ts` * `./.config/karma.conf.js` * `./.config/karma.conf.coffee` +* `./.config/karma.conf.ts` in that order. @@ -40,6 +42,17 @@ module.exports = (config) -> # ... ``` +```typescript +# karma.conf.ts +module.exports = (config) => { + config.set({ + basePath: '../..', + frameworks: ['jasmine'], + //... + }); +} +``` + ## File Patterns All of the configuration options, which specify file paths, use the [minimatch][minimatch] library to facilitate flexible but concise file expressions so you can easily list all of the files you want to include and exclude. diff --git a/lib/cli.js b/lib/cli.js index 3a9e98823..25a9e988a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -94,10 +94,14 @@ var processArgs = function (argv, options, fs, path) { configFile = './karma.conf.js' } else if (fs.existsSync('./karma.conf.coffee')) { configFile = './karma.conf.coffee' + } else if (fs.existsSync('./karma.conf.ts')) { + configFile = './karma.conf.ts' } else if (fs.existsSync('./.config/karma.conf.js')) { configFile = './.config/karma.conf.js' } else if (fs.existsSync('./.config/karma.conf.coffee')) { configFile = './.config/karma.conf.coffee' + } else if (fs.existsSync('./.config/karma.conf.ts')) { + configFile = './.config/karma.conf.ts' } } diff --git a/lib/config.js b/lib/config.js index a7b27fdf2..4d8cc57fd 100644 --- a/lib/config.js +++ b/lib/config.js @@ -7,6 +7,7 @@ var constant = require('./constants') var COFFEE_SCRIPT_AVAILABLE = false var LIVE_SCRIPT_AVAILABLE = false +var TYPE_SCRIPT_AVAILABLE = false // Coffee is required here to enable config files written in coffee-script. // It's not directly used in this file. @@ -22,6 +23,11 @@ try { LIVE_SCRIPT_AVAILABLE = true } catch (e) {} +try { + require('ts-node').register() + TYPE_SCRIPT_AVAILABLE = true +} catch (e) {} + var Pattern = function (pattern, served, included, watched, nocache) { this.pattern = pattern this.served = helper.isDefined(served) ? served : true @@ -307,6 +313,9 @@ var parseConfig = function (configFilePath, cliOptions) { } else if (extension === '.ls' && !LIVE_SCRIPT_AVAILABLE) { log.error('You need to install LiveScript.\n' + ' npm install LiveScript --save-dev') + } else if (extension === '.ts' && !TYPE_SCRIPT_AVAILABLE) { + log.error('You need to install TypeScript.\n' + + ' npm install typescript ts-node --save-dev') } } return process.exit(1) diff --git a/lib/init/formatters.js b/lib/init/formatters.js index bf4a9efef..f479e09a2 100644 --- a/lib/init/formatters.js +++ b/lib/init/formatters.js @@ -9,6 +9,8 @@ var COFFEE_REQUIREJS_TEMPLATE_PATH = path.join(__dirname, '/../../requirejs.conf var COFFEE_REGEXP = /\.coffee$/ var LIVE_TEMPLATE_PATH = path.join(__dirname, '/../../config.tpl.ls') var LIVE_REGEXP = /\.ls$/ +var TYPE_TEMPLATE_PATH = path.join(__dirname, '/../../config.tpl.ts') +var TYPE_REGEXP = /\.ts$/ var isCoffeeFile = function (filename) { return COFFEE_REGEXP.test(filename) @@ -18,6 +20,10 @@ var isLiveFile = function (filename) { return LIVE_REGEXP.test(filename) } +var isTypeFile = function (filename) { + return TYPE_REGEXP.test(filename) +} + var JavaScriptFormatter = function () { var quote = function (value) { return "'" + value + "'" @@ -114,9 +120,16 @@ var LiveFormatter = function () { this.TEMPLATE_FILE_PATH = LIVE_TEMPLATE_PATH } +var TypeFormatter = function () { + JavaScriptFormatter.call(this) + + this.TEMPLATE_FILE_PATH = TYPE_TEMPLATE_PATH +} + exports.JavaScript = JavaScriptFormatter exports.Coffee = CoffeeFormatter exports.Live = LiveFormatter +exports.Type = TypeFormatter exports.createForPath = function (path) { if (isCoffeeFile(path)) { @@ -127,5 +140,9 @@ exports.createForPath = function (path) { return new LiveFormatter() } + if (isTypeFile(path)) { + return new TypeFormatter() + } + return new JavaScriptFormatter() } diff --git a/test/unit/cli.spec.js b/test/unit/cli.spec.js index bf3b0dff2..a4af1c659 100644 --- a/test/unit/cli.spec.js +++ b/test/unit/cli.spec.js @@ -12,7 +12,8 @@ describe('cli', () => { var fsMock = mocks.fs.create({ cwd: {'karma.conf.js': true}, - cwd2: {'karma.conf.coffee': true} + cwd2: {'karma.conf.coffee': true}, + cwd3: {'karma.conf.ts': true} }) var currentCwd = null @@ -87,6 +88,13 @@ describe('cli', () => { expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd2/karma.conf.coffee')) }) + it('should set default karma.conf.ts config file if exists', () => { + setCWD('/cwd3') + var options = processArgs(['--port', '10']) + + expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd3/karma.conf.ts')) + }) + it('should not set default config if neither exists', () => { setCWD('/') var options = processArgs([])