Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluebird Promise each in mocha/chai test not working

I would like some help to determine why my unit test in a sails.js app is not working as expected.

I am using mocha, chai and bluebird promise library on a sails.js app.

What I want to achieve:

  • Create a test for TagsService.create(name) method, which accepts a name parameter.
  • Test that this method will not create a new tag record based on invalid names I pass
  • The name parameter is required and should be less than 121 characters long

What I currently have:

// Test the 'create' method
describe('Method \'create\' test result: \n', function () {
  
  // Test that name is required and less than 121 chars long
  it('Must receive the name parameter and be less than 121 chars long', function(done) {
		
    // It should not accept any of the following names
    var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$£%fsf','$%@~}[','£$%jkdfi',' $%"£asdwdFDE','hD8U £$&{DS ds'];
    
    
      sails.bluebird.each(names,function(name){
        TagsService.create(name).then(function(data){
          assert.propertyVal(data,'status','err','An error was NOT returned - even though names provided should be invalid');
        });
      }).then(function(){
        done();
      });
    
		
   });
  
});

What happens is it seems to pass, even if I pass in a valid name or return null from the method.

like image 548
mrpetem Avatar asked Sep 15 '15 20:09

mrpetem


People also ask

How to use Chai as promised with Mocha?

Do an npm install chai-as-promised to get up and running. Then: You can of course put this code in a common test fixture file; for an example using Mocha, see the Chai as Promised tests themselves. Note when using other Chai plugins: Chai as Promised finds all currently-registered asserters and promisifies them, at the time it is installed.

How do you test promise-based code in Mocha?

Since you may be in the midst of wrestling your failing tests back into the green, let’s jump straight into how you can effectively test promise-based code in Mocha. If a function returns a promise that you want to assert on the result of the function, all you need to do for it to work effectively with Mocha is return the promise in your it block.

What is unit testing with Mocha and Chai?

In this article, we are mainly going to learn about Unit testing with Mocha and Chai. Let's learn about unit testing our code using Mocha, which is a light-weight Node.js test framework, and Chai, which is a TDD assertion library for node. Both Mocha and Chai run in NodeJs and the browser and allow asynchronous testing.

Can I use Chai as promised with jQuery?

In particular, Chai as Promised makes extensive use of the standard transformation behavior of then, which jQuery<3.0 does not support. Angular promises have a special digest cycle for their processing, and need extra setup code to work with Chai as Promised.


1 Answers

Well, looks like I managed to solve it, after much trial and error.

Turns out I need to catch the done() callback from the Promise after the each method executed. Also needed to return the result of the tests done from the TagsService promise object. (Still not 100% sure this is the correct way to think about it..). Anyway the test seems to function properly now.

Here is my result:

var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$%fsf','$%@~}[','�$%jkdfi',' $%"�asdwdFDE','hD8U �$&{DS ds'];
			
sails.bluebird.each(names, function(name){
    return TagsService.create(name).then(function(data) {
	assert.property(data, 'status', 'create method did not return a status property');
	assert(data.status === 'err', 'even with an invalid name parameter passed - it did not return an err status, which it must do with an invalid name.');
    });
}).then(function(){
	done();
}).catch(done);
like image 157
mrpetem Avatar answered Sep 30 '22 00:09

mrpetem