关于chai:前端单元测试-chai

介绍chai 是一个断言库,能够与任何javascript测试框架完满地配对。反对BDD和TDD两种格调的断言模式,具备链性能的BDD款式提供了一种表白性强的语言和易于浏览的款式,而TDD断言款式则提供了更古典的感觉: should: BDD格调断言expect: BDD格调断言assert: TDD格调断言集体倡议应用expect,在这里只介绍expect。 装置npm install chai语言链以下是可链接的获取器,以进步断言的可读性。 tobebeenisthatwhichandhashavewithatofsamebutdoesstill.not 否定链中随后的所有断言。 expect(function () {}).to.not.throw();expect({a: 1}).to.not.have.property('b');expect([1, 2]).to.be.an('array').that.does.not.include(3); 通常最好断言是产生了一个预期的输入,而不是断言没有产生有数意外的输入之一。所以.not否定任何断言,并不意味着您应该这样做。 expect(2).to.equal(2); // Recommendedexpect(2).to.not.equal(1); // Not recommended .deep 导致链中前面的所有.equal,.include,.members,.keys和.property断言应用深度相等而不是严格 === 相等。 // Target object deeply (but not strictly) equals `{a: 1}`expect({a: 1}).to.deep.equal({a: 1});expect({a: 1}).to.not.equal({a: 1});// Target array deeply (but not strictly) includes `{a: 1}`expect([{a: 1}]).to.deep.include({a: 1});expect([{a: 1}]).to.not.include({a: 1});// Target object deeply (but not strictly) includes `x: {a: 1}`expect({x: {a: 1}}).to.deep.include({x: {a: 1}});expect({x: {a: 1}}).to.not.include({x: {a: 1}});// Target array deeply (but not strictly) has member `{a: 1}`expect([{a: 1}]).to.have.deep.members([{a: 1}]);expect([{a: 1}]).to.not.have.members([{a: 1}]);// Target set deeply (but not strictly) has key `{a: 1}`expect(new Set([{a: 1}])).to.have.deep.keys([{a: 1}]);expect(new Set([{a: 1}])).to.not.have.keys([{a: 1}]);// Target object deeply (but not strictly) has property `x: {a: 1}`expect({x: {a: 1}}).to.have.deep.property('x', {a: 1});expect({x: {a: 1}}).to.not.have.property('x', {a: 1}); .nested ...

October 22, 2020 · 3 min · jiezi