问题描述
我正在尝试进行单元测试(使用Karma +茉莉花+ karma-typescript )我的TypeScript项目.项目结构如下:
I am trying to unit test (with Karma + Jasmine + karma-typescript) my TypeScript project. The project structure is as follows:
root
|- src/*.ts //all TypeScript source files
|- tests/unit/*.spec.ts //all spec (test) files
|- karma.conf.js
|- tsconfig.json
我的karma.conf.js
如下所示:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', "karma-typescript"],
karmaTypescriptConfig: {
tsconfig: "./tsconfig.json"
},
files: [
'src/*.ts',
'tests/**/*Spec.ts'
],
exclude: [],
preprocessors: {
"**/*.ts": ["karma-typescript"]
},
reporters: ["progress", "karma-typescript"],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
我的规格文件如下所示:
My spec file looks like below:
import 'aurelia-polyfills'; //<- importing this, as the project have dependency on Aurelia
// import "babel-polyfill";
import "reflect-metadata";
import "jasmine";
import { Utility } from './../../src/Utility';
describe("this is a try to set up karma-jasmine-webpack test (TS)", () => {
it("utility_test", () => {
const result = Utility.doSomething();
const expected = Expected_Result;
expect(result).toEqual(expected);
});
});
但是当我运行karma start
时,我会得到
But when I run karma start
, I get
Chrome 55.0.2883 (Windows 10 0.0.0) ERROR
Uncaught TypeError: Reflect.getOwnMetadata is not a function
at C:/Users/spal/AppData/Local/Temp/karma-typescript-bundle-16376WqjdFvsYtjdI.js:2325
我认为这是因为pollyfill没有在浏览器中加载.但是,我在规格文件中已import
ed aurelia-pollyfills
.
I assume, that it is because of pollyfill(s) is/are not being loaded in the browser. However, I have import
ed aurelia-pollyfills
in my spec file.
请提出如何更正此问题的建议.
Please suggest how this can be corrected.
更新:任何正在寻找答案的人,可能还会遇到karma-remap-istanbul
中试图生成覆盖率报告的源地图(Error: Could not find source map for:''
)的问题.
Update: Anyone looking at this for answer, might also face issues with source map (Error: Could not find source map for:''
) from karma-remap-istanbul
trying to generate coverage report.
避免此问题的一种方法是简单地删除有问题的报告程序插件.例如,将reporters: ['mocha', 'coverage', 'karma-remap-istanbul']
更改为reporters: ['mocha', 'coverage']
.
One way to avoid this problem is to simply remove the problematic reporter plugin. For example, change reporters: ['mocha', 'coverage', 'karma-remap-istanbul']
to reporters: ['mocha', 'coverage']
.
其他解决方案是生成源地图.如果无法在tsconfig.json
中指定相同的名称,则可以在karma.conf.js
中指定(如果使用的是karma-typescript
):
Other solution would be to generate the source maps. In case you can't specify the same in your tsconfig.json
, you can specify that in karma.conf.js
if you are using karma-typescript
:
karmaTypescriptConfig: {
tsconfig: "./tsconfig.json",
compilerOptions: {
sourceMap: true
}
}
最后,我以reporters: ["mocha", "karma-typescript"]
结尾,它显示了哪个测试通过,哪个测试失败以及生成覆盖率报告.
Lastly, I ended up with reporters: ["mocha", "karma-typescript"]
, as it shows which test passed, and which failed, as well as generate a coverage report.
推荐答案
您可能缺少reflect-metadata
导入:
然后将以下内容添加到您的files
:
Then add the following to your files
:
files: [
{ pattern: "node_modules/reflect-metadata/Reflect.js", include: true },
{ pattern: "src/*.ts", include: true },
{ pattern: "tests/**/*Spec.ts", include: true }
]
这篇关于Reflect.getOwnMetadata不是因果类型脚本的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!