Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular2 karma测试将systemjs移动到子目录会停止评估;。js";延伸_Angular_Typescript_Karma Runner_Karma Jasmine - Fatal编程技术网

Angular2 karma测试将systemjs移动到子目录会停止评估;。js";延伸

Angular2 karma测试将systemjs移动到子目录会停止评估;。js";延伸,angular,typescript,karma-runner,karma-jasmine,Angular,Typescript,Karma Runner,Karma Jasmine,我使用了与此处完全相同的设置: 此处记录: 除了我将systemjs.config.js移动到一个子目录(我们称之为“src”),并更新了karma-test-shim.js和karma.conf.js以指向它。systemjsapp:'../app', 我运行karma start,除了jasmine测试本身,它都可以工作,它查找base/app/app.component,并返回404。如果我将import语句更改为具有“.js”,如下所示: 从'/app.component.js'导入

我使用了与此处完全相同的设置:

此处记录:

除了我将systemjs.config.js移动到一个子目录(我们称之为“src”),并更新了karma-test-shim.js和karma.conf.js以指向它。systemjs
app:'../app',

我运行
karma start
,除了jasmine测试本身,它都可以工作,它查找
base/app/app.component
,并返回404。如果我将import语句更改为具有“.js”,如下所示:
从'/app.component.js'导入{AppComponent}

它起作用了

当systemjs位于根目录上时,不会发生这种情况。我错过了什么

我没有对
tsconfig.json
进行任何更改,以防它发生问题。以下是quick starter中包含的文件:

{
 "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  },
  "exclude": [
    "node_modules/*",
    "**/*-aot.ts"
  ]
}
src/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: '../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/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.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'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);
.config/karma.conf.js(将其移动到.config不会产生不同的结果)

.config/karma-test-shim.js

// #docregion
// /*global jasmine, __karma__, window*/
Error.stackTraceLimit = 0; // "No stacktrace"" is usually best for app testing.

// Uncomment to get full stacktrace output. Sometimes helpful, usually not.
// Error.stackTraceLimit = Infinity; //

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

var builtPath = '/base/app/';

__karma__.loaded = function () { };

function isJsFile(path) {

  return path.slice(-3) == '.js';
}

function isSpecFile(path) {
  return /\.spec\.(.*\.)?js$/.test(path);
}

function isBuiltFile(path) {
  return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath);
}

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);

System.config({
  baseURL: 'base',
  // Extend usual application package list with test folder
  packages: { 'testing': { main: 'index.js', defaultExtension: 'js' } },

  // Assume npm: is set in `paths` in systemjs.config
  // Map the angular testing umd bundles
  map: {
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
    '@angular/forms/testing': 'npm:@angular/forms/bundles/forms-testing.umd.js',
  },
});

System.import('src/systemjs.config.js')
  .then(initTestBed)
  .then(initTesting);

function initTestBed(){
  return Promise.all([
    System.import('@angular/core/testing'),
    System.import('@angular/platform-browser-dynamic/testing')
  ])

  .then(function (providers) {
    var coreTesting    = providers[0];
    var browserTesting = providers[1];

    coreTesting.TestBed.initTestEnvironment(
      browserTesting.BrowserDynamicTestingModule,
      browserTesting.platformBrowserDynamicTesting());
  })
}

// Import all spec files and start karma
function initTesting () {
  return Promise.all(
    allSpecFiles.map(function (moduleName) {
      return System.import(moduleName);
    })
  )
  .then(__karma__.start, __karma__.error);
}
app.component.spec.ts

import { AppComponent } from './app.component'; // do i have to include .js?

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

////////  SPECS  /////////////
describe('AppComponent', function () {
  let de: DebugElement;
  let comp: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  beforeEach(async(() => {
   TestBed.configureTestingModule({
      declarations: [ AppComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    comp = fixture.componentInstance;
    de = fixture.debugElement.query(By.css('h1'));
  });

  it('should create component', () => expect(comp).toBeDefined() );

  it('should have expected <h1> text', () => {
    fixture.detectChanges();
    const h1 = de.nativeElement;
    expect(h1.innerText).toMatch(/angular/i,
      '<h1> should say something about "Angular"');
  });
});
从“/app.component”导入{AppComponent};//我必须包括.js吗?
从“@angular/core/testing”导入{async,ComponentFixture,TestBed};
从“@angular/platform browser”导入{By}”;
从“@angular/core”导入{DebugElement};
////////规格/////////////
描述('AppComponent',函数(){
设de:DebugElement;
让comp:AppComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[AppComponent]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(AppComponent);
comp=夹具。组件状态;
de=fixture.debugElement.query(By.css('h1');
});
它('should create component',()=>expect(comp.toBeDefined());
它('应该有预期的文本',()=>{
fixture.detectChanges();
常数h1=de.nativeElement;
expect(h1.innerText).toMatch(/angular/i,
'应该说一些关于“棱角的”');
});
});

发现了很多问题,主要是文件路径、karma配置和systemjs配置中的问题,但对于这个特定问题,却是systemjs中“app”的路径

app: '../app',
应该是相对于根目录而不是子文件夹的

app: 'app',

在karma配置和systemjs配置中发现了许多问题,主要是在文件路径中,但是对于这个特定问题,它是systemjs中“app”的路径

app: '../app',
应该是相对于根目录而不是子文件夹的

app: 'app',

我刚刚用systemjs原样再次尝试(没有修改app:'app',),它成功了!:/我不明白为什么或者怎么想我只是用systemjs按原样再次尝试(没有修改app:'app',),结果成功了!:/我不明白为什么或者怎么做