How to mock ES6 class

Click for: original source

Madhan Ganesh post about mocking a class in JavaScript for the purpose of unit testing. The post targets developers who are coming to ES6 from environments that has features like dependency injection and interfaces.

Object is a first citizen in JavaScript land. The stunningly easy way to create an object makes this language simple to start. To create an object you start with object (and not class).

var person = {
  name: 'Velu'
};

Author then explains how to use:

  • Constructor function
  • Prototype as a mechanism to share a code
  • ES6 class
  • Mocking ES6 class

Basically to mock a method on ES6 class just get the reference of the function through class prototype and stub the same. Everything is well explained via code examples and link to the GitHub repository provided. Really useful for anybody new into ES6 JavaScript.

[Read More]

Tags javascript tdd nodejs programming