Skip to content

Instantly share code, notes, and snippets.

@adrienjoly
Created March 11, 2023 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrienjoly/4d2d3d5331dbed36b8b1e705d0adc40e to your computer and use it in GitHub Desktop.
Save adrienjoly/4d2d3d5331dbed36b8b1e705d0adc40e to your computer and use it in GitHub Desktop.
Logging from Cypress E2E tests, for better troubleshooting
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
// 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',
],
})
// 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
}
#!/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