Check out a free preview of the full Testing and Modular Front-End course

The "I/O Shell" Lesson is part of the full, Testing and Modular Front-End course featured in this preview video. Here's what you'd learn in this lesson:

James introduces the concept of the I/O Shell, which allows for writing code that is easy to test and code that is reusable. By taking inputs and outputs such as reading or writing to a file push it up to the uppermost layer where it might be more configurable.


Transcript from the "I/O Shell" Lesson

>> Some other things that make testing easier, I don't know where I first encountered this idea, but it's really good idea for writing both reusable code and for writing code that's easy to test. It's the idea of the IO shell. That's where you try to defer all of the places where you're gonna use input/output.

So these are things where you what you're doing has kind of like an effect on the environment. So that might include something like reading from a file or writing to a file, these kinds of operations. If you can kind of push those up to the uppermost layer so that it's configurable, it'll be a lot easier to run your code in different kinds of environments.

Like a web browser, or on some other kind of system, or kind of in a testing, in a way where it's easy to kind of provide synthetic inputs and get synthetic outputs. An example of what that might look like is if you have a piece of code where normally you have a module called foo.

And by default, it uses the fs module behind the scenes. But you also maybe don't want it to do a bunch of stuff on your file system. So what you can do is pass in an optional fs argument that's like fake-fs. And it's just in memory, for example.

This will make your tests run a bit faster. Although I would caution a little bit, if you use too many of these fake libraries, sooner or later, you're gonna hit a bug in one of those libraries. And then it's like you're kind of a little bit far removed from the real system.

So it's just good to use a mix, I would say, of both real system calls when you can. But sometimes it's better to also use fake ones.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now