Calculating the terminal width and height As part of the bigger problem I am trying to solve smaller problems first and hence below is the code to find the terminal width and height.

Code

'use strict';

class Terminal {
  width() {
    return process.stdout.columns || 80
  }

  height() {
    return process.stdout.rows || 60
  }
}

module.exports = Terminal


How can I make the above code testable? can I use some better abstraction?
What is the functionality of this class?
Get width and height from process.stdout,
an external resource,
or else fall back to default values.

You can make this testable by making it possible to inject process.stdout, the external resource.
Then in the test you can inject an object that you control,
and verify that Terminal uses that object correctly or else falls back to the defaults correctly.

To make process.stdout injectable, you have at least two options:


Add an optional constructor argument. By default use process.stdout, only the test method will inject a custom object.
Add a _stdout function, that will be used by width and height, and in the test override it appropriately.