2016-09-07 100 views
0

我正在嘗試調試用Jasmine編寫的,由Karma運行的JS單元測試。如何在運行我的測試時在Intellij中的測試中放置斷點?我該如何執行單個測試?我該如何在Intellij或Webstorm中使用Karma&Jasmine調試JS單元測試

這裏是我的IntelliJ的運行配置爲執行噶測試

Here's my Intellij's Karma TEST Run Config

這裏是我的榜樣單元測試

import { 
    RouterTestingModule 
} from '@angular/router/testing'; 
import { 
    async, 
    TestBed, 
    ComponentFixture 
} from '@angular/core/testing'; 
import { provideRoutes, Routes, RouterModule } from '@angular/router'; 
import { Component } from '@angular/core'; 

import { AppComponent } from './app.component'; 
import { NavbarComponent } from './shared/navbar/navbar.component'; 

@Component({ 
    selector: 'as-test-cmp', 
    template: '<div class="title">Hello test</div>' 
}) 
class TestRouterComponent { 
} 

let config: Routes = [ 
    { 
     path: '', component: TestRouterComponent 
    } 
]; 

describe('AppComponent',() => { 
    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      declarations: [ 
       TestRouterComponent, 
       AppComponent, 
       NavbarComponent 
      ], 
      imports: [ RouterTestingModule, RouterModule ], 
      providers: [ provideRoutes(config) ] 
     }); 
    }); 

    it('should have title Hello world', async(() => { 
     TestBed.compileComponents().then(() => { 
      let fixture: ComponentFixture<AppComponent>; 
      fixture = TestBed.createComponent(AppComponent); 
      fixture.detectChanges(); 

      let compiled = fixture.debugElement.nativeElement; 
      expect(compiled).toBeDefined(); 
      // TODO: find a way to compile the routed component 
      // expect(compiled.querySelector('div.title')).toMatch('Hello world'); 
     }); 
    })); 
}); 

這裏是我的karma.conf.js

