当前位置: 首页 > 知识库问答 >
问题:

Karma无法加载导入的文件

利俊迈
2023-03-14

我已经挣扎了好几天了,因为我无法用业力进行任何真正的测试。我可以运行不需要导入的测试(如基本健全性测试),但一旦我必须从我的应用程序导入某些内容,我就会收到错误:

系统src。js:1085获取http://localhost:9876/base/dist/components/test.service404(未找到)fetchTextFromURL@system。src。js:1085(匿名函数)@system。src。js:1646ZoneAwarePromise@angular2 polyfills。js:589(匿名函数)@system。src。js:1645(匿名函数)@system。src。js:2667(匿名函数)@system。src。js:3239(匿名函数)@system。src。js:3506(匿名函数)@system。src。js:3888(匿名函数)@system。src。js:4347(匿名函数)@system。src。js:4599(匿名函数)@system。src。js:337ZoneDelegate。调用@angular2 polyfills。js:332Zone。运行@angular2 polyfills。js:227(匿名函数)@angular2 polyfills。js:576ZoneDelegate。invokeTask@angular2 polyfills。js:365Zone。runTask@angular2 polyfills。js:263drainMicroTaskQueue@angular2 polyfills。js:482ZoneTask。调用@angular2 polyfills。js:434棱角2多边形填充。js:469未处理的promise拒绝:因果报应。错误不是一个函数;区域:;任务:promise。然后Value:TypeError:karma。错误不是函数(…)consoleError@angular2 polyfills。js:469drainMicroTaskQueue@angular2 polyfills。js:498ZoneTask。调用@angular2 polyfills。js:434棱角2多边形填充。js:471错误:未捕获(在promise中):类型错误:因果报应。错误不是函数(…)

我需要提到:示例测试:

import { provide } from 'angular2/core';
import {TestService} from './test.service';
import {
//   beforeEach,
  beforeEachProviders,
  describe,
  expect,
  it,
  inject,
//   injectAsync
} from 'angular2/testing';

 describe('TestService', () => {

  beforeEachProviders(() => [
    provide(TestService, {useClass: TestService})
  ]);
  it('should say hello with name', inject([TestService], (testService: TestService) => {
    expect(testService.name).toBe('Injected Service');
  }));

  it('should say hello with name', () => {
    expect(true).toBe(true);
  });

});

我的项目结构是基于多模块的,如:

因果报应。conf.js:

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.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 },
    { pattern: 'dist/components/matchers.js', included: true, watched: true },

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

    // paths loaded via Angular's component compiler
    // (these paths need to be rewritten, see proxies section)
    { pattern: 'dist/*.html', included: false, watched: true },
    { pattern: 'dist/styles/*.css', included: false, watched: true },
    { pattern: 'dist/components/**/*.html', included: false, watched: true },
    { pattern: 'dist/components/**/*.css', included: false, watched: true },

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

// proxied base paths
proxies: {
    // required for component assests fetched by Angular's compiler
    "/src/": "/base/src"
}, 

业力测试垫片。js:

...

System.config({
    packages: {
        'base/src': {
            defaultExtension: false,
            format: 'register',
            map: Object.keys(window.__karma__.files).
                filter(onlyAppFiles).
                reduce(function createPathRecords(pathsMapping, appPath) {
                    // creates local module name mapping to global path with karma's fingerprint in path, e.g.:
                    // './hero.service': '/base/src/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e'
                    var moduleName = appPath.replace(/^\/base\/src\//, './').replace(/\.js$/, '');
                    pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]
                    return pathsMapping;
                }, {})

        }
    }
});


function filePath2moduleName(filePath) {
    console.log('filePath2moduleName', filePath)
    return filePath.
        replace(/^\//, '').              // remove / prefix
        replace(/\.\w+$/, '');           // remove suffix
}


function onlyAppFiles(filePath) {
    console.log('filePath', filePath)
    return /^\/base\/src\/.*\.js$/.test(filePath)
}


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

欢迎任何意见!

共有1个答案

濮阳俊明
2023-03-14

如果您将TS文件编译到dist文件夹中,我认为您有一个缺口。我会在你的业力测试中尝试以下内容。js文件:

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

(...)

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;
}

