1 minute read

Reminder

I can’t stress enough how important TDD and CI/CD is when it comes to having an efficient engineering team. We scoff at hand ledgers in the era of Excel yet we (business included) still overuse manual QA. Forget about nonsense like this to compare two Mongoose Ids

Jump-in

Often, we want to reuse the test suites and encompassing assertions when writing out test cases but want to input different values. Currently, Jasmine doesn’t provide any kind of pattern for doing so. If you don’t want to use another 3rd party library, such as ‘jasmine-where’, there is a relatively easy way to do so below. In both examples, it will go through the array of objects expecting the answer from the calculation to equal the answer in the array of values.

The first way is if we are doing things synchronously.

describe("A calculator", function() {
  it("should square correctly", function() {
    [
      { number: 2, answer: 4},
      { number: 3, answer: 9},
      { number: 4, answer: 16}
    ].forEach(function(problem, index, array){
      // we will pretend to calculate with our fnc here
      var calcAnswer = problem.number*problem.number;
      expect(calcAnswer).toEqual(problem.answer);
    });
  });
});

The second way is if we are doing things asynchronously which I have just pretended with the good ole’ setTimeout function.

describe("A calculator", function() {
  it("should square correctly", function(done) {
    [
      { number: 2, answer: 4},
      { number: 3, answer: 9},
      { number: 4, answer: 16}
    ].forEach(function(problem, index, array){
      // we will pretend to calculate with our fnc here
      setTimeout(function(){
        var calcAnswer = problem.number*problem.number;
        expect(calcAnswer).toEqual(problem.answer);
        // once we have finished checking the values, we can call done
        if (index === (array.length-1)){
          done();
        }
      },100);
    });
  });
});

Code-Examples

The gists are here in case you are interested.

Synchronous

Asynchronous

Sync Example on Github

Async Example on Github