Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test private methods in Typescript

When I tried to do unit testing for private methods in a Class getting error as private methods are only accessible inside the class. Here I added sample snippet for my class and mocha test. Kindly provide me solution to implement unit test for private methods.

Class Name: Notification.ts

class Notification {  constructor() {}  public validateTempalte() {   return true;  }   private replacePlaceholder() {   return true;  } } 

Unit Test:

import {Notification} from 'Notification'; import * as chai from "chai";  describe("Notification", function(){    describe('#validateTempalte - Validate template', function() {       it('it should return success', function() {         const result = new Notification()         chai.expect(result.validateTempalte()).to.be.equal(true);       });     });   describe('#replacePlaceholder - Replace Placeholder', function() {       it('it should return success', function() {         const result = new Notification()         // As expected getting error "Private is only accessible within class"         chai.expect(result.replacePlaceholder()).to.be.equal(true);       });     }); }); 

As a workaround, currently, I am changing access specifier of function replacePlaceholder to public. But I don't think its a valid approach.

like image 598
Ashok JayaPrakash Avatar asked Feb 21 '18 12:02

Ashok JayaPrakash


People also ask

How do you test private methods in unit testing?

To test private methods, you just need to test the public methods that call them. Call your public method and make assertions about the result or the state of the object. If the tests pass, you know your private methods are working correctly.

Do private methods need unit tests?

You generally don't unit test private methods directly. Since they are private, consider them an implementation detail. Nobody is ever going to call one of them and expect it to work a particular way. You should instead test your public interface.

How do you cover a private method in a test class?

Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only.

Does TypeScript have private methods?

TypeScript Private MethodsMethods can also be private which is useful for hiding implementation detail of how a Class works to the user of the Class.


1 Answers

Technically, in current versions of TypeScript private methods are only compile-time checked to be private - so you can call them.

class Example {     public publicMethod() {         return 'public';     }      private privateMethod() {         return 'private';     } }  const example = new Example();  console.log(example.publicMethod()); // 'public' console.log(example.privateMethod()); // 'private' 

I mention this only because you asked how to do it, and that is how you could do it.

Correct Answer

However, that private method must be called by some other method... otherwise is isn't called at all. If you test the behaviour of that other method, you will cover the private method in the context it is used.

If you specifically test private methods, your tests will become tightly coupled to the implementation details (i.e. a good test wouldn't need to be changed if you refactored the implementation).

Disclaimer

If you still test in at the private method level, the compiler might in the future change and make the test fail (i.e. if the compiler made the method "properly" private, or if a future version of ECMAScript added visibility keywords, etc).

like image 121
Fenton Avatar answered Oct 02 '22 15:10

Fenton