【问题标题】:Karma with Webpack and Typescript performs no tests带有 Webpack 和 Typescript 的 Karma 不执行任何测试
【发布时间】:2017-06-03 12:06:55
【问题描述】:

我正在尝试弄清楚如何将 Karma 测试运行程序与 Webpack 和 Typescript 源文件一起使用。以这个源文件作为唯一的测试文件为例:

test.spec.ts

var message: string = 'yay';
alert(message);
describe('1st tests', () => {
  it('true is true', () => expect(true).toBe(true));
});

以及以下业力配置:

karma.config.js

module.exports = function (config) {

  config.set({
    basePath: '',
    frameworks: ['jasmine'],

    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-webpack'),
      require('karma-jasmine-html-reporter')
    ],

    client: {
      builtPaths: ['app/'], // add more spec base paths as needed
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },

    customLaunchers: {
      // From the CLI. Not used here but interesting
      // chrome setup for travis CI using chromium
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },

    files: [
      { pattern: 'app/test.spec.ts', included: true, watched: true },
    ],

    exclude: [],
    preprocessors: {
      // add webpack as preprocessor
      'app/test.spec.ts': ['webpack']
    },

    webpack: {
      // karma watches the test entry points
      // (you don't need to specify the entry option)
      // webpack watches dependencies

      // webpack configuration
      resolve: {
        //root: [ path.join(__dirname, 'app') ],
        extensions: ['', '.ts', '.js']
      },
      module: {
        loaders: [
          // .ts files for TypeScript
          { test: /\.ts$/, loaders: ['awesome-typescript-loader', 'angular2-template-loader'], exclude: '/node_modules/' },
        ]
      }
    },
    reporters: ['progress', 'kjhtml'],

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  })
}

运行karma start 时未找到任何测试。浏览器将启动,但它说它找到了 0 个测试。当我将 .ts 扩展名更改为 .js 并更新 karma 配置文件时,它确实可以工作,所以很明显是 Typescript 搞砸了。

但是,当使用上面的配置手动运行 webpack 时,它只会输出一个正确的 javascript 文件,所以配置看起来确实没问题...

完整地说,这些是package.jsontsconfig.json 文件:

package.json

{
  "name": "karma-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "karma": "karma init"
  },
  "author": "",
  "license": "ISC",
  "private": true,
  "devDependencies": {
    "@types/jasmine": "^2.5.41",
    "angular2-template-loader": "^0.6.0",
    "awesome-typescript-loader": "^3.0.0-beta.18",
    "jasmine-core": "^2.5.2",
    "karma": "^1.4.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-jasmine": "^1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-webpack": "^2.0.1",
    "typescript": "^2.1.5",
    "webpack": "^1.14.0"
  }
}

tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "awesomeTypescriptLoaderOptions": {
        "useWebpackText": true
    },
    "exclude": [
        "node_modules"
    ]
}

【问题讨论】:

标签: typescript webpack karma-runner karma-jasmine


【解决方案1】:

我也遇到过这个问题。我最终切换到karma-typescript,它可以完美地运行测试。

【讨论】:

  • 感谢您的建议,但由于其他原因,我们需要使用 Webpack
  • 我们做到了,但现在不在电脑后面。我会在今天晚些时候尝试发布我们的解决方案。
  • 嘿@Robba,你还记得最终是什么问题以及你是如何解决的吗?
【解决方案2】:

您必须为 Typescript 文件添加 mime 类型。否则,Chrome 将不会运行这些文件。

mime: {
  'text/x-typescript': ['ts']
}

【讨论】:

  • 我们需要在哪里添加mime类型? (哪个文件)
  • 业力配置文件
猜你喜欢
  • 2016-06-20
  • 2013-04-08
  • 2019-06-17
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 2017-01-13
  • 2015-09-16
相关资源
最近更新 更多