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.


'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?


1 Answer 1


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.
  • \$\begingroup\$ First alternative seems better to me. Also, what could be the name of the injected argument? \$\endgroup\$
    – CodeYogi
    Jun 30, 2017 at 16:17
  • \$\begingroup\$ @CodeYogi I would call it simply stdout \$\endgroup\$
    – janos
    Jun 30, 2017 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.