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

查看:21
本文介绍了有条件地忽略 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.

在我们的测试中,我们将内容包装在 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天全站免登陆