Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between assertion library, testing framework and testing environment in javascript?

Chai is an assertion library.

Mocha and Jasmine are testing frameworks.

and Karma is a testing environment.

I've already read Difference between available testing frameworks: mocha, chai, karma, jasmine, should.js etc.

like image 498
Nader Avatar asked Sep 05 '14 03:09

Nader


People also ask

What is assertion library JavaScript?

Assertion libraries are tools to verify that things are correct. This makes it a lot easier to test your code, so you don't have to do thousands of if statements.

What is assertion framework?

Advertisements. Python testing framework uses Python's built-in assert() function which tests a particular condition. If the assertion fails, an AssertionError will be raised. The testing framework will then identify the test as Failure.

What is testing framework in JavaScript?

What is a JavaScript Testing Framework? The JavaScript testing framework is a dynamic framework based on JS, which is well known for its ease of use in front-end and back-end development. These transitions over time also result in the need for excellent testing tools.


2 Answers

Assertion libraries are tools to verify that things are correct.
This makes it a lot easier to test your code, so you don't have to do thousands of if statements.
Example (using should.js and Node.js assert module):

var output = mycode.doSomething(); output.should.equal('bacon'); //should.js assert.eq(output, 'bacon'); //node.js assert  // The alternative being: var output = mycode.doSomething(); if (output !== 'bacon') {   throw new Error('expected output to be "bacon", got '+output); } 

Testing frameworks are used to organize and execute tests.
Mocha and Jasmine are two popular choices (and they're actually kinda similar).
Example (using mocha with should.js here):

describe('mycode.doSomething', function() {   it ('should work', function() {     var output = mycode.doSomething();     output.should.equal('bacon');        });   it ('should fail on an input', function() {     var output = mycode.doSomething('a input');     output.should.be.an.Error;   }); }); 

Testing Environments are the places where you run your tests.

Karma is a bit of an edge case, in the sense that it's kind of a one off tool, not many like it. Karma works by running your unit tests inside of browsers (defaulting to PhantomJS, a headless WebKit browser), to allow you to test browser-based JavaScript code.

Frameworks like Mocha and Jasmine work both in the browser and with Node.js, and usually default to Node.

like image 128
Zoey Mertes Avatar answered Sep 18 '22 08:09

Zoey Mertes


The testing environment (or test runner) is what runs all of your tests. It launches them, aggregates results, etc.

The testing framework is what you use to create each of the tests. For example, jasmine uses a syntax of

it('name of test', function() {    // do some tests }); 

The assertion library is what does the actual verification of your test results

it('name of test', function() {    assert x == 5 //pseudocode, the syntax will vary based on your asserting framework  }); 
like image 41
Jeff Storey Avatar answered Sep 20 '22 08:09

Jeff Storey