Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

would like spec title available in lighthouse() callback in order to create uniquely titled logfiles #173

Open
bvandercar-vt opened this issue Jul 14, 2022 · 3 comments

Comments

@bvandercar-vt
Copy link

Before creating a feature request, make sure another issue is not opened for the same thing.

What would you like to see in the library?
In the "generating reports" example (https://mfrachet.github.io/cypress-audit/guides/lighthouse/reports.html#generating-html-reports) the issue with this is that each time cy.lighthouse is ran, the log file will be overwritten. Would like a way to obtain the current spec filename (in, say, the lighthouseReport object) in order to name lighthouse report files uniquely.

Why?
Reporting can only be ran once before it overwrites.

@MichaelRobsonSliide
Copy link

I was just trying to work around this today, it would be great if that feature could be implemented. For now I just append a timestamp to the report name, when you open the report you can see the page it ran on so it's now too bad for now.

      on('task', {
        pa11y: pa11y(console.log.bind(console)),

        // Set up lighthouse and make it write the html report to the reports folder
        // Report would be overwritten so append timestamp, the report says what page it ran against.
        lighthouse: lighthouse((lighthouseReport) => {
          fs.writeFile(
            `cypress/reports/lighthouseReport-${Date.now()}.html`,
            lighthouseReport.report,
            (err) => {
              if (err) {
                console.error(err)
              }
            },
          )
        }),
      })

@bvandercar-vt
Copy link
Author

bvandercar-vt commented Jul 18, 2022

I found a way. In plugins, do something like:

let lighthouseFilenameStr: string

module.exports = (on, config) => {
  on('task', {
    setlighthouseFilenameStr: (newLighthouseFilenameStr) => {
      lighthouseFilenameStr = newLighthouseFilenameStr
      return null
    },

    lighthouse: lighthouse((lighthouseReport: any) => {
      if (!fs.existsSync(lighthouseReportDirPath)) {
        fs.mkdirSync(lighthouseReportDirPath)
      }

      const outputExt = lighthouseReport.lhr.configSettings.output
      const lighthouseReportFilePath = `${lighthouseReportDirPath}/lighthouse_report_page_${lighthouseFilenameStr}.${outputExt}`
  })
}

then before you call cy.lighthouse, call: cy.task('setlighthouseFilenameStr', fileNameStr)

@bjankord
Copy link

bjankord commented Aug 3, 2022

Thanks @bvandercar-vt, I used that cy.task('setlighthouseFilenameStr', fileNameStr) method to get the file names I wanted.

I think it would be nice if we could pass the fileNameStr directly in our cy.lighthouse call that we could then use from the lightHouseReport object that is passed to the callback. Or add a second param to the callback that provides an object of additional configurations where the fileNameStr could be set that are passed to cy.lighthouse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants