我一直试图使用Karma+Jasmine来运行用TypeScript编写的测试。
在网上搜索,似乎有相当多的人完成了这样的壮举,但我找不到关于如何做到这一点的完整指南。环境:
整个事情是怎么安排的?
发布于 2016-01-28 00:15:19
有关完整源代码,请参见此GitHub存储库。
package.json
让我们从devDependencies开始
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-watch": "^0.6.1",
"grunt-karma": "^0.12.1",
"grunt-ts": "^5.1.0",
"jasmine-core": "^2.3.4",
"karma": "^0.13.15",
"karma-jasmine": "^0.3.6",
"karma-phantomjs-launcher": "^0.2.1",
"karma-requirejs": "^0.2.3",
"load-grunt-tasks": "^3.3.0",
"phantomjs": "^1.9.18",
"requirejs": "^2.1.22"
}Gruntfile.js
观看
我们监视所有的.ts文件,并且在更改后首先传输它们,然后运行测试:
watch: {
typescript: {
// Watching all typescript files (specs and units)
files: [ 'source/**/*.ts' ],
// First transpile, then run tests
tasks: [ 'ts:default', 'karma:continuous:run' ],
options: {
spawn: false
}
}
},打字本编撰
我们使用amd模块传输所有.ts文件。请注意,我们需要茉莉花的类型:
ts: {
default: {
src: [
// Note that we add the typings for jasmine here
'typings/jasmine.d.ts',
// We compile all .ts files (specs and units)
'source/**/*.ts'
],
options: {
// We need to convert ES6 to ES5 and use amd so it works
// with RequireJS
module: 'amd',
fast: 'never'
},
// We output all files to a temp folder
outDir: 'transpiled'
}
},因果报应
如果所有的.ts文件都在transpiled文件夹中,我们可以运行业力测试:
karma: {
options: {
configFile: 'karma.conf.js'
},
continuous: {
logLevel: 'INFO',
singleRun: false,
}
}karma.conf.js
最棘手的部分是:
requirejs添加为一个框架。include标志设置为false。test-main.js的文件扫描这些文件,并为测试动态加载所有.spec文件。test-main.js是由Karma生成的,有关更多信息,请参见本指南。就像这样:
module.exports = function(config) {
config.set({
// Note that we also use `requirejs`
frameworks: [ 'jasmine', 'requirejs' ],
files: [
// We add all transpiled files (specs and unit) with included set
// to false.
{ pattern: 'transpiled/**/*.js', included: false },
// Test main will search the files for specs and load them
// dynamically.
'test-main.js'
],
reporters: [ 'progress' ],
browsers: [ 'PhantomJS' ],
autoWatch: false,
singleRun: true
})
}发布于 2016-02-01 12:05:14
看看如何使用卡玛-打字稿-预处理器,这可能比观察、编译和运行测试要容易一些。
这可以很容易地设置为您的karma和/或grunt信任。
https://stackoverflow.com/questions/35050700
复制相似问题