chai-as-promised

How to test for a propety of a class after promise resolution with mocha and chai

半腔热情 提交于 2020-01-07 06:25:53
问题 I am trying out a bit of unit testing with Mocha + Chai as promised 'use strict'; var chai = require('chai').use(require('chai-as-promised')) var should = chai.should(); describe('Testing how promises work', () => { it("should work right", function(){ class SomeClass { constructor() { this.x = 0; } getSomething(inputVal) { let self = this; return new Promise((resolve, reject) => { setTimeout(function(){ if(inputVal){ self.x = 1; resolve(); } else{ self.x = -1; reject(); } }, 10); }); } } var

Do I really need to return a promise in test when using Chai as Promised?

冷暖自知 提交于 2019-12-18 07:06:56
问题 Chai as Promised documentation states as follows: Notice : either return or notify(done) must be used with promise assertions. And the examples on the site are as follows: return doSomethingAsync().should.eventually.equal("foo"); doSomethingAsync().should.eventually.equal("foo").notify(done); The thing is; I actually wrote a test using chai as promised without returning the promise. Like so: it('should resolve user', function () { $state.get(state).resolve.user(dataservice, { userId: testUser

How do I handle an array of promises using Protractor w/ chai-as-promised

ε祈祈猫儿з 提交于 2019-12-14 02:20:52
问题 I am using Protractor with CucumberJS and chai-as-promised (given that CucumberJS does not have a built-in assertions library) to build an automated test suite. Everything works fine for single assertions (using the expect feature of chai-as-promised). I run into trouble, however, when attempting to handle multiple promises within the same test (step). In the following example, verifyUserFirstName returns a promise mapped to a certain row's td.getText(). this.Then(/^I should see my user entry

Selenium-Webdriver w/ Chai as Promised and Mocha failing to wait

自古美人都是妖i 提交于 2019-12-13 07:48:07
问题 I've got a simple coffeescript test w/ Selenium-Webdriver using Chai-as-Promised and Mocha which is supposed to test a web page that I have that uses an AJAX call to do authentication once a login button is pressed: selenium = require 'selenium-webdriver' chai = require 'chai' chai.use require 'chai-as-promised' expect = chai.expect before -> @timeout 10000 @driver = new selenium.Builder() .withCapabilities(selenium.Capabilities.chrome()) .build() @driver.getWindowHandle() after -> @driver

Chai-As-Promised is eating assertion errors

爱⌒轻易说出口 提交于 2019-12-12 10:34:44
问题 I'm using chai-as-promised + mocha for writing some selenium-webdriver tests. Since webdriver extensively uses promises , I imagined it would be better if I used chai-as-promised for those type of tests. The problem is that when the tests fail, the error is not being caught properly by mocha, and it just fails without outputting anything. Sample code: it 'tests log', (next) -> log = @client.findElement(By.css("...")) Q.all([ expect(log.getText()).to.eventually.equal("My Text") expect(log

check chai as promised on multiple properties object

我的未来我决定 提交于 2019-12-12 04:54:31
问题 hello I have a method which returns me data along with URL , so return object has url and body as two properties. return new Promise(function(resolve,reject) { request(url, function (error, response, body) { if(error) reject(error); else { if(response.statusCode ==200) resolve( { "url" :url , "body" : body}); else reject("error while getting response from " + url); } }); }); How should I test this in Chai- as promised it works for 1 property. it("get data from correct url", function(){ return

How does one use Q.all with chai-as-promised?

回眸只為那壹抹淺笑 提交于 2019-12-10 21:43:10
问题 The chai-as-promised docs have the following example of dealing with multiple promises in the same test: it("should all be well", function (done) { Q.all([ promiseA.should.become("happy"), promiseB.should.eventually.have.property("fun times"), promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed") ]).should.notify(done); }); I assume that the Q here has come from npm install q and var Q = require('q'); . Where does .should come from? When I try this should is undefined

Testing rejected promise in Mocha/Chai

你离开我真会死。 提交于 2019-12-04 17:02:43
问题 I have a class that rejects a promise: Sync.prototype.doCall = function(verb, method, data) { var self = this; self.client = P.promisifyAll(new Client()); var res = this.queue.then(function() { return self.client.callAsync(verb, method, data) .then(function(res) { return; }) .catch(function(err) { // This is what gets called in my test return P.reject('Boo'); }); }); this.queue = res.delay(this.options.throttle * 1000); return res; }; Sync.prototype.sendNote = function(data) { var self = this

Verify that an exception is thrown using Mocha / Chai and async/await

主宰稳场 提交于 2019-12-03 11:56:06
问题 I'm struggling to work out the best way to verify that a promise is rejected in a Mocha test while using async/await. Here's an example that works, but I dislike that should.be.rejectedWith returns a promise that needs to be returned from the test function to be evaluated properly. Using async/await removes this requirement for testing values (as I do for the result of wins() below), and I feel that it is likely that I will forget the return statement at some point, in which case the test

Testing rejected promise in Mocha/Chai

旧街凉风 提交于 2019-12-03 11:02:24
I have a class that rejects a promise: Sync.prototype.doCall = function(verb, method, data) { var self = this; self.client = P.promisifyAll(new Client()); var res = this.queue.then(function() { return self.client.callAsync(verb, method, data) .then(function(res) { return; }) .catch(function(err) { // This is what gets called in my test return P.reject('Boo'); }); }); this.queue = res.delay(this.options.throttle * 1000); return res; }; Sync.prototype.sendNote = function(data) { var self = this; return self.doCall('POST', '/Invoice', { Invoice: data }).then(function(res) { return data; }); }; In