logo
down
shadow

Mocha JS: How to reuse assertions within a spec?


Mocha JS: How to reuse assertions within a spec?

By : AVG
Date : November 24 2020, 03:41 PM
will be helpful for those in need I'm using Mocha bdd for unit testing. , Here is an example of parameterized tests:
code :


Share : facebook icon twitter icon
Mocha: assertions within promise not working

Mocha: assertions within promise not working


By : Matt Weise
Date : March 29 2020, 07:55 AM
I hope this helps . Mocha is not able to find your tests because you're not structuring them correctly. The it() blocks should go immediately within the describe() blocks (and the describe() blocks may be nested within each other as you see fit). If you want to use promises, the promises should go inside the it() blocks, not the other way around. So, as a first pass, the updated code should look something like this (I'm cutting some parts out for brevity):
code :
describe('Domain Model', function(){
  var options = { ... };

  describe('Initialize',function(){
    it('should be an string', function(){
      //request auth key
      var promise = new Promise(function(resolve,reject){
        // ...
      });
      promise.then(function(data,test){
        var token = (JSON.parse(data))["access_token"];
        console.log(token);
        (token).should.be.type('string');
      });
    });
  });
});
describe('Domain Model', function(){
  var options = { ... };

  describe('Initialize',function(){
    it('should be an string', function(done){
      //request auth key
      var promise = new Promise(function(resolve,reject){
        // ...
      });
      promise.then(function(data,test){
        var token = (JSON.parse(data))["access_token"];
        console.log(token);
        (token).should.be.type('string');
        done();
      });
    });
  });
});
describe('Domain Model', function(){
  var options = { ... };

  describe('Initialize',function(){
    it('should be an string', function(done){
      //request auth key
      request.post(options, function(error, response, body){
        //body contains the auth key, within a string of JSON
        var token = (JSON.parse(body))["access_token"];
        console.log(token);
        (token).should.be.type('string');
        done();
      });
    });
  });
});
describe('Domain Model', function(){
  var options = { ... };

  describe('Initialize',function(){
    it('should be an string', function(done){
      //request auth key
      var promise = new Promise(function(resolve,reject){
        // ...
      });
      promise.then(function(data){
        var token = (JSON.parse(data))["access_token"];
        console.log(token);
        (token).should.be.type('string');
        done();
      }, function (err) {
        done(err);
      });
    });
  });
});
describe('Domain Model', function(){
  var options = { ... };

  describe('Initialize',function(){
    it('should be an string', function(done){
      //request auth key
      var promise = new Promise(function(resolve,reject){
        // ...
      });
      promise.done(function(data){
        var token = (JSON.parse(data))["access_token"];
        console.log(token);
        (token).should.be.type('string');
        done();
      });
    });
  });
});
mocha times out on failed assertions with Q promises

mocha times out on failed assertions with Q promises


By : Lovecraft
Date : March 29 2020, 07:55 AM
To fix the issue you can do The assertion call throws an exception, which is caught by Q in order to properly conform to the promises spec. So mocha never reaches done(), nor does it see an exception thrown. You could do something like this:
code :
it('test', function(done) {
  hack().then(function(bool) {
    assert(false);
    done();
  }).catch(function(err){
    done(err);
  });
});
it('test', function() {
  return hack().then(function(bool) {
    assert(false);
  });
});
Mocha: Translating a user story to a spec of describe / it (bdd framework i.e. mocha)?

Mocha: Translating a user story to a spec of describe / it (bdd framework i.e. mocha)?


By : user2982232
Date : March 29 2020, 07:55 AM
With these it helps I am not very familiar with mocha so I did a similar one for you. Hope you get the idea.
Scenario 1:
code :
it should authenticate card currectly if a valid card and details are provided:
System.readCard(new Card({config}))....
expect(System.Card.isValid).toBe(true)

it should get the correct financial details from the user 
// You know it's your card and you have $100
var originalBalance = System.User.Balance;
expect(originalBalance).to.be(100);

it should have enough for the user
System.User.RequestBalance = 20;
expect(System.Vault.Balance).to.be.at.least(System.User.RequestBalance);

it should dispense the requested amount:
System.Dispence(System.User.RequestBalance);
expect(System.Vault.Balance).to.be(System.Vault.Balance - System.User.RequestBalance);

it should deduct the amount from user's account
expect(System.User.Balance).to.be(originalBalance - System.User.RequestBalance);

it should return the card
Syste.ejectCard();
expect(System.CardHolder.isEmpty).to.be(true);
Mocha unit tests and assertions in `for`loop

Mocha unit tests and assertions in `for`loop


By : Miki Degtyar
Date : March 29 2020, 07:55 AM
To fix this issue I'm working on Gulp plugin allowing to write JavaScript code using reserved words written in language different than English - its main doing is translating, yet it is not what I'm about. , Wrap the code inside your loop in a closure:
code :
for (var i = 0, l = asserts.length; i < l; i++) {
  (function(n) {
    var assert = asserts[n];
    var desc = 'should turn `' + assert.passed + '` into `' + assert.expected + '`';
    it(desc, function (done) {
        tester(assert.passed, assert.expected, done);
    });
  })(i);
}
Scopes & Closures in Mocha/Chai Assertions

Scopes & Closures in Mocha/Chai Assertions


By : Dasha
Date : March 29 2020, 07:55 AM
hop of those help? I'm writing some tests for an express app and I am wondering how to properly access a variable in one assertion block from another. The variable I am trying to access is this.token = res.body.token , You can move your variable to the scope of your describe.
Related Posts Related Posts :
  • angular 6 Please add a @NgModule annotation
  • Trying to cast element ID to class selector in jQuery
  • Swipe JS - Display 3 Slides at a time
  • Jasmine test to call function when value is null
  • Redux reducer gets string instead of object
  • Keep chrome javascript running even if page reload
  • The event.target.value is not insert the userinput into a variable
  • Exporting HTML tables to Excel (.xls) in a separate sheet
  • Jquery tooltip on dialog close button
  • access variable inside anonymous function from outside
  • How come func is called from a function?
  • How to change add and remove active class in JavaScript
  • how to toggle a specific div in javascript
  • How to get all the values from multiple keys of an array of object?
  • jQuery bind() unbind() and on() and off()
  • regular expression to find DD Month YYYY from a string
  • How to upload files from web client (HTML/jQuery form) to your Dropbox folder
  • Backbone history and require.js issue
  • Confusion over Classical and Prototypal Inheritance Javascript
  • How to take screen shot of current webpage using Javascript/JQuery?
  • Store meridiem in a variable
  • class declared inside closure vs standard class without closure
  • javascript confirm box always return true
  • dynamically changing the size of font size based on text length using css and html
  • Node.js - Getting the host IP address while on Amazon EC2
  • Confusing CSS, can anyone explain please
  • How to install v8-profiler on Windows 8 (64 bit) using npm?
  • Resolve promise based on another promise
  • google maps adding overlay layer above UI and markers
  • Style specific letter?
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org