gpt4 book ai didi

typescript - 使用 Angular2 : Unexpected anonymous System. 注册调用进行 karma 测试

转载 作者:太空狗 更新时间:2023-10-29 18:00:05 25 4
gpt4 key购买 nike

当我在 Angular2 中运行 Karma 测试时,出现以下错误。

未捕获的类型错误:意外的匿名 System.register 调用。在 http://localhost:9876/base/node_modules/systemjs/dist/system.src.js?38538ebca96bc7b222f7e7ba15943f173a485f6e:2885

我想这是因为 var System 当前不可用。即使它实际加载。有谁知道为什么这不起作用?

这是我的精简配置:

C:\project\karma.conf.js

module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'node_modules/systemjs/dist/system.src.js',
'node_modules/angular2/bundles/angular2.dev.js',
'tests/**/*Spec.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome', 'Firefox'],
singleRun: false,
concurrency: Infinity
})
}

C:\project\tests\MyTestSpec.ts这一行足以触发错误。

import {describe, expect, it, xit, inject, beforeEachProviders} from 'angular2/testing';

呈现给C:\project\tests\MyTestSpec.js并在第一次出现 System 时产生错误

System.register([], function(exports_1) {
return {
setters:[],
execute: function() {
}
}
});
//# sourceMappingURL=CmiCloudSpec.js.map

C:\project\tsconfig.json

{
"compilerOptions": {
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"watch": true,
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}

C:\project\src\node_modules

包含文件夹 angular2 和 systemjs 以及 karma、karma-jasmine、jasmine-core 和 typescript

最佳答案

问题是您应该将所有测试文件直接包含在karma.conf.jsfiles 属性中的Karma 中。因为您没有在 tsc 配置中定义 outFile 属性(这里没问题),所以模块的名称不会指定到编译的 JS 文件中。

您需要创建一个 karma-test-shim.js 文件(将包含在内)并配置 SystemJS 以按文件名加载模块并使用 System.import 导入这些文件

这是 karma-test-shim.js 文件的示例:

Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

__karma__.loaded = function() {};

System.config({
packages: {
'base/dist': {
defaultExtension: false,
format: 'cjs',
map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
}
}
});

System.import('angular2/src/platform/browser/browser_adapter')
.then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
.then(function() { return Promise.all(resolveTestFiles()); })
.then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });

function createPathRecords(pathsMapping, appPath) {
var pathParts = appPath.split('/');
var moduleName = './' + pathParts.slice(Math.max(pathParts.length - 2, 1)).join('/');
moduleName = moduleName.replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
return pathsMapping;
}

function onlyAppFiles(filePath) {
return /\/base\/dist\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
}

function onlySpecFiles(path) {
return /\.spec\.js$/.test(path);
}

function resolveTestFiles() {
return Object.keys(window.__karma__.files)
.filter(onlySpecFiles)
.map(function(moduleName) {
return System.import(moduleName);
});
}

以及karma.conf.js中对应的配置:

module.exports = function(config) {
config.set({
basePath: '.',
frameworks: ['jasmine'],
files: [
// paths loaded by Karma
{pattern: 'node_modules/angular2/bundles/angular2-polyfills.js', included: true, watched: true},
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
{pattern: 'node_modules/rxjs/bundles/Rx.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/angular2.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/http.dev.js', included: true, watched: true},
{pattern: 'node_modules/angular2/bundles/testing.dev.js', included: true, watched: true},
{pattern: 'karma-test-shim.js', included: true, watched: true},

// paths loaded via module imports
{pattern: 'dist/**/*.js', included: false, watched: true},

// paths to support debugging with source maps in dev tools
{pattern: 'src/**/*.ts', included: false, watched: false},
{pattern: 'dist/**/*.js.map', included: false, watched: false}
],
(...)
});
};

认为本文及其源代码可以帮助到您:

关于typescript - 使用 Angular2 : Unexpected anonymous System. 注册调用进行 karma 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36035820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com