diff --git a/packages/kbn-test/jest-preset.js b/packages/kbn-test/jest-preset.js index d3795c775ea21..df9ed4cab4f51 100644 --- a/packages/kbn-test/jest-preset.js +++ b/packages/kbn-test/jest-preset.js @@ -130,4 +130,7 @@ module.exports = { globals: { structuredClone: {}, }, + + testResultsProcessor: + '/packages/kbn-test/src/jest/result_processors/logging_result_processor.js', }; diff --git a/packages/kbn-test/src/jest/result_processors/logging_result_processor.js b/packages/kbn-test/src/jest/result_processors/logging_result_processor.js new file mode 100644 index 0000000000000..b9b00dc8ee765 --- /dev/null +++ b/packages/kbn-test/src/jest/result_processors/logging_result_processor.js @@ -0,0 +1,36 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +const FAILURE_MESSAGE_TRIGGERS = ['but is not defined anymore']; +const log = (...args) => { + const loggable = args.map((arg) => + typeof arg === 'string' ? arg : JSON.stringify(arg, null, 2) + ); + process.stdout.write(`${loggable.join(' ')}\n`, 'utf8'); +}; +/** + * This processor looks for specific errors, and logs the result context of test suites where they occur. + * @param results + * @returns {*} + */ +module.exports = (results) => { + const resultsThatMatchTriggers = results.testResults.filter( + (e) => + e.failureMessage && + FAILURE_MESSAGE_TRIGGERS.some((trigger) => e.failureMessage.includes(trigger)) + ); + + if (resultsThatMatchTriggers.length !== 0) { + log('The following test suites failed, with notable errors:'); + resultsThatMatchTriggers.forEach((e) => { + log(` -> ${e.testFilePath}`, 'Details: ', e, '\n'); + }); + } + + return results; +};