我如何关注jasmine.js中的一个规范?

我从一个相当大的体系结构变化中获得了一堆失败的规范。 我想通过用“焦点”标记每一个来逐一修复它们。

jasmine.js有这样的function吗? 我发誓我曾经读过,但是我没有在文档中看到它。

您可以使用规范的url来运行单个规格

describe("MySpec", function() { it('function 1', function() { //... }) it('function 2', function() { //... } }) 

现在你可以通过这个url http://localhost:8888?spec=MySpec来运行整个规范,并且使用http://localhost:8888?spec=MySpec+function+1

当使用业力时,你可以用iitddescribe启用一个testing。

(或者根据alxndr的评论,自从Jasmine 2.1: fitfdescribe

这只能运行Spec1

 ddescribe('Spec1', function () { it('should do something', function () { ... }); }); describe('Spec2', function () { it('should do something', function () { ... }); }); 

这只运行testA

 describe('Spec1', function () { iit('testA', function () { ... }); it('testB', function () { ... }); }); 

从2.1以来的核心与fitfdescribe

对于任何人来说,一个更好的方法,你可以从代码本身设置,是使用这个插件: https : //github.com/davemo/jasmine-only

它允许你在代码中设置规范排他性:

 describe.only("MySpec", function() { it('function 1', function() { //... }) it.only('function 2', function() { //... } }) // This won't be run if there are specs using describe.only/ddescribe or it.only/iit describe("Spec 2", function(){}) 

有一个长时间的讨论,把它添加到茉莉花的核心,请参阅: https : //github.com/pivotal/jasmine/pull/309

如果你恰巧通过Karma / Testacular使用Jasmine,你应该已经可以访问ddescribe()iit()

有几种方法可以做到这一点。

有:茉莉花的特点聚焦规格 (2.2): http : //jasmine.github.io/2.2/focused_specs.html

聚焦规格将使其成为唯一运行的规格。 任何适合声明的规格都是重点。

 describe("Focused specs", function() { fit("is focused and will run", function() { expect(true).toBeTruthy(); }); it('is not focused and will not run', function(){ expect(true).toBeFalsy(); }); }); 

但是,我不太喜欢编辑我的testing(fit和fdescribe)来select性地运行它们的想法。 我更喜欢使用像karma这样的testing运行器,它可以使用正则expression式过滤掉testing。

这里是一个使用grunt的例子。

 $ grunt karma:dev watch --grep=mypattern 

如果你正在使用gulp (这是我最喜欢的任务跑步者),你可以通过设置karma的configuration将args传递给yargs的gulp -karma并匹配模式。

有点像这样:

 var Args = function(yargs) { var _match = yargs.m || yargs.match; var _file = yargs.f || yargs.file; return { match: function() { if (_match) { return {args: ['--grep', _match]} } } }; }(args.argv); var Tasks = function() { var test = function() { return gulp.src(Files.testFiles) .pipe(karma({ configFile: 'karma.conf.js', client: Args.match()})) .on('error', function(err) { throw err; }); }; return { test: function() { return test() } } }(Args); gulp.task('default', ['build'], Tasks.test); 

看我的要点: https : //gist.github.com/rimian/0f9b88266a0f63696f21

所以现在,我可以使用描述来运行一个单一的规范:

我的本地testing运行:(执行14(跳过13))

 gulp -m 'triggers the event when the API returns success' [20:59:14] Using gulpfile ~/gulpfile.js [20:59:14] Starting 'clean'... [20:59:14] Finished 'clean' after 2.25 ms [20:59:14] Starting 'build'... [20:59:14] Finished 'build' after 17 ms [20:59:14] Starting 'default'... [20:59:14] Starting Karma server... INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome WARN [watcher]: All files matched by "/spec/karma.conf.js" were excluded. INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket hivjQFvQbPdNT5Hje2x2 with id 44705181 Chrome 42.0.2311 (Mac OS X 10.10.3): Executed 1 of 14 (skipped 13) SUCCESS (0.012 secs / 0.009 secs) [20:59:16] Finished 'default' after 2.08 s 

另见: https : //github.com/karma-runner/karma-jasmine

你可以在前面创build你的所有规格,但用xdescribexit禁用它们,直到你准备好testing它们为止。

 describe('BuckRogers', function () { it('shoots aliens', function () { // this will be tested }); xit('rescues women', function () { // this won't }); }); // this whole function will be ignored xdescribe('Alien', function () { it('dies when shot', function () { }); }); 

通过独立的Jasmine(2.0.0),在spec_runner.htlm上,我可以点击一个特定的规范并专注于这个规范。 我应该早点注意到这个function。

不完全是你所要求的,但添加iit将只testing特定的规格,并忽略文件中的所有其他人, ddescribe以同样的方式工作。 所以你可以使用iitddescribe来关注特定的规范