0.0.3 • Published 2 years ago
@scalvert/cli-test-harness v0.0.3
@scalvert/cli-test-harness
Provides a test harness for node CLIs that allow you to run tests against a real project.
Install
npm add @scalvert/cli-test-harness --save-dev
# or
yarn add @scalvert/cli-test-harness --dev
Usage
@scalvert/cli-test-harness
uses two libraries to provide the test harness:
fixturify-project
: Allows you to dynamically create test fixtures using real directories and files in a tmp directoryexeca
: A better replacement forchild_process.exec
It combines the above and provides an API for running a binary with a set of arguments against a real project structure, thus mimicking testing a real environment.
import { createCLITestHarness } from '@scalvert/cli-test-harness';
describe('Some tests', () => {
let project;
let { setupProject, teardownProject, runBin } = createCliTestHarness({
binPath: 'node_modules/.bin/someBin',
});
beforeEach(() => {
project = await setupProject();
});
afterEach(() => {
await teardownProject();
});
// Run the bin and do something with the result
test('a test', () => {
const result = await runBin();
expect(result.stdout).toBe('Did some stuff');
});
// Write a file with contents to the tmp directory
test('another test', () => {
project.write({
'some/file.txt': 'some content',
});
const result = await runBin({
args: ['--path', 'some/file.txt'],
});
expect(result.stdout).toBe('Read "some/file.txt"');
});
});
API
class CLITestProject extends Project {
private _dirChanged;
constructor(name: string, version?: string, cb?: (project: Project) => void);
gitInit(): execa.ExecaChildProcess<string>;
chdir(): void;
dispose(): void;
}
interface CLITestHarnessOptions {
binPath: string;
projectConstructor?: any;
}
interface RunOptions {
args?: string[];
execaOptions?: execa.Options<string>;
}
interface CreateCLITestHarnessResult<TProject extends CLITestProject> {
runBin: (runOptions?: RunOptions) => execa.ExecaChildProcess<string>;
setupProject: () => Promise<TProject>;
setupTmpDir: () => Promise<string>;
teardownProject: () => void;
}
function createCLITestHarness<TProject extends CLITestProject>(options: CLITestHarnessOptions): CreateCLITestHarnessResult<TProject>;