cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
64 lines (63 loc) • 1.63 kB
JavaScript
import redent from "redent";
class GenericTypeError extends Error {
constructor(expectedString, received, matcherFn, context) {
super();
if (Error.captureStackTrace) {
Error.captureStackTrace(this, matcherFn);
}
let withType = "";
try {
withType = context.utils.printWithType(
"Received",
received,
context.utils.printReceived
);
} catch (e) {
}
this.message = [
context.utils.matcherHint(
`${context.isNot ? ".not" : ""}.${matcherFn.name}`,
"received",
""
),
"",
`${context.utils.RECEIVED_COLOR(
"received"
)} value must ${expectedString}.`,
withType
].join("\n");
}
}
class CliInstanceTypeError extends GenericTypeError {
constructor(...args) {
super("be a TestInstance", ...args);
}
}
function checkCliInstance(cliInstance, ...args) {
if (!(cliInstance && cliInstance.process && cliInstance.process.stdout)) {
throw new CliInstanceTypeError(cliInstance, ...args);
}
}
function display(context, value) {
return typeof value === "string" ? value : context.utils.stringify(value);
}
function getMessage(context, matcher, expectedLabel, expectedValue, receivedLabel, receivedValue) {
return [
`${matcher}
`,
`${expectedLabel}:
${context.utils.EXPECTED_COLOR(
redent(display(context, expectedValue), 2)
)}`,
`${receivedLabel}:
${context.utils.RECEIVED_COLOR(
redent(display(context, receivedValue), 2)
)}`
].join("\n");
}
export {
CliInstanceTypeError,
checkCliInstance,
getMessage
};
//# sourceMappingURL=utils.js.map