createPathRecords函数中的下一行似乎很奇怪,因为您不应该拥有来自src的文件,而应该拥有来自dist的文件。

通过这段代码,我生成了以下SystemJS配置(mapblock):

System.config({
  packages: {
    'base/dist': {
      defaultExtension: false,
      format: 'register',
      map: {
        './comps/my-list': '/base/dist/comps/my-list.js?e9(...)',
        './dist/my-app': '/base/dist/my-app.js?fe(...)',
        './pipes/my-pipe': '/base/dist/pipes/my-pipe.js?78(...)',
        './services/http-service': '/base/dist/services/http-service.js?c1(...)',
        './services/my-service': '/base/dist/services/my-service.js?b1(...)'
      }
    }
  }
});

在您的情况下/base/dist/组件/test.service不能由SystemJS解决,所以我猜测要么您的map块不正确,要么是的条目。

请注意,map块的条目与packagesone的键相关。在您的情况下,我猜您将生成以下配置:

System.config({
  packages: {
    'base/src': {
      defaultExtension: false,
      format: 'register',
      map: {
        './components/test.service': '/base/dist/components/test.service.js',
        (...)
      }
    }
  }
});

为了调试您的问题,我将执行以下操作:

>

  • createPathRecords函数中记录appPath

    reduce(function createPathRecords(pathsMapping, appPath) {
      console.log('appPath = '+appPath);
      (...)
    }
    

    记录整个map配置:

    console.log(Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {}));
    

    根据这些提示,您需要调整代码以实现以下配置:

    {
      './components/test.service': '/base/dist/components/test.service.js',
      (...)
    }
    

  •  类似资料:
    • 问题内容: 当我使用pydot运行非常简单的代码时 它向我显示错误消息: 我正在使用python 2.7.3 问题答案: 回答: (上游)的不兼容性已由6dff94b3f1修复,因此`pydot = 1.1pyparsing >= 1.5.7`](https://github.com/erocarrera/pydot/commit/e26af21426fcf15955f7b11b348b14dc6

    • 问题内容: 我真的不想对此提出自己的问题,因为这似乎是一个常见错误。但是,由于现在已经浪费了数小时并且关注了我所能找到的每个线程,因此给出的答案都没有为我解决这个问题。 因此,我唯一的选择是提供我所能提供的有关我的设置的所有信息,并希望你们中的一个能认识到问题。 我正在Windows 10(x64)上运行,并从此处的集合中安装了以下预构建的二进制文件。 python 3.5.0-win32(从py

    • 我正在使用openpyxl导入/导出xlsx文件。 但是我不能导入我用openpyxl导出的文件。 我必须在Excel中打开导出的xlsx文件,更改导入的xlsx文件的一些值并保存它(它的大小也会改变(例如:从148 Ko到180 Ko)。然后我可以用openpyxl导入它。 我认为,有一个问题,在出口,因为后保存手动导入作品。 谢谢

    • 我试图从djangotoolbox.fields使用Listfield,但它给我一个错误说: 我做错了什么?

    • 问题内容: 我有一个使用Karma + Jasmine进行测试的AngularJS应用程序。我有一个要测试的函数,该函数需要一个大型JSON对象,然后将其转换为应用程序的其余部分更易使用的格式,然后返回该转换后的对象。而已。 对于我的测试,我希望您有单独的JSON文件(* .json),仅包含模拟JSON内容- 没有脚本。对于测试,我希望能够加载JSON文件并将对象泵入要测试的功能中。 我知道我可

    • 问题内容: 我最近决定从Apache2切换到Nginx。我在CentOS服务器上安装了Nginx并设置了基本配置。当我尝试在浏览器(FF / Chrome)中加载网站时,我注意到未加载CSS文件。我检查了错误控制台,并看到以下消息: 我检查了Nginx的配置,一切似乎都很好: 在/etc/nginx/mime.types中正确设置了css文件的mime类型。 一切似乎都配置正确,但是我的css文件