@scalvert/bin-tester
Version:
A test harness to invoke a CLI in a tmp directory
106 lines (102 loc) • 3.65 kB
TypeScript
import execa from 'execa';
import { Project } from 'fixturify-project';
declare class BinTesterProject extends Project {
private _dirChanged;
/**
* Constructs an instance of a BinTesterProject.
*
* @param {string} name - The name of the project. Used within the package.json as the name property.
* @param {string} version - The version of the project. Used within the package.json as the version property.
* @param {Function} cb - An optional callback for additional setup steps after the project is constructed.
*/
constructor(name?: string, version?: string, cb?: (project: Project) => void);
/**
* Runs `git init` inside a project.
*
* @returns {*} {execa.ExecaChildProcess<string>}
*/
gitInit(): execa.ExecaChildProcess<string>;
/**
* Changes a directory from inside the project.
*/
chdir(): Promise<void>;
/**
* Correctly disposes of the project, observing when the directory has been changed.
*
* @returns {void}
*/
dispose(): void;
}
interface BinTesterOptions<TProject> {
/**
* The absolute path to the bin to invoke
*/
binPath: string | (<TProject extends BinTesterProject>(project: TProject) => string);
/**
* An array of static arguments that will be used every time when running the bin
*/
staticArgs?: string[];
/**
* An optional function to use to create the project. Use this if you want to provide a custom implementation of a BinTesterProject.
*/
createProject?: () => Promise<TProject>;
}
interface RunBin {
/**
* A runBin implementation that takes no parameters.
*
* @returns {*} {execa.ExecaChildProcess<string>}
* @memberof RunBin
*/
(): execa.ExecaChildProcess<string>;
/**
* A runBin implementation that takes string varargs.
*
* @param {...RunBinArgs} args
* @returns {*} {execa.ExecaChildProcess<string>}
* @memberof RunBin
*/
(...args: [...binArgs: string[]]): execa.ExecaChildProcess<string>;
/**
* A runBin implementation that takes an execa.Options<string> object.
*
* @param {...RunBinArgs} args
* @returns {*} {execa.ExecaChildProcess<string>}
* @memberof RunBin
*/
(...args: [execaOptions: execa.Options<string>]): execa.ExecaChildProcess<string>;
/**
* A runBin implementation that takes string or an execa.Options<string> object varargs.
*
* @param {...RunBinArgs} args
* @returns {*} {execa.ExecaChildProcess<string>}
* @memberof RunBin
*/
(...args: [...binArgs: string[], execaOptions: execa.Options<string>]): execa.ExecaChildProcess<string>;
}
interface CreateBinTesterResult<TProject extends BinTesterProject> {
/**
* Runs the configured bin function via execa.
*/
runBin: RunBin;
/**
* Sets up the specified project for use within tests.
*/
setupProject: () => Promise<TProject>;
/**
* Sets up a tmp directory for use within tests.
*/
setupTmpDir: () => Promise<string>;
/**
* Tears the project down, ensuring the tmp directory is removed. Shoud be paired with setupProject.
*/
teardownProject: () => void;
}
/**
* Creates the bin tester API functions to use within tests.
*
* @param {BinTesterOptions<TProject>} options - An object of bin tester options
* @returns {CreateBinTesterResult<TProject>} - A project instance.
*/
declare function createBinTester<TProject extends BinTesterProject>(options: BinTesterOptions<TProject>): CreateBinTesterResult<TProject>;
export { BinTesterProject, createBinTester };