用chai摆脱没有多余的表达式的linter错误的好方法 [英] Nice way to get rid of no-unused-expressions linter error with chai

查看:66
本文介绍了用chai摆脱没有多余的表达式的linter错误的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chai测试中,我经常发现自己想使用它们的断言,例如 .to.be.empty .to.be .true 等,因为我发现它们比 .to.be.length(1)读起来更干净.to.be.equal(true)。但是,这样会打碎我的棉inter(我使用的是默认的Airbnb棉绒)。

In my Chai tests I often find myself wanting to use their assertions that are something like .to.be.empty, .to.be.true e.t.c., because I find them to be cleaner to read than .to.be.length(1) or .to.be.equal(true). However, this breaks my linter (I'm using default Airbnb linting).

我可以使用 // disable-eslint-line 语法,但是随后我必须添加它每一行看起来像这样,看起来都很乏味。

I could use the // disable-eslint-line syntax, but then I'd have to add it to every single line that reads like that and that seems tedious.

我还读过 DirtyChai 库,但这将要求我回顾整个测试库,并在其中添加方括号,这似乎是我不必为了使棉短短短短短短短衣就可以通过某些东西而应该做的事情地点。

I've also read about the DirtyChai library, but that would require me to go back through my entire testing library adding brackets to them all which seems like something I shouldn't have to do simply to get my linter to pass something it should probably be OK with in the first place.

还有谁能比我上面概述的方法更好地处理此问题?

Does anyone know a nicer way to handle this than the ways I've outlined above?

推荐答案

您可以使用 禁止禁用 在相关文件的顶部:

You can disable the rule for the entire file using eslint-disable at the top of the file in question:

/* eslint-disable no-unused-expressions */
expect(someTrueValue).to.be.true; 

但是,将其添加到每个测试文件的顶部可能很繁琐。要为所有相关文件禁用此规则,您可以:

However, adding this at the top of every test file can be tedious. To disable this rule for all relevant files, you can:


  1. 放入新的 .eslintc 配置文件与您的目录位于同一目录中测试文件,配置为禁用该规则。这样,您就可以对所有其他规则使用默认配置,而只在该文件夹中的文件上忽略该规则。 ESLint将此称为 配置级联

  1. Put a new .eslintc configuration file in the same directory as your test files, configured to disable that rule. This allows you to use the default configuration for all other rules while ignoring that rule specifically only on files in that folder. ESLint calls this Configuration Cascading.

{
    "rules": {
        "no-unused-expressions": "off"
    }
}


  • 在主 .eslintrc 文件中使用替代键来禁用具有全局模式匹配的文件组规则

    {
        "overrides": [
            {
                "files": ["*.test.js", "*.spec.js"],
                "rules": {
                    "no-unused-expressions": "off"
                }
            }
        ]
    }
    


  • 这还允许您禁用其他ru在使用 no-underscore-dangle > 重新布线

    This also allows you to disable other rules which become troublesome in testing, such as no-underscore-dangle when using rewire.

    这篇关于用chai摆脱没有多余的表达式的linter错误的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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