0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Cypressにlighthouseを導入する

Posted at

Cypress Lighthouse

Table of Contents

0. 前提
1. lighthouseをProjectに入れる
2. Cypress Commandを追加する

0. 前提

lighthouse
  version "9.6.1"
cypress
  version "9.6.1"
typescript
  version "4.6.4"

1. lighthouseをProjectに入れる

$ yarn add lighthouse

2. lighthouseコマンドを追加する

cypress/support/commands以下に新しいコマンドを記載する。

/// <reference types="cypress" />
declare namespace Cypress {
  interface Chainable<Subject> {
    lighthouse(url: string): Promise<any>
  }
}
Cypress.Commands.add("lighthouse", (url: string) => {
  // https://github.com/GoogleChrome/lighthouse#using-the-node-cli
  cy.exec(`lighthouse ${url} --output json --chrome-flags="--headless"`).then(({ stdout }) => {
    const { categories, ...data } = JSON.parse(stdout)
    console.log(categories, data)
    return Object.keys(categories).reduce((curr, key) => {
      return {
        ...curr,
        [key]: categories[key].score
      }
    }, {});
  });
})
cypress/integration/sample.spec.ts
describe("", () => {
    beforeEach(() => {
        cy.visit("/")
    })
    it("/", () => {
        cy.location().url().should("include", "/")
        cy.lighthouse("http://localhost:3000").then(res => {
            cy.wrap(res).its("accessibility").should("gt", 0.9)
            cy.wrap(res).its("best-practices").should("gt", 0.9)
            cy.wrap(res).its("performance").should("gt", 0.9)
            cy.wrap(res).its("pwa").should("gt", 0.9)
            cy.wrap(res).its("seo").should("gt",0.9)
        });
    })
})

上記のように対応するとScoreの計測が可能です。
ですが、めちゃめちゃ遅い。

Github ActionsでCloneを利用すれば、いくらか対応策はありそうです。。。

refs
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?