@sprucelabs/spruce-cli
Version:
Command line interface for building Spruce skills.
125 lines • 4.77 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 mercury_event_emitter_1 = require("@sprucelabs/mercury-event-emitter");
const mercury_types_1 = require("@sprucelabs/mercury-types");
const schema_1 = require("@sprucelabs/schema");
const spruce_skill_utils_1 = require("@sprucelabs/spruce-skill-utils");
const SpruceError_1 = __importDefault(require("../../errors/SpruceError"));
const JestJsonParser_1 = __importDefault(require("../../tests/JestJsonParser"));
class TestRunner extends mercury_event_emitter_1.AbstractEventEmitter {
cwd;
commandService;
wasKilled = false;
testResults = { totalTestFiles: 0 };
constructor(options) {
super(testRunnerContract);
this.cwd = options.cwd;
this.commandService = options.commandService;
}
async run(options) {
this.wasKilled = false;
const jestPath = this.resolvePathToJest();
const debugArgs = (options?.debugPort ?? 0) > 0
? `--inspect=${options?.debugPort}`
: ``;
const pattern = options?.pattern ?? '';
let escapeShell = function (cmd) {
return ('--testPathPatterns="' +
cmd.replace(/(["\s'$`\\])/g, '\\$1') +
'"');
};
const command = `node --experimental-vm-modules --unhandled-rejections=strict ${debugArgs} ${jestPath} --reporters="@sprucelabs/jest-json-reporter" --testRunner="jest-circus/runner" --passWithNoTests ${pattern ? escapeShell(pattern) : ''}`;
const parser = new JestJsonParser_1.default();
this.testResults = {
totalTestFiles: 0,
};
try {
await this.commandService.execute(command, {
forceColor: true,
onError: async (data) => {
const isDebugMessaging = this.isDebugMessage(data);
if (!isDebugMessaging) {
await this.emit('did-error', { message: data });
}
},
onData: async (data) => {
parser.write(data);
this.testResults = parser.getResults();
await this.emit('did-update', { results: this.testResults });
},
});
}
catch (err) {
if (!this.testResults.totalTestFiles) {
throw err;
}
}
return { ...this.testResults, wasKilled: this.wasKilled };
}
isDebugMessage(data) {
return (data.search(/^ attached/i) === 0 ||
data.search(/^ listening/i) === 0 ||
data.search(/^waiting for the /i) === 0);
}
hasFailedTests() {
return (this.testResults.totalFailed ?? 0) > 0;
}
hasSkippedTests() {
return (this.testResults.totalSkipped ?? 0) > 0;
}
kill() {
this.wasKilled = true;
this.commandService.kill();
}
resolvePathToJest() {
const jestPath = 'node_modules/.bin/jest';
const fullPath = spruce_skill_utils_1.diskUtil.resolvePath(this.cwd);
const pathParts = fullPath.split(path_1.default.sep);
while (pathParts.length > 0) {
const path = path_1.default.sep +
path_1.default.join(...pathParts) +
path_1.default.sep +
jestPath;
if (spruce_skill_utils_1.diskUtil.doesFileExist(path)) {
return path;
}
pathParts.pop();
}
throw new SpruceError_1.default({ code: 'INVALID_TEST_DIRECTORY', dir: this.cwd });
}
}
exports.default = TestRunner;
const testRunnerContract = (0, mercury_types_1.buildEventContract)({
eventSignatures: {
'did-error': {
emitPayloadSchema: (0, schema_1.buildSchema)({
id: 'testRunnerDidErrorEmitPayload',
fields: {
message: {
type: 'text',
isRequired: true,
},
},
}),
},
'did-update': {
emitPayloadSchema: (0, schema_1.buildSchema)({
id: 'testRunnerDidUpdateEmitPayload',
fields: {
results: {
type: 'raw',
isRequired: true,
options: {
valueType: 'SpruceTestResults',
},
},
},
}),
},
},
});
//# sourceMappingURL=TestRunner.js.map