AngularJS, JavaScript, Testing, TypeScript

JavaScript Testing: How to Mock Static Variables

When we have $ctrl created by $componentController, how to mock static members of a class?

let $ctrl = $componentController("funny-component", null);

Either:

The Ugly Way

$ctrl.__proto__.constructor.staticVariable = true;

The Elegant Way

// At top of test spec file, import the controller:
import { FunnyComponentCtrl } from "./funny-component";

// Then in your test specs:
FunnyComponentCtrl.staticVariable = true;

The only thing is that, if you’re testing the controller with situations where static variables differ, or that some other part of your code depends on the variable, remember to put it back to normal after you changed it.

Standard