导入 Angular 2 测试 类 ComponentFixture & TestBed 导致 404 错误

Importing the Angular 2 testing classes ComponentFixture & TestBed causing 404 error

只有当我尝试 运行 我的 jasmine 测试时才会出现此错误。我 运行 他们有一个规范 运行ner html 文件。

zone.js:1382 GET http://localhost:3002/node_modules/@angular/core/bundles/core.umd.js/testing 404 (Not Found)

zone.js:232 Error: (SystemJS) XHR error (404 Not Found)

如果我只是 运行 我的应用程序,它工作正常。

我正在尝试 运行 导入 angular 2 个测试模块的规范文件:

import { ComponentFixture, TestBed } from '@angular/core/testing'; 这就是导致错误的原因。

然而,该行正是 angular 2 testing documentation

中的内容

我的systemjs.config.js也和里面的一模一样 angular 2 documentation.

问题是 angular 测试模块在 @angular/core 中,所以 systemjs.config.js 这一行应该解决这个问题:

map: {
      // our app is within the app folder
      app: 'js/app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',

然而,这是 node_modules@angular 的文件结构,我还单击了 "index.js" 向您展示了导出 类 的文件我想在规范测试中使用:

但是浏览器显示它在导入过程中正在寻找这个:

zone.js:1382 GET http://localhost:3002/node_modules/@angular/core/bundles/core.umd.js/testing 404 (Not Found)

显然 core.umd.js 文件中不能有文件或文件夹,所以很容易看出这是错误的。

我想我可能希望它正在寻找这个:

http://localhost:3002/node_modules/@angular/core/testing/index.js

那么我该如何让它这样做呢?在systemjs.config.js中,我肯定会对这一行做些什么?

map: {
  // our app is within the app folder
  app: 'js/app',
  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',

或者在它之后添加另一个映射,专门用于 @angular/core 中的测试模块?

我试过这个:

map: {
  // our app is within the app folder
  app: 'js/app',
  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
  '@angular/core/testing': 'npm:@angular/core/testing/index.js',

这个我也试过了(看最下面的包):

packages: {
  app: {
    main: 'main.js',
    defaultExtension: 'js'
  },
  "node_modules/ng2-bs3-modal": {
    defaultExtension: 'js' 
  },
  'node_modules/angular2-google-maps/core': { 
    defaultExtension: 'js', 
    main: 'index.js'
  },
  '@angular/core/testing': {
    main:'npm:@angular/core/testing/index.js'
  },

但两者都没有work/cause其他问题。

我该如何解决这个错误?

我的 systemjs.config.js 文件:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'js/app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
    'angular2-google-maps': 'npm:angular2-google-maps',
    'angular2-google-map-auto-complete' : 'npm:angular2-google-map-auto-complete', 
    'ng2-bs3-modal': 'npm:ng2-bs3-modal',
    "ng2-popover": "npm:ng2-popover",
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: 'main.js',
        defaultExtension: 'js'
      },
      "node_modules/ng2-bs3-modal": {
        defaultExtension: 'js' 
      },
      'node_modules/angular2-google-maps/core': { 
        defaultExtension: 'js', 
        main: 'index.js'
      },
      '@angular/core/testing': {
        main:'npm:@angular/core/testing/index.js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

我的 jasmine 规格 运行ner html 文件:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html;charset=utf-8">
  <title>Vepo Unit Tests</title>
  <link rel="stylesheet" href="node_modules/jasmine-core/lib/jasmine-core/jasmine.css">
  <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine.js"></script>
  <script src="node_modules/jasmine-core/lib/jasmine-core/jasmine-html.js"></script>
  <script src="node_modules/jasmine-core/lib/jasmine-core/boot.js"></script>
</head>
<body>
  <!-- #1. add the system.js and angular libraries -->
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>
  <script src="systemjs.config.js"></script>
  <script>
    Promise.all([
      System.import('js/app/landing-page/landing-page.component.spec.js'),
      System.import('js/app/pipes/my-uppercase.pipe.spec.js'),
      System.import('js/app/landing-page/subcomponents/middle-row.component.spec.js')
    ]).then(window.onload)
      .catch(console.error.bind(console));
  </script>
</body>

</html>

我的规格文件:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';

import { MiddleRowComponent } from './middle-row.component';

let comp: MiddleRowComponent;
let fixture: ComponentFixture<MiddleRowComponent>;
let de: DebugElement;
let el: HTMLElement;

describe('MiddleRowComponent', () => {
   beforeEach(() => {
      TestBed.configureTestingModule({
         declarations: [MiddleRowComponent], // declare the test component
      });

      fixture = TestBed.createComponent(MiddleRowComponent);

      comp = fixture.componentInstance; // MiddleRowComponent test instance

      // query for the title <h1> by CSS element selector
      de = fixture.debugElement.query(By.css('h1'));
      el = de.nativeElement;
   });

   it('should display original title', () => {
      fixture.detectChanges();
      expect(el.textContent).toContain(comp.word);
   });

   it('should display a different test title', () => {
      comp.word = 'Test Title';
      fixture.detectChanges();
      expect(el.textContent).toContain('Test Title');
   });
});

@angular/core/testing/package.json 里面有这个代码:

{"main": "../bundles/core-testing.umd.js"}

所以我将其添加到 systemjs.config.js 映射中:

'@angular/core/testing': "npm:@angular/core/bundles/core-testing.umd.js"

例如:

map: {
  // our app is within the app folder
  app: 'js/app',
  // angular bundles
  '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
  '@angular/core/testing': "npm:@angular/core/bundles/core-testing.umd.js",

而且有效。