【问题标题】:Unit tests with chai and Eshint are not passingchai 和 Eshint 的单元测试没有通过
【发布时间】:2014-12-17 19:37:49
【问题描述】:

希望您能提供帮助,最近从 jshint 转移到 eslint,我想看看如何在不进行更改的情况下让我的测试通过;

以下面的测试为例

expect(ctrl.screens).to.not.be.undefined;

eshint 抱怨以下期望;

error  Expected an assignment or function call and instead saw an expression  no-unused-expressions

将测试更改为;

expect(ctrl.screens).to.not.be.undefined();

给出以下错误:

TypeError: '[object Object]' is not a function (evaluating 'expect(ctrl.screens).to.not.be.undefined()')

任何想法在这里采取什么方法?当我从任务列表中删除 eshint 时,所有测试都通过了,所以我需要一些方法来清理这些测试。

J

【问题讨论】:

    标签: node.js unit-testing jshint


    【解决方案1】:

    对未定义的测试很棘手。有一些替代方法可以做到这一点。我通常检查typeof(variable) === 'undefined

    例子:

    expect(typeof(ctrl.screens)).to.not.equal('undefined');
    

    【讨论】:

    • @user falsy 不保证它是未定义的。它可以是 null、false、未定义、空字符串等...
    • @fmsf 这个答案有效,但不是因为原始检查未定义(即expect(ctrl.screens).to.not.be.undefined;)无效。它之所以有效,是因为它使用了一个函数调用,它通过了 eshint 规则。
    猜你喜欢
    • 2018-04-17
    • 1970-01-01
    • 2021-07-01
    • 2018-03-22
    • 2023-04-11
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2018-11-05
    相关资源
    最近更新 更多