参考chai官网、react官网和自己以前的文章新建了一个react脚手架,,参考mocha官网或者我以前的文章 构建好mocha环境
最终项目结构如下
我们看一下test0731.js
var chai = require("chai");
var expect = chai.expect;
var should = chai.should();
var beverages = { tea: ["chai", "matcha", "oolong"] };
var foo = 1;
describe("expect test", function() {
it("a test of expect1", function() {
// 1、expect(foo).to.have.lengthOf(3);
expect(beverages)
.to.have.property("tea")
.with.lengthOf(3);
});
// 2、 .deep (全等,不同于==)
it("a test of .deep", function() {
// expect({ foo: { bar: { baz: "quux" } } }).to.have.deep.property(
// "foo.bar.baz",
// "quux"
// ); /wrong
expect(new Set([{ a: 1 }])).to.have.deep.keys([{ a: 1 }]);
});
// 3、 .not 否定,慎用
it("a test of .not", function() {
expect({ a: 1 }).to.not.have.property("b");
});
// 4、嵌套.nest(不能和.own一起用)-----------在property和include之前
it("a test of .nested", function() {
expect({ a: { b: ["x", "y"] } }).to.have.nested.property("a.b[1]");
expect({ ".a": { "[b]": "x" } }).to.have.nested.property("\\.a.\\[b\\]");
});
// 5、 忽略继承的属性.own(不能和.nest一起用)-----------在property和include之前
it("a test of .own", function() {
Object.prototype.b = 2;
expect({ a: 1 }).to.have.own.property("a");
expect({ a: 1 }).to.not.have.own.property("b");
});
// 6、按照顺序.ordered-----------在members之前
it("a test of .ordered", function() {
expect([1, 2])
.to.have.ordered.members([1, 2])
.but.not.have.ordered.members([2, 1]);
// 如果有.include 就要从数组的开头排序
expect([1, 2, 3]).to.include.ordered.members([1, 2]);
expect([1, 2, 3])
.to.include.ordered.members([1, 2])
.but.not.include.ordered.members([2, 3]);
});
// 7、 .any 目标至少具有一个给定的键。这与.all相反-----------在keys之前
it("a test of .any", function() {
expect({ a: 1, b: 2 }).to.have.any.keys("c", "b");
});
// 8、 .all 要求目标具有所有的键-----------在keys之前,
it("a test of .all", function() {
expect({ a: 1, b: 2 }).to.have.all.keys("a", "b");
});
// 9、 .a(type[,msg]) 类型检查
it("a test of .all", function() {
// 9.1
expect(Symbol()).to.be.a("symbol");
// 9.2、支持通过symbol.tostringtag设置自定义类型的对象
let myObj = {
[Symbol.toStringTag]: "myCustomType"
};
expect(myObj)
.to.be.a("myCustomType")
.but.not.an("object");
// 9.3、在对同一目标做出更多断言之前,最好使用.a检查目标的类型。这样,您就可以避免根据目标类型执行不同操作的任何断言出现意外行为。
// a和an可以互换
expect([1, 2, 3])
.to.be.an("array")
.that.includes(2);
expect([]).to.be.an("array").that.is.empty;
// 9.4、不太建议在前面加.not
expect("foo").to.be.a("string"); // Recommended
expect("foo").to.not.be.an("array"); // Not recommended
// 9.5、可以加参数,表示断言失败后显示的信息
// expect(2).to.be.a("string", "why fail??");
// expect(1, "why fail??").to.be.a("string");
// 9.6、也可以这么写,增加可读性
expect({ b: 2 }).to.have.a.property("b");
});
// 10、 .include(val[, msg]) 包含关系
it("a test of .all", function() {
// 10.1 包含字符串
expect("foobar").to.include("foo");
// 10.2 数组包含某一项
expect([1, 2, 3]).to.include(2);
// 10.3 对象包含子集
expect({ a: 1, b: 2, c: 3 }).to.include({ a: 1, b: 2 });
// 10.4 集合中的值
expect(new Set([1, 2])).to.include(2);
// 10.5 map中的某个value
expect(new Map([["a", 1], ["b", 2]])).to.include(2);
// 10.6 因为.include根据目标的类型执行不同的操作,所以在使用.include之前检查目标的类型很重要。有关测试目标类型的信息,请参见.a文档
expect([1, 2, 3])
.to.be.an("array")
.that.includes(2);
// 10.7 添加deep
expect([{ a: 1 }]).to.deep.include({ a: 1 });
expect([{ a: 1 }]).to.not.include({ a: 1 });
expect({ x: { a: 1 } }).to.deep.include({ x: { a: 1 } });
expect({ x: { a: 1 } }).to.not.include({ x: { a: 1 } });
// 10.8 默认情况下,使用对象时搜索目标的所有属性。这包括继承和/或不可枚举的属性。在链的前面添加.own以从搜索中排除目标的继承属性
Object.prototype.b = 2;
expect({ a: 1 }).to.own.include({ a: 1 });
expect({ a: 1 })
.to.include({ b: 2 })
.but.not.own.include({ b: 2 });
// 注意,目标对象总是只搜索val自己的可枚举属性。
// 10.9 .deep 和.own可以一起用
expect({ a: { b: 2 } }).to.deep.own.include({ a: { b: 2 } });
// 10.10 加.nest-------点.和括号[]表示嵌套
expect({ a: { b: ["x", "y"] } }).to.nested.include({ "a.b[1]": "y" });
// \\转义符
expect({ ".a": { "[b]": 2 } }).to.nested.include({ "\\.a.\\[b\\]": 2 });
// 10.11 .deep 和 .nested可以一起用(全等和嵌套)
expect({ a: { b: [{ c: 3 }] } }).to.deep.nested.include({
"a.b[0]": { c: 3 }
});
// 10.12 加.not
expect("foobar").to.not.include("taco");
expect([1, 2, 3]).to.not.include(4);
// 否定要慎用,最好写确定的期望
expect({ c: 3 }).to.not.have.any.keys("a", "b"); // 推荐
expect({ c: 3 }).to.not.include({ a: 1, b: 2 }); // 不推荐
// 当期望目标对象具有val的键时,通常最好断言每个属性都有其预期值,而不是断言每个属性没有许多意外值中的一个。
expect({ a: 3, b: 4 }).to.include({ a: 3, b: 4 }); // Recommended
expect({ a: 3, b: 4 }).to.not.include({ a: 1, b: 2 }); // Not recommended
// 10.13 .include也可以添加参数,把它作为错误描述信息
// expect([1, 2, 3]).to.include(4, "nooo why fail??");
// expect([1, 2, 3], "nooo why fail??").to.include(4);
// .include还可以用作语言链,导致链中后面的所有.members和.keys断言要求目标是预期集的超集,而不是相同集。请注意,添加.include时,成员忽略子集中的重复项。
// Target object's keys are a superset of ['a', 'b'] but not identical
expect({ a: 1, b: 2, c: 3 }).to.include.all.keys("a", "b");
expect({ a: 1, b: 2, c: 3 }).to.not.have.all.keys("a", "b");
// Target array is a superset of [1, 2] but not identical
expect([1, 2, 3]).to.include.members([1, 2]);
expect([1, 2, 3]).to.not.have.members([1, 2]);
// Duplicates in the subset are ignored
expect([1, 2, 3]).to.include.members([1, 2, 2, 2]);
// 10.14 在链中添加.any之前的内容会导致.keys断言被忽略
// Both assertions are identical
expect({ a: 1 }).to.include.any.keys("a", "b");
expect({ a: 1 }).to.have.any.keys("a", "b");
// 10.15.includes、.contain和.contains可以与.include互换使用。
});
});
describe("should test", function() {
it("a test of should", function() {
foo.should.be.a("number");
beverages.should.have.property("tea").with.lengthOf(3);
});
});
运行以及结果