DEV Community

Cover image for End-to-end test your CLI tools
Giuliano1993
Giuliano1993

Posted on

End-to-end test your CLI tools

Good morning, everyone, and happy MonDEV! ☕

We're back in the midst of our activities, between code flow, content creation, and daily life! Before the holidays, I had a couple of open issues on a some repos that I had left pending because I hadn't found the right way to approach them. What kind of issues were they? As you know, I love developing CLI tools. The problem arises when it comes time to test them because, although it's always possible to test individual functions, end-to-end testing of the user flow through the various steps of a CLI application is hardly supported by the main testing frameworks, from what I've observed.

So, I was a bit uncertain about how to proceed in wanting to write tests for these tools, and it's precisely in this moment that I stumbled upon today's tool, almost by chance, as often happens!
The project I wanted to talk to you about is called cli-testing-library, and it's a framework-agnostic npm package, that allows you to simulate the input of the user, thus enabling end-to-end testing for the command line and searching for strings within the terminal (with various query methods). All of this, of course, combined with your favorite testing framework, although, as specified, it is a tool that works best with Jest.

A drawback that this package probably has is probably the documentation, a bit fragmented and scattered (indeed, I'm thinking of a PR, but if you want to anticipate me, feel free, of course ;) ). For this reason, I'll leave you with some steps that I followed for the initial setup:

  • Install cli-testing-library and jest as dev dependency
npm install --save-dev cli-testing-library jest
Enter fullscreen mode Exit fullscreen mode
  • Create a configuration file for Jest
npm init jest@latest
Enter fullscreen mode Exit fullscreen mode
  • Create a file setup.jest.js where we import matchers from cli-testing-library that will allow us to read the output of the CLI. Here's my setup.jest.js
const matchers = require('jest-extended');
const {toBeInTheConsole} = require('cli-testing-library/extend-expect')
expect.extend(matchers);
afterEach(() => {
  jest.useRealTimers();
});
Enter fullscreen mode Exit fullscreen mode
  • At this point, in the jest.config.mjs file, let's link the setup file with this line
//...
setupFilesAfterEnv: ["./setup.jest.js"],
//...
Enter fullscreen mode Exit fullscreen mode
  • We're ready to launch our first test: in tests/welcome.test.js let's try a basic test:
test('Welcome', async () => {
    const {findByText} = await render('node', [
      resolve(__dirname, '../index.js')
    ])
    const instance = await findByText('Welcome')
    expect(instance).toBeInTheConsole()
})
Enter fullscreen mode Exit fullscreen mode

Very simple, once you know how to set it up! ;)
Now it's much easier to test CLI tools, and in the coming weeks, I intend to implement as many tests as possible using this tool, now that I've finally found the tool that does exactly what I was looking for!

Have you ever written tests for a CLI app? What tools have you used for that? I'm very curious about it!

Scheduling Deployment on Netlify

Have you ever wondered how to deploy your GitHub repos on Netlify using only its APIs? Maybe yes, maybe no, but for the Server manager that I've been developing for some time now, I wondered about it and how. Not finding answers in the official documentation, I got lost between forums, Stack Overflow, and other amazing places on the web, until I put together a functional solution that I wanted to share through an article for those who didn't want to dive into this black hole like me! So, for those who have the desire and time for further reading, go to my new dev.to article where I tell you how to perform a deploy entirely with Python (but you'll also find the link to the repo with the Node version of the project, which I'm currently working on).

As always, I hope you found some interesting insights in these lines, and I'm always waiting for feedback on Dev.to or on social media, both in the comments and in DMs! I just have to wish you a good start to the week!
Happy Coding 0_1

Top comments (0)