【发布时间】:2017-07-07 22:15:59
【问题描述】:
我开始从事一个使用 Jasmine 和 Karma 的打字稿项目。不幸的是,Karma 无法执行已编译的单元测试,Chrome 中出现此错误:
Uncaught ReferenceError: define is not defined
示例项目 - 我相信这是演示问题的最简单的配置:
package.json:
{
"name": "HelloWorld",
"scripts": {
"build": "tsc -p ts",
"test": "./node_modules/karma/bin/karma start karma.conf.js"
},
"devDependencies": {
"typescript": "~2.4.1"
,"jasmine-core": "2.6.4"
,"jasmine": "2.6.0"
,"@types/jasmine": "2.5.53"
,"karma": "1.7.0"
,"karma-chrome-launcher": "^2.2.0"
,"karma-jasmine": "^1.1.0"
}
}
karma.conf.js:
module.exports = function(config) {
config.set({
frameworks: ['jasmine'],
files: ['js/*.spec.js'],
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
ts/tsconfig:
{
"compilerOptions": {
"target": "es5"
,"module": "amd"
,"lib": [ "es2015", "dom" ]
,"outDir": "../js"
}
}
ts/helloworld.ts
function returnHello() : string {
return "Hello";
}
export default returnHello;
ts/helloworld.spec.ts
import returnHello from "helloworld";
describe("Some Test", function() {
it("passes", () => expect(returnHello()).toEqual("Hello"));
});
【问题讨论】:
-
有趣,从未想过尝试在 Angular 项目之外运行 jasmine/karma。您正在测试的是后端代码还是前端代码?
-
它的前端代码。
标签: typescript jasmine karma-runner