Search code examples
javascriptgoogle-chromelighthouse

How to set a custom configuration in Lighthouse programmaticaly?


I'm trying to do the equivalent of this lighthouse command but I can't find out how.

lighthouse --config-path=custom-config.js https://www.example.com

Does anyone have any examples to share, on how to set a custom configuration file (with custom gatherers and audits) for lighthouse programmatically?


Solution

  • I managed to find out the answer myself so I am posting it below:

    var url = "https://www.example.com";
    var config = {};
    // The 2nd parameter in the lighthouse function is some of the flags you can pass to lighthouse. I'm posting a few examples below:
    conig.lighthouseFlags = {
      "output": [
        "html"
      ],
      "emulatedFormFactor": "none",
      "disableStorageReset": "true",
      "disableDeviceEmulation": "true"
    };
    // The 3rd parameter includes the configuration on how to run the tests in lighthouse
    conig.lighthouseConfigs = {
      "extends": "lighthouse:default",
      "passes": [{
        "passName": "defaultPass",
        "gatherers": [
          "gatherer_more_seo"
        ]
      }],
      "audits": [
        "audit_seo_something"
      ],
      "categories": {
        "more_seo": {
          "title": "Friendly title",
          "description": "Friendly description",
          "auditRefs": [{
            "id": "more-seo-field",
            "weight": 1
          }]
        }
      }
    };
    
    // This how I call lighthouse programmatically
    const lhr = await lighthouse(url, config.lighthouseFlags, config.lighthouseConfigs);