Javascript 因果报应不执行任何测试

Javascript 因果报应不执行任何测试,javascript,angular,typescript,karma-jasmine,Javascript,Angular,Typescript,Karma Jasmine,我有一个问题,让任何测试运行,现在我有业力开始没有任何问题。我可以在命令行中看到确认我的规范文件,但是,没有运行任何测试,看到“Chrome 53.0.2785(Windows 7 0.0.0):执行0/0错误(0.005秒/0秒)”: 项目结构: app/ ├── common/ ├── components/ | └── header/ | ├── header.component.css | ├── header.component.html | └

我有一个问题,让任何测试运行,现在我有业力开始没有任何问题。我可以在命令行中看到确认我的规范文件,但是,没有运行任何测试,看到“Chrome 53.0.2785(Windows 7 0.0.0):执行0/0错误(0.005秒/0秒)”:

项目结构:

app/
├── common/
├── components/
|   └── header/
|       ├── header.component.css
|       ├── header.component.html
|       └── header.component.ts
test/
├── components/
|   └── header/
|       └── header.spec.ts
├── models/
└── services/
karma-test-shim.js
karma.conf.js
systemjs.config.js
src
└── app/
    ├── components/
    |       └── app.component.ts
    ├── models/
    └── services/
    main.ts
spec/
└── components/
    └── app.component.spec.ts
karma-test-shim.js
karma.conf.js
systemjs.config.js
packages.json(可能是因为试图让一切都顺利启动而受到污染):

karma-test-shim.js(这似乎很标准):

karma.conf.js:

// #docregion
module.exports = function(config) {

  var appBase    = 'app/';       // transpiled app JS and map files
  var appSrcBase = 'app/';       // app source TS files
  var appAssets  = 'app/'; // component assets fetched by Angular's compiler

  var testBase    = 'test/';       // transpiled test JS and map files
  var testSrcBase = 'test/';       // test source TS files

  config.set({
    basePath: '',
    frameworks: ['systemjs','jasmine'],
    plugins: [
      require('karma-systemjs'),
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-htmlfile-reporter'),
      require('karma-browserify'),
      require('browserify')
    ],

    customLaunchers: {
      // From the CLI. Not used here but interesting
      // chrome setup for travis CI using chromium
      Chrome_travis_ci: {
        base: 'Chrome',
        flags: ['--no-sandbox']
      }
    },
    files: [
      // System.js for module loading
      'node_modules/systemjs/dist/system-polyfills.js',
      'node_modules/systemjs/dist/system.src.js',

        // Reflect and Zone.js
      'node_modules/reflect-metadata/Reflect.js',

      'node_modules/zone.js/dist/zone.js',
      'node_modules/zone.js/dist/long-stack-trace-zone.js',
      'node_modules/zone.js/dist/async-test.js',
      'node_modules/zone.js/dist/fake-async-test.js',
      'node_modules/zone.js/dist/sync-test.js',
      'node_modules/zone.js/dist/proxy.js',
      'node_modules/zone.js/dist/jasmine-patch.js',

      // RxJs.
      { pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false },
      { pattern: 'node_modules/rxjs/**/*.js.map', included: false, watched: false },

      // Angular 2 itself and the testing library
      {pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
      {pattern: 'node_modules/@angular/**/*.js.map', included: false, watched: false},

      'karma-test-shim.js',


      'test/**/*spec.js'
    ],

    // Proxied base paths for loading assets
    proxies: {
      // required for component assets fetched by Angular's compiler
      "/app/": appAssets
    },

    exclude: [],
    preprocessors: {

    },

    reporters: ['progress', 'html'],

    // HtmlReporter configuration
    htmlReporter: {
      // Open this file to see results in browser
      outputFile: '_test-output/tests.html',

      // Optional
      pageTitle: 'Unit Tests',
      subPageTitle: __dirname
    },

    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  })
}
最后,systemjs.config.js:

(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/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      // other libraries
      'rxjs':                       'npm:rxjs',
      'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
    },
    // 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'
      },
      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

我知道这是一个沉重的负担-我希望我做了一些明显错误的事情。

我认为这是因为karma不像测试文件那样看待你的规范文件

当它告诉您已检测到更改时:

[1] 27 09 2016 16:06:10.194:INFO [watcher]: Changed file "C:/src/testingexample/test/components/header/header.spec.js".
这并不意味着它是一个测试文件

要告诉karma哪些文件是测试文件,请参见karma test shim.js

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);
它告诉karma加载所有文件,然后只保留那些文件

  • isSpecFile
    :以
    .spec.js
  • isbuildfile
    :构建路径下的内容
    /base/app/
