jest-lighthouse

Helper functions to simplify Jest integration with Lighthouse

Usage no npm install needed!

<script type="module">
  import jestLighthouse from 'https://cdn.skypack.dev/jest-lighthouse';
</script>

README

jest-lighthouse

CI Coverage Status npm

Helper functions to simplify Jest integration with Lighthouse

npm install -D jest-lighthouse

toHaveLighthouseAudit

expect(lhr: ).toHaveLighthouseAudit(category: string, value?: <number | Score>)

This function check if lighthouse report pass audit

Using with jest-playwright

// jest-playwright.config.js
module.exports = {
  ...
  launchType: 'LAUNCH',
  launchOptions: {
    args: ['--remote-debugging-port=9222'],
    headless: true,
  },
}

// test file
const { getLighthouseReport } = require("jest-lighthouse");

...

describe('Lighthouse', () => {
  let lhr;
  beforeAll(async () => {
    const port = 9222; // same port as defined in launchOptions
    lhr = await getLighthouseReport(url, port);
  });
  it('passes an seo audit through Lighthouse', async () => {
    expect(lhr).toHaveLighthouseAudit('seo', 90);
  });
})

...

Using with jest-puppeteer

// test file
const { getLighthouseReport } = require("jest-lighthouse");

...

describe('Lighthouse', () => {
  jest.setTimeout(15000);
  let lhr;
  beforeAll(async () => {
    const port = new URL(browser.wsEndpoint()).port;
    lhr = await getLighthouseReport(url, port);
  });
  it('passes an accessibility audit through Lighthouse', async () => {
    expect(lhr).toHaveLighthouseAudit('performance', 'perfect');
  });
})

...