【问题标题】:Reflect.getOwnMetadata is not a function with karma-typescriptReflect.getOwnMetadata 不是 karma-typescript 的函数
【发布时间】:2017-05-12 22:42:33
【问题描述】:

我正在尝试对我的 TypeScript 项目进行单元测试(使用 Karma + Jasmine + karma-typescript)。项目结构如下:

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
  })
}

我的规范文件如下所示:

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 时,我得到了

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(s) 没有被加载到浏览器中。但是,我的规范文件中有imported aurelia-pollyfills

请建议如何纠正此问题。


更新: 任何正在寻找答案的人,也可能会遇到来自karma-remap-istanbul 的源映射 (Error: Could not find source map for:'') 尝试生成覆盖率报告的问题。

避免此问题的一种方法是简单地删除有问题的报告器插件。例如,将reporters: ['mocha', 'coverage', 'karma-remap-istanbul'] 更改为reporters: ['mocha', 'coverage']

其他解决方案是生成源地图。如果您无法在 tsconfig.json 中指定相同的内容,则可以在使用 karma-typescript 时在 karma.conf.js 中指定:

karmaTypescriptConfig: {
  tsconfig: "./tsconfig.json",
  compilerOptions: {
    sourceMap: true
  }
}

最后,我得到了reporters: ["mocha", "karma-typescript"],因为它显示了哪些测试通过了,哪些失败了,并生成了一份覆盖率报告。

【问题讨论】:

    标签: javascript typescript karma-runner karma-jasmine aurelia


    【解决方案1】:

    您可能缺少 reflect-metadata 导入:

    $ npm install --save-dev reflect-metadata

    然后将以下内容添加到您的files

    files: [
        { pattern: "node_modules/reflect-metadata/Reflect.js", include: true },
        { pattern: "src/*.ts", include: true },
        { pattern: "tests/**/*Spec.ts", include: true }
    ]
    

    【讨论】:

    • 感谢您的回答。它似乎有效,因为该错误现在消失了(但面临其他与 sourcemap 相关的问题;使用 reporters: ['mocha', 'coverage'], 忽略它)。但是,我想了解为什么我们需要明确包含该文件,因为我已经在我的规范文件中导入了相同的文件。猜猜它不是默认捆绑的?
    • reflect-metadata 旨在用于全局,这就是为什么需要作为全局导入
    猜你喜欢
    • 2017-04-17
    • 2017-08-10
    • 1970-01-01
    • 2019-12-31
    • 2015-12-06
    • 2016-03-03
    • 2017-01-14
    • 2016-08-10
    • 2018-06-26
    相关资源
    最近更新 更多