尝试运行单元测试时出现以下错误。
拒绝从“ 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"
]
...