在您的情况下,您的测试不在
app
目录中,因此您必须删除
.filter(isBuiltFile)
以便karma可以将您spec.js作为测试处理


我查看了我自己的业力测试项目,发现了一些不同之处

在我的karma.conf.js

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);
  • 我没有任何SystemJS插件

    frameworks: ['jasmine'],
    plugins   : [
        require('karma-jasmine'),
        require('karma-chrome-launcher')
    ],
    
  • 我已经编译了应用程序并将测试JS添加到
    文件中,以取代您的
    'test/***spec.JS'

    {pattern: appBase + '**/*.js', included: false, watched: true},
    {pattern: testBase + '**/*.js', included: false, watched: true},
    
    我不知道karma是否允许模式不使用
    {pattern:'test/***spec.js',…}

在mykarma test shim.js中

var allSpecFiles = Object.keys(window.__karma__.files)
  .filter(isSpecFile)
  .filter(isBuiltFile);
  • 我不使用systemjs.config.extras.js,因此我删除了所有相关代码

    • 希望能为我的解决方案做出贡献,希望将来有人会觉得它有用

      首先,我用这个开始了我的项目

      我想将测试放在以下结构的spec/文件夹中:

      app/
      ├── common/
      ├── components/
      |   └── header/
      |       ├── header.component.css
      |       ├── header.component.html
      |       └── header.component.ts
      test/
      ├── components/
      |   └── header/
      |       └── header.spec.ts
      ├── models/
      └── services/
      karma-test-shim.js
      karma.conf.js
      systemjs.config.js
      
      src
      └── app/
          ├── components/
          |       └── app.component.ts
          ├── models/
          └── services/
          main.ts
      spec/
      └── components/
          └── app.component.spec.ts
      karma-test-shim.js
      karma.conf.js
      systemjs.config.js
      
      为了做到这一点,我做了三个改变:

      1.-更改karma.config.js中的默认测试文件夹

      ...
      // Testing helpers (optional) are conventionally in a folder called `testing`
      var testingBase    = 'spec/'; // transpiled test JS and map files
      var testingSrcBase = 'spec/'; // test source TS files
      ...
      
      2.-运行
      npm测试时生成spec/文件夹,这将更改package.json文件:

      {
        "name": "my app",
        "version": "1.0.0",
        ...
        "scripts": {
          ...
          "build:test": "tsc -p spec/",
          ...
          "test": "concurrently \"npm run build:test\" \"npm run build:watch\" \"karma start karma.conf.js\"",
          ...
        }
        ...
      }
      
      3.-在spec/文件夹中创建tsconfig.json文件,信息如下:

      {
        "compilerOptions": {
          "target": "es5",
          "module": "commonjs",
          "moduleResolution": "node",
          "sourceMap": true,
          "emitDecoratorMetadata": true,
          "experimentalDecorators": true,
          "lib": [ "es2015", "dom" ],
          "noImplicitAny": true,
          "suppressImplicitAnyIndexErrors": true
        }
      }
      
      运行
      npm测试时,tsc将生成生成.js和.js.map的所有.ts文件。稍后,karma将在spec/文件夹中运行测试

      似乎测试运行了两次,但不确定是否与此配置相关或angular2如何工作

      希望这能帮助别人


      干杯。:)

      Karma是测试运行者,jasmine是编写单元测试的测试框架。如果没有其他帮助的话,我会在回家后提供答案。在我的情况下,业力/茉莉花的设置没有问题。我的规范文件有一个错误。当我修复错误时,测试按预期运行。希望它能帮助其他人。这完全有道理——当我第一次改变它时,我以为这是一个扣篮。我删除了isBuiltFile,只运行了isSpecFile筛选器-仍然得到0中的0。我想可能是我的正则表达式出了问题,我重新输入了它,仍然没有检测到任何测试。我将继续在这一领域中摆弄,因为它感觉不错-还有其他建议吗?您是否尝试在karma-test-shim.js底部的
      allSpecFiles.map
      中添加
      console.log(moduleName)
      ?也许您必须在systemjs.config.js中添加一些conf,因为您的测试不在
      app
      目录下。对于当前的配置,我认为SystemJS不知道如何从
      test/*
      module.console.log(字符串)加载文件,因为某种原因,垫片中的任何位置都无法输出文件-我开始认为这里有更深层次的错误。我查看了我自己的karma测试项目,发现了一些不同之处。我对我的回答进行了编辑,以获得更详细的解释。我希望它能对未来的任何人有所帮助,我解决了这个问题,从中提取了Karma配置和垫片,然后按照Noémi的建议删除isBuiltFile过滤器,并将文件夹从“testing”更改为“test”。多谢了,不必了。