Skip to content

Commit

Permalink
[Ops] Add debug logging to certain jest error cases (elastic#175988)
Browse files Browse the repository at this point in the history
## Summary
We see quite frequently this kind of error in Jest unit tests:
```
Error: The `document` global was defined when React was initialized, but is not defined anymore. This can happen in a test environment if a component schedules an update from an asynchronous callback, but the test has already finished running. To solve this, you can either unmount the component at the end of your test (and ensure that any asynchronous operations get canceled in `componentWillUnmount`), or you can change the test itself to be asynchronous.
```

As the test execution stops due to this runtime error, and the console
logging is disabled in CI, we don't see a lot of the context of the
error.

This PR adds minimal extra logging when the case of this error happens.
  • Loading branch information
delanni authored Feb 1, 2024
1 parent 9b5d040 commit 993c174
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ module.exports = {
globals: {
structuredClone: {},
},

testResultsProcessor:
'<rootDir>/packages/kbn-test/src/jest/result_processors/logging_result_processor.js',
};
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 993c174

Please sign in to comment.