[英]How to import typescript files with karma-systemjs?
背景
我正在嘗試為Angular 1x應用程序設置測試運行器,但是我無法使Karma與Systemjs和Typescript一起使用。
測試用Typescript編寫,我們使用Systemjs導入使用ES6模塊的文件。 大多數文件不使用ES6模塊。
我沒有使用像karma-typescript這樣的karma預處理程序,而是嘗試使用Systemjs來轉換我的Typescript。 那是錯誤的路嗎? 似乎沒有發生任何轉譯!
錯誤
當我運行karma start
,腳本在輸入打字稿命名空間時失敗:
import core = myApp.core;
Chrome 52.0.2743 (Windows 7 0.0.0) ERROR
Error: (SystemJS) SyntaxError: Unexpected token import
Evaluating app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts
Error loading app/scripts/site/core/components/settingsMenu/settingsMenu.spec.ts
這是基於此示例的 karma.conf.js文件。
// Karma configuration
// Generated on Thurs Apr 13 2017
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['systemjs', 'jasmine'],
systemjs: {
configFile: 'config.js',
config: {
paths: {
'systemjs': 'app/vendor/systemjs/system.src.js',
'system-polyfills': 'app/vendor/systemjs/system-polyfills.src.js',
'typescript': '/node_modules/typescript/lib/typescript.js'
},
packages: {
'app': {
defaultExtension: 'ts'
}
},
transpiler: 'typescript'
},
serveFiles: [
'app/scripts/**/*!(spec).ts'
]
},
// list of files / patterns to load in the browser
files: [
{ pattern: 'app/scripts/**/*.spec.ts' }
],
// list of files to exclude
exclude: [],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
systemjs配置:
System.config({
baseURL: './',
defaultJSExtensions: true
});
嘗試在您的systemjs配置(config.js)中添加transpiler屬性:
System.config({
baseURL: './',
defaultJSExtensions: true,
transpiler: 'typescript'
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.