0

尝试运行单元测试时出现以下错误。

拒绝从“ http://localhost:9876/base/tests/test.ts ”执行脚本,因为它的 MIME 类型(“video/mp2t”)不可执行。

问题是 karma 正在为 test.ts 提供服务,Content-Type:video/mp2t而 Chrome v55+ 拒绝执行脚本。

我将 karma.conf.js 中的 mime 属性设置为:

mime: {
  'text/x-typescript': ['ts', 'tsx'],
  'text/losingmyf@#kingmime': ['js']
}

运行测试时,Karma 不使用这两种 mime 类型。在此处设置后,有什么可能会更改 mime 设置吗?

业力.conf.js

module.exports = function (config) {
  const webpackConfig = require("./webpack.config")({ mode: 'test' });

  var appBase = 'app/';
  var appAssets = '/base/app/'; // component assets fetched by Angular's compiler

  const configuration = {
      basePath: '',
      frameworks: ["jasmine"],
      files: [
          { pattern: './dist-test/pretest.bundle.js', watched: false, served: true },
          { pattern: './tests/test.ts', watched: false },
          { pattern: appBase + '**/*.png', included: false, watched: false }
      ],
      preprocessors: {
          './tests/test.ts': ['webpack', 'sourcemap']
      },
      mime: {
          'text/x-typescript': ['ts', 'tsx'],
          'text/losingmyf@#kingmime': ['js']
      },

      // Proxied base paths for loading assets
      proxies: {
          // required for component assets fetched by Angular's compiler
          "/app/": appAssets,
          "/ClientSrc/app/": appAssets
      },

      exclude: [],
      // reporters: ['progress', 'kjhtml', 'dots', 'trx'],
      reporters: ['progress', 'kjhtml'],

      trxReporter: { outputFile: 'test-results.trx' },

      webpack: webpackConfig,
      webpackMiddleware: {
          stats: 'errors-only'
      },

      client: {
          // Set false to leave Jasmine Spec Runner output visible in browser
          clearContext: true
      },

      coverageIstanbulReporter: {
          reports: ['html', 'lcovonly'],
          fixWebpackSourcePaths: true
      },

      port: 9876,
      colors: true,
      logLevel: config.LOG_INFO,
      autoWatch: true,
      browsers: [
          "Chrome"
          // , "ChromeNoSandbox"
          // , "Firefox"
          // , "IE"
      ],

      // customLaunchers: {
      //     ChromeNoSandbox: {
      //         base: 'Chrome',
      //         flags: ['--no-sandbox']
      //     }
      // },

      singleRun: false,
      concurrency: Infinity
  };

  console.log('before', config.mime); // undefined

  config.set(configuration);

  console.log('after', config.mime); // same as mime setting above
};

我的 webpack.config 也有这个设置:

"resolve": {
  "extensions": [
    ".ts",
    ".js"
  ]
...
4

1 回答 1

1

经过数小时的 karma 调试,我找到了一个名为mimewhich karma used to set Content-Typefor a file 的包。

我安装的版本是 version 1.3.5。我将软件包更新为版本1.3.6,我的问题得到了解决。

后来我在 karma 的 github 页面上发现了这个问题 @ https://github.com/karma-runner/karma/issues/2702。可能应该先从那里开始:)

于 2017-07-14T14:26:53.697 回答