尝试运行单元测试时出现以下错误。
拒绝执行来自' http://localhost:9876/base/tests/test.ts'的脚本因为它的MIME类型(' video / mp2t')不可执行。
问题在于业力是使用Content-Type:video/mp2t
为test.ts提供服务而Chrome v55 +拒绝执行该脚本。
我在karma.conf.js中设置mime属性如下:
mime: {
'text/x-typescript': ['ts', 'tsx'],
'text/losingmyf@#kingmime': ['js']
}
当测试运行时,Karma都不使用mime类型。在设置了mime设置后,有什么可能会改变它吗?
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"
]
...
答案 0 :(得分:0)
经过几个小时的调试业力后,我追踪了一个名为mime
的包,其中karma用来为文件设置Content-Type
。
我安装的版本是版本1.3.5
。我将软件包更新为版本1.3.6
,我的问题已解决。
我后来在karma的github页面上找到了这个@ https://github.com/karma-runner/karma/issues/2702的问题。可能应该先在那里开始:)