Tag: chai

NodeJS UnhandledPromiseRejectionWarning

所以,我正在testing一个依赖事件发生器的组件。 为此,我想出了一个使用Mocha + Chai的承诺的解决scheme: it('should transition with the correct event', (done) => { const cFSM = new CharacterFSM({}, emitter, transitions); let timeout = null; let resolved = false; new Promise((resolve, reject) => { emitter.once('action', resolve); emitter.emit('done', {}); timeout = setTimeout(() => { if (!resolved) { reject('Timedout!'); } clearTimeout(timeout); }, 100); }).then(((state) => { resolved = […]

如何获得“should.be.false”语法通过jslint?

我正在为我的NodeJS代码写JS UT。 我使用Chai作为断言库,我更喜欢should语法。 我也使用jslint来检查JS文件的语法,甚至是UT的JS文件。 现在我遇到了jslint和Chai的问题。 在柴 ,你可以使用: myvalue.should.be.true; 但jslint给我: #1 Expected an assignment or function call and instead saw an expression. 我知道jslint带有很多选项,但是我找不到合适的选项来closures这个检查。

Chai:如何用'should'语法来testingundefined

在本教程的基础上testing一个带有chai的angularjs应用程序,我想使用“should”风格为未定义的值添加一个testing。 这失败了: it ('cannot play outside the board', function() { scope.play(10).should.be.undefined; }); 与错误“types错误:不能读属性'应该'未定义”,但testing通过与“期望”风格: it ('cannot play outside the board', function() { chai.expect(scope.play(10)).to.be.undefined; }); 我怎样才能使它与“应该”?

我如何正确地testing摩卡和柴的承诺?

下面的testingperformance得很奇怪: it('Should return the exchange rates for btc_ltc', function(done) { var pair = 'btc_ltc'; shapeshift.getRate(pair) .then(function(data){ expect(data.pair).to.equal(pair); expect(data.rate).to.have.length(400); done(); }) .catch(function(err){ //this should really be `.catch` for a failed request, but //instead it looks like chai is picking this up when a test fails done(err); }) }); 我应该如何妥善处理被拒绝的承诺(并testing)? 我应该如何正确处理一个失败的testing(即: expect(data.rate).to.have.length(400); 这是我正在testing的实现: var requestp = require('request-promise'); […]

柴testing数组相等不按预期方式工作

为什么以下失败? expect([0,0]).to.equal([0,0]); 什么是正确的方法来testing?

在调用asynchronous函数的摩卡testing中如何避免超时错误:超过2000ms的超时

在我的节点应用程序中,我使用摩卡来testing我的代码。 当使用摩卡调用很多asynchronous函数时,我得到超时错误( Error: timeout of 2000ms exceeded. )。 我该如何解决这个问题? var module = require('../lib/myModule'); var should = require('chai').should(); describe('Testing Module', function() { it('Save Data', function(done) { this.timeout(15000); var data = { a: 'aa', b: 'bb' }; module.save(data, function(err, res) { should.not.exist(err); done(); }); }); it('Get Data By Id', function(done) { var id = "28ca9"; module.get(id, function(err, […]

摩卡/柴期望不会抛出抛出的错误

我有问题让柴的expect.to.throw工作在我的node.js应用程序的testing。 testing在抛出的错误上保持失败,但是如果我试着包装testing用例,并捕获和声明被捕获的错误,它就可以工作。 是否expect.to.throw不工作像我认为应该或什么? it('should throw an error if you try to get an undefined property', function (done) { var params = { a: 'test', b: 'test', c: 'test' }; var model = new TestModel(MOCK_REQUEST, params); // neither of these work expect(model.get('z')).to.throw('Property does not exist in model schema.'); expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.')); […]