Created
March 11, 2023 11:02
-
-
Save adrienjoly/4d2d3d5331dbed36b8b1e705d0adc40e to your computer and use it in GitHub Desktop.
Logging from Cypress E2E tests, for better troubleshooting
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
import { defineConfig } from 'cypress' | |
const cypressConfig = defineConfig({ | |
video: true, | |
chromeWebSecurity: false, | |
e2e: { | |
baseUrl: 'http://localhost:3000', | |
supportFile: 'cypress/support/e2e/index.js', | |
setupNodeEvents(on, config) { | |
require('cypress-terminal-report/src/installLogsPrinter')(on, { | |
// cf https://www.npmjs.com/package/cypress-terminal-report#logging-to-files | |
printLogsToConsole: 'always', // default: 'onFail' | |
printLogsToFile: 'always', // default: 'onFail' | |
outputRoot: config.projectRoot + '/cypress/logs/', | |
outputTarget: { | |
'cypress-terminal-report.txt': 'txt', | |
'cypress-terminal-report.json': 'json', | |
}, | |
}) | |
// usage: cy.task('log', 'something to log'); | |
on('task', { | |
log(message) { | |
console.log('🔵', message) // prints the message to stdout | |
return null | |
}, | |
}) | |
}, | |
}, | |
}) | |
export default cypressConfig |
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
// file: cypress/support/e2e/index.js | |
require('cypress-terminal-report/src/installLogsCollector')({ | |
collectTypes: [ | |
'cons:log', | |
'cons:info', | |
'cons:warn', | |
'cons:error', | |
'cons:debug', | |
'cy:log', | |
'cy:xhr', | |
'cy:fetch', | |
'cy:request', | |
'cy:intercept', | |
'cy:command', | |
], | |
}) |
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
// sample test | |
describe("<System under test>", () => { | |
const actions = getPageActions() | |
it("logs in", () => { | |
actions.login() // "actions.login()" will be logged in stdout | |
}) | |
}) | |
function getPageActions() { | |
const actions = { | |
login(){ /* ... */ }, | |
navigate(){ /* ... */ }, | |
} | |
const methodLogger = { | |
get(target, prop) { | |
if (typeof target[prop] === 'function') { | |
cy.task('log', `🧵 actions.${prop}()`) | |
} | |
return target[prop] | |
}, | |
} | |
return new Proxy(actions, methodLogger) as typeof sutMethods | |
} |
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
#!/usr/bin/env sh | |
while ! curl -sS http://localhost:3000 > /dev/null ; do | |
echo "will retry in 5 seconds"; sleep 5; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment