-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[no ticket][risk=no] Puppeteer test log improvement (#4660)
* wip * wip * save jest log to file * winston logger * write individual test log * save * remove jest bail * remove @jest npm packages * jest bail alternative * ci retest * redundant --no-color option * consistent test report title * PR feedback * create winston file logger in jest-reporter.js * revert last commit * failed test log file name is FAILED * remove bail alternative bc it's not working for parallism
- Loading branch information
Showing
8 changed files
with
368 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const winston = require("winston"); | ||
|
||
module.exports = class JestReporter { | ||
|
||
constructor(globalConfig, options) { | ||
if (globalConfig.verbose === true) { | ||
throw Error("Verbose must be false or Console messages won't be available.") | ||
} | ||
this._globalConfig = globalConfig; | ||
this._options = options; | ||
this.logDir = this._options.outputdir || 'logs/jest'; | ||
this.summaryFile = this._options.filename || 'test-results-summary.json'; | ||
} | ||
|
||
onTestStart(test) { | ||
const time = new Date().toLocaleTimeString(); | ||
console.info(`Running ${path.parse(test.path).name} at ${time}`); | ||
} | ||
|
||
// @ts-ignore | ||
onTestResult(testRunConfig, testResult, runResults) { | ||
const testName = path.parse(testResult.testFilePath).name; | ||
let testLogName = `${this.logDir}/${testName}.log`; | ||
testResult.testResults.forEach((result) => { | ||
const status = result.status; | ||
if (status === 'failed') { | ||
testLogName = `${this.logDir}/${testName}-FAILED.log`; | ||
} | ||
}); | ||
|
||
const fileLogger = winston.createLogger({ | ||
level: process.env.LOG_LEVEL || "info", | ||
format: winston.format.combine( | ||
winston.format.splat(), | ||
winston.format.printf( (info) => {return `${info.message}`; }) | ||
), | ||
transports: [new winston.transports.File({ | ||
filename: testLogName, | ||
options: { flags: 'w' }, | ||
handleExceptions: true, | ||
}) | ||
], | ||
exitOnError: false | ||
}); | ||
|
||
// Read jest console messages and save to a log file. | ||
// Get all console logs. | ||
if (testResult.console && testResult.console.length > 0) { | ||
testResult.console.forEach((log) => { | ||
fileLogger.info(log.message); | ||
}); | ||
} | ||
|
||
// Get failure messages. | ||
testResult.testResults.forEach((result) => { | ||
fileLogger.info('----------------------------------------'); | ||
fileLogger.log('info', 'test name: %s', result.title); | ||
fileLogger.log('info', 'status: %s', result.status); | ||
// Get failure message. | ||
if (result.failureMessages) { | ||
const failure = result.failureMessages; | ||
fileLogger.log('info', 'failure: %s', failure); | ||
} | ||
}); | ||
console.log(`Save test log: ${testLogName}`); | ||
} | ||
|
||
// @ts-ignore | ||
onRunComplete(test, runResults) { | ||
runResults.testResults.forEach(suite => { | ||
const testFilePath = suite.testFilePath.split('e2e/')[1]; | ||
const failedTests = []; | ||
suite.testResults.forEach(result => { | ||
if (result.status === 'failed') { | ||
failedTests.push(`yarn test ${testFilePath}`); | ||
} | ||
}); | ||
if (failedTests.length > 0) { | ||
console.info(`**** To rerun failed tests:\n ${failedTests}`); | ||
} | ||
}); | ||
|
||
// Save test results to a file. | ||
if (!fs.existsSync(this.logDir)) { | ||
fs.mkdirSync(this.logDir); | ||
} | ||
fs.writeFileSync(`${this.logDir}/${this.summaryFile}`, JSON.stringify(runResults, null, 2)); | ||
console.info(`Save tests results summary file: ${this.logDir}/${this.summaryFile}`); | ||
return runResults; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const winston = require('winston'); | ||
const { createLogger, format } = winston; | ||
|
||
// Log to Console. | ||
const logger = createLogger({ | ||
level: process.env.LOG_LEVEL || "info", | ||
format: format.combine( | ||
format.prettyPrint(), | ||
format.splat(), | ||
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), | ||
format.printf( (info) => { | ||
return `${info.level.toUpperCase()}: [${info.timestamp}] - ${info.message}`; | ||
}), | ||
), | ||
transports: [ | ||
new winston.transports.Console({ handleExceptions: true}), | ||
], | ||
exitOnError: false | ||
}); | ||
|
||
module.exports = { logger }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.