Skip to content

Commit 6445310

Browse files
committed
feat(config): add support for TypeScript
- Add TypeScript support for `karma.conf.ts`
1 parent 9c894f9 commit 6445310

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

config.tpl.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Karma configuration
2+
// Generated on %DATE%
3+
4+
module.exports = (config) => {
5+
config.set({
6+
7+
// base path that will be used to resolve all patterns (eg. files, exclude)
8+
basePath: '%BASE_PATH%',
9+
10+
11+
// frameworks to use
12+
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13+
frameworks: [%FRAMEWORKS%],
14+
15+
16+
// list of files / patterns to load in the browser
17+
files: [%FILES%
18+
],
19+
20+
21+
// list of files to exclude
22+
exclude: [%EXCLUDE%
23+
],
24+
25+
26+
// preprocess matching files before serving them to the browser
27+
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
28+
preprocessors: %PREPROCESSORS%,
29+
30+
31+
// test results reporter to use
32+
// possible values: 'dots', 'progress'
33+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
34+
reporters: ['progress'],
35+
36+
37+
// web server port
38+
port: 9876,
39+
40+
41+
// enable / disable colors in the output (reporters and logs)
42+
colors: true,
43+
44+
45+
// level of logging
46+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
47+
logLevel: config.LOG_INFO,
48+
49+
50+
// enable / disable watching file and executing tests whenever any file changes
51+
autoWatch: %AUTO_WATCH%,
52+
53+
54+
// start these browsers
55+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
56+
browsers: [%BROWSERS%],
57+
58+
59+
// Continuous Integration mode
60+
// if true, Karma captures browsers, runs the tests and exits
61+
singleRun: false,
62+
63+
// Concurrency level
64+
// how many browser should be started simultaneous
65+
concurrency: Infinity
66+
})
67+
}

docs/config/01-configuration-file.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ Unless provided as argument, the Karma CLI will look for a configuration file at
1212

1313
* `./karma.conf.js`
1414
* `./karma.conf.coffee`
15+
* `./karma.conf.ts`
1516
* `./.config/karma.conf.js`
1617
* `./.config/karma.conf.coffee`
18+
* `./.config/karma.conf.ts`
1719

1820
in that order.
1921

@@ -40,6 +42,17 @@ module.exports = (config) ->
4042
# ...
4143
```
4244

45+
```typescript
46+
# karma.conf.ts
47+
module.exports = (config) => {
48+
config.set({
49+
basePath: '../..',
50+
frameworks: ['jasmine'],
51+
//...
52+
});
53+
}
54+
```
55+
4356
## File Patterns
4457
All of the configuration options, which specify file paths, use the [minimatch][minimatch] library to facilitate flexible
4558
but concise file expressions so you can easily list all of the files you want to include and exclude.

lib/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,14 @@ var processArgs = function (argv, options, fs, path) {
9494
configFile = './karma.conf.js'
9595
} else if (fs.existsSync('./karma.conf.coffee')) {
9696
configFile = './karma.conf.coffee'
97+
} else if (fs.existsSync('./karma.conf.ts')) {
98+
configFile = './karma.conf.ts'
9799
} else if (fs.existsSync('./.config/karma.conf.js')) {
98100
configFile = './.config/karma.conf.js'
99101
} else if (fs.existsSync('./.config/karma.conf.coffee')) {
100102
configFile = './.config/karma.conf.coffee'
103+
} else if (fs.existsSync('./.config/karma.conf.ts')) {
104+
configFile = './.config/karma.conf.ts'
101105
}
102106
}
103107

lib/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var constant = require('./constants')
77

88
var COFFEE_SCRIPT_AVAILABLE = false
99
var LIVE_SCRIPT_AVAILABLE = false
10+
var TYPE_SCRIPT_AVAILABLE = false
1011

1112
// Coffee is required here to enable config files written in coffee-script.
1213
// It's not directly used in this file.
@@ -22,6 +23,11 @@ try {
2223
LIVE_SCRIPT_AVAILABLE = true
2324
} catch (e) {}
2425

26+
try {
27+
require('ts-node').register()
28+
TYPE_SCRIPT_AVAILABLE = true
29+
} catch (e) {}
30+
2531
var Pattern = function (pattern, served, included, watched, nocache) {
2632
this.pattern = pattern
2733
this.served = helper.isDefined(served) ? served : true
@@ -307,6 +313,9 @@ var parseConfig = function (configFilePath, cliOptions) {
307313
} else if (extension === '.ls' && !LIVE_SCRIPT_AVAILABLE) {
308314
log.error('You need to install LiveScript.\n' +
309315
' npm install LiveScript --save-dev')
316+
} else if (extension === '.ts' && !TYPE_SCRIPT_AVAILABLE) {
317+
log.error('You need to install TypeScript.\n' +
318+
' npm install typescript ts-node --save-dev')
310319
}
311320
}
312321
return process.exit(1)

lib/init/formatters.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ var COFFEE_REQUIREJS_TEMPLATE_PATH = path.join(__dirname, '/../../requirejs.conf
99
var COFFEE_REGEXP = /\.coffee$/
1010
var LIVE_TEMPLATE_PATH = path.join(__dirname, '/../../config.tpl.ls')
1111
var LIVE_REGEXP = /\.ls$/
12+
var TYPE_TEMPLATE_PATH = path.join(__dirname, '/../../config.tpl.ts')
13+
var TYPE_REGEXP = /\.ts$/
1214

1315
var isCoffeeFile = function (filename) {
1416
return COFFEE_REGEXP.test(filename)
@@ -18,6 +20,10 @@ var isLiveFile = function (filename) {
1820
return LIVE_REGEXP.test(filename)
1921
}
2022

23+
var isTypeFile = function (filename) {
24+
return TYPE_REGEXP.test(filename)
25+
}
26+
2127
var JavaScriptFormatter = function () {
2228
var quote = function (value) {
2329
return "'" + value + "'"
@@ -114,9 +120,16 @@ var LiveFormatter = function () {
114120
this.TEMPLATE_FILE_PATH = LIVE_TEMPLATE_PATH
115121
}
116122

123+
var TypeFormatter = function () {
124+
JavaScriptFormatter.call(this)
125+
126+
this.TEMPLATE_FILE_PATH = TYPE_TEMPLATE_PATH
127+
}
128+
117129
exports.JavaScript = JavaScriptFormatter
118130
exports.Coffee = CoffeeFormatter
119131
exports.Live = LiveFormatter
132+
exports.Type = TypeFormatter
120133

121134
exports.createForPath = function (path) {
122135
if (isCoffeeFile(path)) {
@@ -127,5 +140,9 @@ exports.createForPath = function (path) {
127140
return new LiveFormatter()
128141
}
129142

143+
if (isTypeFile(path)) {
144+
return new TypeFormatter()
145+
}
146+
130147
return new JavaScriptFormatter()
131148
}

test/unit/cli.spec.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ describe('cli', () => {
1212

1313
var fsMock = mocks.fs.create({
1414
cwd: {'karma.conf.js': true},
15-
cwd2: {'karma.conf.coffee': true}
15+
cwd2: {'karma.conf.coffee': true},
16+
cwd3: {'karma.conf.ts': true}
1617
})
1718

1819
var currentCwd = null
@@ -87,6 +88,13 @@ describe('cli', () => {
8788
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd2/karma.conf.coffee'))
8889
})
8990

91+
it('should set default karma.conf.ts config file if exists', () => {
92+
setCWD('/cwd3')
93+
var options = processArgs(['--port', '10'])
94+
95+
expect(path.resolve(options.configFile)).to.equal(path.resolve('/cwd3/karma.conf.ts'))
96+
})
97+
9098
it('should not set default config if neither exists', () => {
9199
setCWD('/')
92100
var options = processArgs([])

0 commit comments

Comments
 (0)