@boost/core
Version:
Robust pipeline for creating dev tools that separate logic into routines and tasks.
76 lines (75 loc) • 2.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const testing_1 = require("@boost/debug/lib/testing");
exports.mockDebugger = testing_1.mockDebugger;
const Console_1 = __importDefault(require("./Console"));
const Plugin_1 = __importDefault(require("./Plugin"));
const Routine_1 = __importDefault(require("./Routine"));
const Task_1 = __importDefault(require("./Task"));
const Tool_1 = __importDefault(require("./Tool"));
function stubArgs(fields) {
return Object.assign({ $0: '', _: [] }, fields);
}
exports.stubArgs = stubArgs;
function stubPackageJson(fields) {
return Object.assign({ name: 'test-boost', version: '0.0.0' }, fields);
}
exports.stubPackageJson = stubPackageJson;
function stubToolConfig(config) {
return Object.assign({ debug: false, extends: [], locale: '', output: 2, plugins: [], reporters: [], settings: {}, silent: false, theme: 'default' }, config);
}
exports.stubToolConfig = stubToolConfig;
function mockTool(options, config, injectPlugin = true) {
const tool = new Tool_1.default(Object.assign({ appName: 'test-boost',
// Match fixtures path
appPath: path_1.default.join(process.cwd(), 'tests/__fixtures__/app') }, options));
// Register default plugins
if (injectPlugin) {
// @ts-ignore Ignore this for convenience
tool.registerPlugin('plugin', Plugin_1.default);
}
// Stub out standard objects
tool.args = stubArgs();
tool.config = stubToolConfig(config);
tool.package = stubPackageJson();
// Stub out methods
tool.debug = testing_1.mockDebugger();
// TODO Remove in 2.0
// @ts-ignore
tool.createDebugger = testing_1.mockDebugger;
// @ts-ignore Allow private access to avoid loaders
tool.initialized = true;
return tool;
}
exports.mockTool = mockTool;
function mockConsole(tool) {
const cli = new Console_1.default(tool, {
stderr: jest.fn(),
stdout: jest.fn(),
});
cli.err = jest.fn();
cli.out = jest.fn();
return cli;
}
exports.mockConsole = mockConsole;
class MockRoutine extends Routine_1.default {
execute(context, value) {
return Promise.resolve(value);
}
}
exports.MockRoutine = MockRoutine;
function mockRoutine(tool, key = 'key', title = 'Title') {
const routine = new MockRoutine(key, title);
routine.tool = tool;
routine.debug = testing_1.mockDebugger();
return routine;
}
exports.mockRoutine = mockRoutine;
function mockTask(action = null, title = 'Description') {
return new Task_1.default(title, action || jest.fn());
}
exports.mockTask = mockTask;