有没有类似于Junit的框架用于测试Node.js中的面向对象JavaScript?

6

有没有关于在Node.js中测试面向对象JavaScript的最佳实践?

例如,如果我有以下Cat.js类:

function Cat(age, name) {
    this.name = name || null;
    this.age = age || null;
}

Cat.prototype.getAge = function() {
    return this.age;
}

Cat.prototype.setAge = function(age) {
    this.age = age;
}

Cat.prototype.getName = function(name) {
    return this.name;
}

Cat.prototype.setName = function(name) {
    this.name = name;
}

Cat.prototype.equals = function(otherCat) {
    return otherCat.getName() === this.getName()
        && otherCat.getAge() === this.getAge();
}

Cat.prototype.fill = function(newFields) {
    for (var field in newFields) {
        if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) {
            if (this[field] !== 'undefined') {
                this[field] = newFields[field];
            }
        }
    }
};

module.exports = Cat;

我希望写出像Java的junit测试一样的测试类:

var cat1;

setUp() {
    cat1 = new Cat(10, 'Tom');
}

提醒一下,这个问题可能会被标记为关闭,因为它太宽泛了(并且要求外部资源推荐)。如果可以的话,请尝试进行精细化改进。 - brandonscript
这里有很多JavaScript的单元测试框架。 - Schleis
我写了一个小型的JS库,叫做Tidbits,它既有面向对象的框架,也有单元测试框架。 - user1115652
1个回答

1

还有JsUnit。 - David Balažic
现在我也会把Jest加入到列表中)。 - Alex Barkun
Jest也在那里。 - Partha Paul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接