Problem

Sometimes, especially when you are writing unit tests, you want to create an (mocking) object on the fly, not to create a mocking class, which seems to heavy to do.

e.g.: the mocked object has 2 properties: a, and b. b shoule change whenever a changes, which is typical when using class.

Solution

You can use computed properties on object.

e.g.:

const obj = {
  a: 200,
  get b() {
    return this.a * 3;
  },
};

So b will always change when the a changes. It’s quick and easy to create a mocked object without creating the mock class implementation.

As always, happy coding!