有条件地忽略Karma / Jasmine的个别测试 [英] Conditionally ignore individual tests with Karma / Jasmine

查看:96
本文介绍了有条件地忽略Karma / Jasmine的个别测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些测试在PhantomJS中失败但在其他浏览器中失败。

I have some tests that fail in PhantomJS but not other browsers.

我希望在我的监视任务中使用PhantomJS时忽略这些测试(所以新的浏览器窗口不需要关注并且性能更快一些),但在我的标准测试任务和我的CI管道中,我希望所有测试在Chrome,Firefox等中运行...

I'd like these tests to be ignored when run with PhantomJS in my watch task (so new browser windows don't take focus and perf is a bit faster), but in my standard test task and my CI pipeline, I want all the tests to run in Chrome, Firefox, etc...

我考虑过像 foo.spec.dont-use-phantom.js 这样的文件命名约定排除我的Karma配置中的那些,但这意味着我必须将未能进入自己文件的各个测试分开,将它们与逻辑 describe 块分开并具有更多具有奇怪命名约定的文件通常很糟糕。

I've considered a file-naming convention like foo.spec.dont-use-phantom.js and excluding those in my Karma config, but this means that I will have to separate out the individual tests that are failing into their own files, separating them from their logical describe blocks and having more files with weird naming conventions would generally suck.

简而言之:

有没有办法我可以扩展Jasmine和/或Karma并以某种方式注释单个测试只能运行某些配置吗?

Is there a way I can extend Jasmine and/or Karma and somehow annotate individual tests to only run with certain configurations?

推荐答案

我可以分享我的经验。

在我们的环境中,我们有几个测试运行不同的浏览器和不同的技术。
为了在所有平台和浏览器上始终运行相同的套件,我们在karma中加载了一个帮助文件( helper.js ),并加载了一些功能检测功能全球。

In our environment we have several tests running with different browsers and different technologies. In order to run always the same suites on all the platforms and browsers we have a helper file loaded in karma (helper.js) with some feature detection functions loaded globally.

function isFullScreenSupported(){
  // run some feature detection code here
}

你也可以使用 Modernizr 也是如此。

You can use also Modernizr for this as well.

在我们的测试中,我们将内容包装起来 if / else 阻止如下:

In our tests then we wrap things in if/else blocks like the following:

it('should work with fullscreen', function(){
  if(isFullScreenSupported()){
    // run the test
  }
  // don't do anything otherwise
});

或异步测试

it('should work with fullscreen', function(done){
  if(isFullScreenSupported()){
    // run the test
    ...
    done();
  } else {
    done();
  }
});

虽然它有点冗长,但它可以为你所面临的情景节省时间。

While it's a bit verbose it will save you time for the kind of scenario you're facing.

在某些情况下,您可以使用用户代理嗅探来检测特定的浏览器类型或版本 - 我知道这是不好的做法,但有时实际上没有其他办法。

In some cases you can use user agent sniffing to detect a particular browser type or version - I know it is bad practice but sometimes there's effectively no other way.

这篇关于有条件地忽略Karma / Jasmine的个别测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