module.exports = function (config) { 
    var gulpConfig = require('../gulp/config')(); 

    /** 
    * List of npm packages that imported via `import` syntax 
    */ 
    var dependencies = [ 
     '@angular', 
     'lodash', 
     'rxjs', 
     'moment' 
    ]; 

    var configuration = { 
     basePath: '../../', 

     frameworks: ['jasmine'], 
     browsers: ['PhantomJS'], 
     reporters: ['progress', 'coverage'], 

     preprocessors: {}, 

     // Generate json used for remap-istanbul 
     coverageReporter: { 
      dir: 'report/', 
      reporters: [ 
       {type: 'json', subdir: 'report-json'} 
      ] 
     }, 

     files: [ 
      'node_modules/core-js/client/shim.min.js', 
      'node_modules/zone.js/dist/zone.js', 
      'node_modules/zone.js/dist/long-stack-trace-zone.js', 
      'node_modules/zone.js/dist/proxy.js', 
      'node_modules/zone.js/dist/sync-test.js', 
      'node_modules/zone.js/dist/jasmine-patch.js', 
      'node_modules/zone.js/dist/async-test.js', 
      'node_modules/zone.js/dist/fake-async-test.js', 
      'node_modules/systemjs/dist/system.src.js', 
      'node_modules/moment/moment.js' 
     ], 

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

     port: 9876, 
     colors: true, 
     logLevel: config.LOG_INFO, 
     autoWatch: true 
    }; 

    configuration.preprocessors[gulpConfig.tmpApp + '**/!(*.spec)+(.js)'] = ['coverage']; 
    configuration.preprocessors[gulpConfig.tmpApp + '**/*.js'] = ['sourcemap']; 
    configuration.preprocessors[gulpConfig.tmpTest + '**/*.js'] = ['sourcemap']; 

    var files = [ 
     gulpConfig.tmpTest + 'test-helpers/global/**/*.js', 
     gulpConfig.src + 'systemjs.conf.js', 
     'config/test/karma-test-shim.js', 
     createFilePattern(gulpConfig.tmpApp + '**/*.js', {included: false}), 
     createFilePattern(gulpConfig.tmpTest + 'test-helpers/*.js', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.html', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.css', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.ts', {included: false, watched: false}), 
     createFilePattern(gulpConfig.tmpApp + '**/*.js.map', {included: false, watched: false}) 
    ]; 

    configuration.files = configuration.files.concat(files); 

    dependencies.forEach(function (key) { 
     configuration.files.push({ 
      pattern: 'node_modules/' + key + '/**/*.js', 
      included: false, 
      watched: false 
     }); 
    }); 

    if (process.env.APPVEYOR) { 
     configuration.browsers = ['IE']; 
     configuration.singleRun = true; 
     configuration.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough 
    } 

    config.set(configuration); 

    // Helpers 
    function createFilePattern(path, config) { 
     config.pattern = path; 
     return config; 
    } 
} 
module.exports = function (config) { 
    var gulpConfig = require('../gulp/config')(); 

    /** 
    * List of npm packages that imported via `import` syntax 
    */ 
    var dependencies = [ 
     '@angular', 
     'lodash', 
     'rxjs', 
     'moment' 
    ]; 

    var configuration = { 
     basePath: '../../', 

     frameworks: ['jasmine'], 
     browsers: ['PhantomJS'], 
     reporters: ['progress', 'coverage'], 

     preprocessors: {}, 

     // Generate json used for remap-istanbul 
     coverageReporter: { 
      dir: 'report/', 
      reporters: [ 
       {type: 'json', subdir: 'report-json'} 
      ] 
     }, 

     files: [ 
      'node_modules/core-js/client/shim.min.js', 
      'node_modules/zone.js/dist/zone.js', 
      'node_modules/zone.js/dist/long-stack-trace-zone.js', 
      'node_modules/zone.js/dist/proxy.js', 
      'node_modules/zone.js/dist/sync-test.js', 
      'node_modules/zone.js/dist/jasmine-patch.js', 
      'node_modules/zone.js/dist/async-test.js', 
      'node_modules/zone.js/dist/fake-async-test.js', 
      'node_modules/systemjs/dist/system.src.js', 
      'node_modules/moment/moment.js' 
     ], 

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

     port: 9876, 
     colors: true, 
     logLevel: config.LOG_INFO, 
     autoWatch: true 
    }; 

    configuration.preprocessors[gulpConfig.tmpApp + '**/!(*.spec)+(.js)'] = ['coverage']; 
    configuration.preprocessors[gulpConfig.tmpApp + '**/*.js'] = ['sourcemap']; 
    configuration.preprocessors[gulpConfig.tmpTest + '**/*.js'] = ['sourcemap']; 

    var files = [ 
     gulpConfig.tmpTest + 'test-helpers/global/**/*.js', 
     gulpConfig.src + 'systemjs.conf.js', 
     'config/test/karma-test-shim.js', 
     createFilePattern(gulpConfig.tmpApp + '**/*.js', {included: false}), 
     createFilePattern(gulpConfig.tmpTest + 'test-helpers/*.js', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.html', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.css', {included: false}), 
     createFilePattern(gulpConfig.app + '**/*.ts', {included: false, watched: false}), 
     createFilePattern(gulpConfig.tmpApp + '**/*.js.map', {included: false, watched: false}) 
    ]; 

    configuration.files = configuration.files.concat(files); 

    dependencies.forEach(function (key) { 
     configuration.files.push({ 
      pattern: 'node_modules/' + key + '/**/*.js', 
      included: false, 
      watched: false 
     }); 
    }); 

    if (process.env.APPVEYOR) { 
     configuration.browsers = ['IE']; 
     configuration.singleRun = true; 
     configuration.browserNoActivityTimeout = 90000; // Note: default value (10000) is not enough 
    } 

    config.set(configuration); 

    // Helpers 
    function createFilePattern(path, config) { 
     config.pattern = path; 
     return config; 
    } 
} 
+0

要執行單個測試,您應該使用「fit」而不是「it」。目前這是因爲業力加載整個文件的唯一途徑。如果你只想運行一個套件,那麼你可以使用「fdescribe」。 – TypeScripter

+0

你知道如何去做嗎?我面對完全相同的問題,我嘗試過的所有東西都不起作用。這似乎是一個正常的期望,能夠做到這一點。 – Julian

回答

0

你可以在您的瀏覽器上調試。只需點擊DEBUG按鈕,找到你的班級並在你想要的地方添加一個斷點。要再次運行它,只需刷新頁面即可。

+1

當Karma執行時,我知道該怎麼做。我希望能夠像在其他語言中那樣在我的IDE中進行調試。 – user6123723

1

上述答案是正確的,但在此之前,你必須安裝鉻擴展Jetbrains IDE support plugin,然後你可以在你的WebStorm進行調試。

相關問題