DEV Community

John Peters
John Peters

Posted on

Unit Testing is not Jasmine/Karma

Big revelation here, ready... Jasmine/Karma have redefined the term Unit test. Their prescription is a deviation from the past.

Traditional Unit Testing

Traditional Unit testing allowed for harnessing the class/object and calling any function or method with a predetermined set of input parameters.

Any outbound Http requests were fully functional. All DB requests worked. All security worked, everything just worked. The ouput of each test asserted correctness.

The Units of the Unit test were the functions themselves.

Diminished Results

Jasmine/Karma doesn't do this. The reason is that its own web server is used, making all application Http calls fail due to CORS restrictions.

The prescription for this problem is to inject mock objects and spies. This technique bypasses the traditional meaning of Unit testing. Diminished Unit Testing producing diminished results.

Solution

Today we have better testing Frameworks which can double down as both Unit test and include Integration test as soon as the class begins to integrate with other components.

Better Test Frameworks

1) Protractor allows 100% addressibiliy to all DOM nodes. Simply enter a well planned set of permutations to one or more inputs, and Assert the output is correct.

2) Cypress is everything Protractor is but allows for Http Request and Response interception. This ability leaves Protractor in the dust.

3) Puppeteer, similar to Cypress but newest to the market. Is as powerful as Cypress.

Recommendation

Just say no to Karma/Jasmine except for elementary CTOR tests. Step up to the GUI layer for more in-depth testing. The depth is controlled by the permutations.

From there add AI to those tests and you may be able to generate tests in seconds.

Top comments (0)