我如何在 Jasmine.js 中专注于一个规格?

我有一大堆失败的规格从一个相当大的架构变化。我想通过给每一个都贴上“专注”的标签来一个一个地修复它们。

Jasmine.js 有这样的功能吗?我发誓我曾经读到过,但是我没有在文件里看到。

101579 次浏览

您可以使用规范的 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进行第一次测试

您可以创建您的所有规格前,但禁用他们与 xdescribexit,直到您准备测试他们。

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 () {
});
});

对于任何碰到这个问题的人来说,一个更好的方法是使用这个插件: 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()的访问权限

使用 spec _ runner 上的独立 Jasmine (2.0.0)。Htlm,我可以点击一个特定的规格,并集中在一个规格。我应该早点注意到这个特征。

当使用 Karma 时,您只能使用 fitfdescribe(2.1之前的茉莉中的 iitddescribe)启用一个测试。


这只能运行 Spec1:

// or "ddescribe" in Jasmine prior 2.1
fdescribe('Spec1', function () {
it('should do something', function () {
// ...
});
});


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

这只能运行 testA:

describe('Spec1', function () {


// or "iit" in Jasmine prior 2.1
fit('testA', function () {
// ...
});


it('testB', function () {
// ...
});


});

不完全是你所要求的,但添加 iit将只测试特定的规格和忽略文件中的所有其他,ddescribe的工作原理是相同的。因此,您可以使用 iitddescribe关注特定的规格

在核心 自2.1与 fitfdescribe

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

有: 茉莉花的特点 聚焦眼镜(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();
});
});

但是,我不喜欢编辑我的测试(fit 和 fdescription)来有选择地运行它们。我更喜欢使用像 因果报应这样的测试运行程序,它可以使用正则表达式过滤掉测试。

下面是一个使用 哼哼的示例。

$ grunt karma:dev watch --grep=mypattern

如果您使用的是 吞下去(我最喜欢的任务运行器) ,您可以将 args 传递给 因果报应,并通过设置 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

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

我的本地测试运行: (执行14个中的1个(跳过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

这是最简单的答案与实际例子。即使在 f 描述中,你也可以使用它来运行一些积木。F 代表专注。

同样在一个没有描述的块中,你可以通过标记它们为合适来选择特定的块。

请运行以下代码并观察控制台日志,同时阅读代码中的注释。 阅读这篇作者的文章也会有所帮助。 < a href = “ https://davidtan.io/2016/01/03/control-which-test-run-in-jasmine.html”rel = “ nofollow norefrer”> https://davidtang.io/2016/01/03/controlling-which-tests-run-in-jasmine.html

 //If you want to run few describe only add f so using focus those describe blocks and it's it block get run


fdescribe("focus description i get run with all my it blocks ", function() {
it("1 it in fdescribe get executed", function() {
console.log("1 it in fdescribe get executed unless no fit within describe");


});
it("2 it in fdescribe get executed", function() {
console.log("2 it in fdescribe get executed unless no fit within describe");


});
//but if you and fit in fdescribe block only the fit blocks get executed
fit("3  only fit blocks in  fdescribe get executed", function() {
console.log("If there is  a fit   in fdescribe only fit blocks  get executed");


});
});


describe("none description i get skipped with all my it blocks ", function() {
it("1 it in none describe get skipped", function() {
console.log("1 it in none describe get skipped");


});
it("2 it in none describe get skipped", function() {
console.log("2 it in none describe get skipped");
});
//What happen if we had fit in a none fdescribe block will it get run ?   yes
fit("3 fit in none describe get executed too eventhough it;s just describe ", function() {
console.log("3 fit in none describe get executed too");
});
});