Skip to content

Commit

Permalink
Merge pull request karma-runner#2224 from wesleycho/feat/typescript
Browse files Browse the repository at this point in the history
feat(config): add support for TypeScript
  • Loading branch information
dignifiedquire authored Jul 3, 2016
2 parents 9c894f9 + 6445310 commit 43dccd1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
67 changes: 67 additions & 0 deletions config.tpl.ts
Original file line number Diff line number Diff line change
@@ -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
})
}
13 changes: 13 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions lib/init/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 + "'"
Expand Down Expand Up @@ -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)) {
Expand All @@ -127,5 +140,9 @@ exports.createForPath = function (path) {
return new LiveFormatter()
}

if (isTypeFile(path)) {
return new TypeFormatter()
}

return new JavaScriptFormatter()
}
10 changes: 9 additions & 1 deletion test/unit/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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([])
Expand Down

0 comments on commit 43dccd1

Please sign in to comment.