This package uses the NodeJs Readline and wrap it in a custom package in order to simplify its use and testing with jest mocks in Typescript.
fwl-readline
is not yet available on npm.
To install it, add this in your package.json
file:
"dependencies": {
"fwl-readline": "fewlines-education/fwl-readline#master"
}
// example.js
import * as readline from "fwl-readline";
We decided to simplify the initial API of the readline package. For this reason, we only export the createInterface
function.
This function takes an object
parameter with two keys:
input
: generally theprocess.stdin
output
: generally theprocess.stdout
export const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
In the example above, the created rl
will have the Reader
type:
interface Reader {
question: (query: string, callback: (answer: string) => void) => void;
close: () => void;
}
rl.question("Enter something\n> ", (input) => {
console.log(`User input: "${input}"`);
rl.close();
});
This example will output:
Enter something
> something
User input: "something"
jest.mock("fwl-readline", () => {
return {
createInterface: () => {
return {
question: () => {},
close: () => {},
};
},
};
});
import functionThatUseFwlRealine from "wherever-you-want"
// [...]
This way, the reader won't interfer when you start tests with jest
.