testing节点,摩卡和柴(应/ bdd语法)

上下文

我是一个JavaScript的爱好者,并把我的项目到一个新的水平,我试图在BitBucket中设置一个pipe道,将运行我的unit testing(和稍后做其他事情)。

我已经使用Mocha和Chai进行testing,但是我在浏览器中使用了一个设置依赖关系并运行testing的html页面。

问题

我面临的问题是,我不能让should语法在节点中工作,而我之前在浏览器中已经有工作testing。 should作为柴图书馆的一部分。

我使用最less的代码初始化了一个新的项目,但是在这里它也不起作用: should简单地不分配。

package.json ,包括摩卡和柴,并build立摩卡进行testing。

 { "name": "testtest", "version": "1.0.0", "description": "Minimal, complete and verifiable example", "main": "index.js", "dependencies": {}, "devDependencies": { "chai": "^4.0.1", "mocha": "^3.4.2" }, "scripts": { "test": "mocha -u bdd" }, "author": "GolezTrol", "license": "ISC" } 

test / test-foo.js ,包含testing。 我添加的唯一的东西是require的第一行。 之前,浏览器包含了这个文件,Foo是全球的。 我回应道,给了我5个口,应该给我不明确的。

 var Foo = require('../src/foo.js').Foo; describe('Foo', function(){ it('should flurp gnip snop', function() { var sut = Foo.flurp(); console.log(sut); // 5 console.log(sut.should); // undefined sut.should.be.closeTo(5.00000, 0.00001); }); }); 

src / foo.js ,被testing的实际单位。 在旧的设置中,Foo将是全局的(或者实际上将其本身作为一个属性添加到另一个全局中,但现在不相关)。 我改变了这一点,所以它被导出为exports.Foo ,所以我可以require它。 这基本上是有效的,因为我在testing中得到'5'。

 exports.Foo = function(){}; // Export Foo var Foo = exports.Foo; // Give Foo a method Foo.flurp = function(){ return 5; } 

我从testing中得到的输出显示'Foo'(描述),5(logging结果)和undefined(sut.should的logging值)。 之后,这显然显示了testing失败的正式结果:

  Foo 5 undefined <clipped for brevity> 1) Foo should flurp gnip snop: TypeError: Cannot read property 'be' of undefined at Context.<anonymous> (test\test-foo.js:9:15) 

替代

我可以通过添加这一行来更改所有现有的unit testing:

 var expect = require('chai').expect; 

并将assert的语法改为

 expect(sut).to.be.closeTo(5.00000, 0.00001); 

这是一个可行的select,因为我只有几十个testing。 但是我仍然有兴趣find上述问题的答案。

你需要调用should方法来添加所有应该原型的开始

 let should = chai.should();