作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试调试用 Jasmine 编写并由 Karma 运行的 JS 单元测试。运行测试时如何在 Intellij 中的测试中放置断点?我如何执行单个测试?
这是我用于执行 Karma 测试的 Intellij 运行配置
这是我的示例单元测试
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;
}
}
最佳答案
上面的答案是正确的,但在此之前你必须安装 chrome 扩展 Jetbrains IDE support plugin , 然后你就可以在你的 WebStorm 中调试了。
关于javascript - 如何在 Intellij 或 Webstorm 中使用 Karma & Jasmine 调试 JS 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39377081/
我是一名优秀的程序员,十分优秀!