js-tests-results-collector
Version:
Universal test results collector for Jest, Jasmine, Mocha, Cypress, and Playwright that sends results to Buddy Works API
74 lines (64 loc) • 2.55 kB
JavaScript
const sessionManager = require('../../core/session-manager');
const TestResultMapper = require('../../utils/test-result-mapper');
const Logger = require('../../utils/logger');
class JestReporter {
constructor(globalConfig, options) {
this.globalConfig = globalConfig;
this.options = options;
this.logger = new Logger('JestReporter');
}
async onRunStart() {
this.logger.debug('Jest test run started');
try {
// Create session at the start of the test run
await sessionManager.createSession();
this.logger.debug('Session created at Jest test run start');
} catch (error) {
this.logger.error('Error creating session at Jest test run start', error);
// Mark this as a framework error
sessionManager.markFrameworkError();
}
}
async onTestResult(test, testResult, aggregatedResult) {
try {
// Debug logging to see what Jest sends
this.logger.debug('Jest onTestResult called:', {
testFilePath: testResult.testFilePath,
numPassingTests: testResult.numPassingTests,
numFailingTests: testResult.numFailingTests,
numPendingTests: testResult.numPendingTests,
numTodoTests: testResult.numTodoTests,
skipped: testResult.skipped,
totalTestResults: testResult.testResults.length
});
// Process each individual test case
for (const testCase of testResult.testResults) {
const mappedResult = TestResultMapper.mapJestResult(testCase, testResult);
// Log the mapped result for debugging
this.logger.debug('Mapped test result:', {
name: mappedResult.name,
status: mappedResult.status,
originalStatus: testCase.status
});
await sessionManager.submitTestCase(mappedResult);
}
} catch (error) {
this.logger.error('Error processing Jest test result', error);
// Mark this as a framework error since we failed to process test results
sessionManager.markFrameworkError();
}
}
async onRunComplete(contexts, results) {
this.logger.debug('Jest test run completed');
try {
// Close the session when all tests are finished
await sessionManager.closeSession();
this.logger.debug('Session closed after Jest test completion');
} catch (error) {
this.logger.error('Error closing session after Jest test completion', error);
// Mark this as a framework error
sessionManager.markFrameworkError();
}
}
}
module.exports = JestReporter;