I have a project with the cypress automation. I am trying to add the plugin such as the cypress in my project but after changing my index.js in plugin folder.It shows me the error of
cy.lighthouse() is not a function
index.js file
const { lighthouse, pa11y, prepareAudit } = require("cypress-audit");
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
};
can any one help on this issue. Thanks!
CodePudding user response:
If you take a look at the docs, this should be the full cypress/plugins/index.js
Cypress v9 - cypress/plugins/index.js
const { lighthouse, pa11y, prepareAudit } = require("cypress-audit");
module.exports = (on, config) => {
on("before:browser:launch", (browser = {}, launchOptions) => {
prepareAudit(launchOptions);
});
on("task", {
lighthouse: lighthouse(), // calling the function is important
pa11y: pa11y(), // calling the function is important
});
};
But your error message "cy.lighthouse() is not a function" is caused by missing line in cypress/support/index.js
which you also add
Cypress v9 - cypress/support/index.js
import "cypress-audit/commands";
For latest Cypress
Cypress v10 - cypress.config.js
const { defineConfig } = require('cypress')
const { lighthouse, pa11y, prepareAudit } = require("cypress-audit");
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1234',
setupNodeEvents(on, config) {
on("before:browser:launch", (browser = {}, launchOptions) => {
prepareAudit(launchOptions);
});
on("task", {
lighthouse: lighthouse(), // calling the function is important
pa11y: pa11y(), // calling the function is important
});
},
})
Cypress v10 - cypress/support/e2e.js
import "cypress-audit/commands";
CodePudding user response:
This code works fine for me:
const { lighthouse, pa11y, prepareAudit } = require("cypress-audit");
module.exports = (on, config) => {
on("before:browser:launch", (browser = {}, launchOptions) => {
prepareAudit(launchOptions);
});
on("task", {
lighthouse: lighthouse(), // calling the function is important
pa11y: pa11y(), // calling the function is important
});
};
Cypress v9 - cypress/support/index.js
import "cypress-audit/commands";