How do you test an interactive command line tool? πŸ€” My answer: use the web!

πŸ”Œ Recently I’ve been porting my cli tool pr-cli from Go to Deno (Typescript). I wanted to take it one step further and also add tests for it.

❓ I got ready to write some tests and realized I had no idea how to go about it. For non-interactive CLI tools you can test input/output, but how do you check and interact with prompts halfway through the program?

πŸ’‘ The solution I came to was using what I already know: The web and Playwright. Using a tool called ttyd to get a running terminal as a web app, I was able to use my application in my browser. Nice! Now I could use playwright to end-to-end test the program, just like I would any other web app.

Challenges

1️⃣ Once I had ttyd running I saw one big problem: It does its rendering on a canvas, so I wasn’t able to programmatically read the output. Luckily there was an easy solution: ttyd has an accessibility mode (screenReaderMode=true), which adds the output as hidden text for screen readers. I was able to utilize this to read the output with Playwright.

2️⃣ To interact with a command line utility through playwright isn’t the most natural experience. Most website UIs don’t consist of just pressing keys. I wrote a little CLI helper in playwright to make it easier to write tests.