fern-jest-client
Version:
Jest test reporter client for Fern platform - captures and sends test execution data to fern-reporter
111 lines • 4.34 kB
JavaScript
;
/**
* Jest custom reporter for Fern platform
* Integrates with Jest's reporter system to capture and send test results
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.FernReporter = void 0;
const reporters_1 = require("@jest/reporters");
const client_1 = require("../client");
const mapper_1 = require("../utils/mapper");
const git_1 = require("../utils/git");
const config_1 = require("../config");
class FernReporter extends reporters_1.BaseReporter {
constructor(globalConfig, options) {
super();
// Configuration with environment variable fallbacks
this.projectId = options.projectId || (0, config_1.getEnvVar)(config_1.ENV_VARS.FERN_PROJECT_ID) || 'unknown-project';
this.projectName = options.projectName || (0, config_1.getEnvVar)('FERN_PROJECT_NAME') || this.projectId;
this.enabled = options.enabled !== false && (0, config_1.getEnvVar)(config_1.ENV_VARS.FERN_ENABLED) !== 'false';
if (!this.enabled) {
console.log('Fern reporter is disabled');
return;
}
const baseUrl = options.baseUrl || (0, config_1.getEnvVar)(config_1.ENV_VARS.FERN_REPORTER_BASE_URL);
const timeout = options.timeout;
this.client = (0, client_1.createFernClient)(this.projectId, {
baseUrl,
timeout
});
console.log(`Fern reporter initialized for project: ${this.projectId}`);
console.log(`Reporting to: ${this.client.getBaseUrl()}`);
}
/**
* Called when all tests have completed
*/
async onRunComplete(contexts, results) {
if (!this.enabled) {
return;
}
try {
console.log('\nCollecting test results for Fern reporting...');
// Get git and CI information
const [gitInfo, ciInfo] = await Promise.all([
(0, git_1.getGitInfo)(),
Promise.resolve((0, git_1.getCIInfo)())
]);
// Convert Jest results to Fern format
const testRun = (0, mapper_1.mapJestResultsToTestRun)(results, this.projectId, this.projectName, gitInfo, ciInfo);
// Log summary
console.log((0, mapper_1.generateTestSummary)(testRun));
// Convert to API input format
const createInput = (0, mapper_1.mapTestRunToCreateInput)(testRun);
// Send to Fern API
console.log('\nSending test results to Fern...');
await this.client.report(createInput);
console.log('✅ Test results successfully sent to Fern\n');
}
catch (error) {
console.error('❌ Failed to send test results to Fern:', error);
// Don't fail the test run if reporting fails
if (process.env.FERN_FAIL_ON_ERROR === 'true') {
throw error;
}
}
}
/**
* Called when a test starts
*/
onTestStart(test) {
// Optional: Log test start for debugging
if (process.env.FERN_DEBUG === 'true') {
console.log(`Starting test: ${test.path}`);
}
}
/**
* Called when a test file completes
*/
onTestResult(test, testResult, aggregatedResult) {
// Optional: Log test completion for debugging
if (process.env.FERN_DEBUG === 'true') {
const { numFailingTests, numPassingTests, numPendingTests } = testResult;
console.log(`Completed test: ${test.path} (${numPassingTests} passed, ${numFailingTests} failed, ${numPendingTests} pending)`);
}
}
/**
* Test API connectivity
*/
async testConnection() {
if (!this.enabled) {
return false;
}
try {
const isHealthy = await this.client.ping();
if (isHealthy) {
console.log('✅ Fern API connection successful');
}
else {
console.warn('⚠️ Fern API is not responding correctly');
}
return isHealthy;
}
catch (error) {
console.error('❌ Failed to connect to Fern API:', error);
return false;
}
}
}
exports.FernReporter = FernReporter;
// Export for Jest configuration
exports.default = FernReporter;
//# sourceMappingURL=index.js.map