Skip to content

Commit

Permalink
tests(cli): add mock LHR support (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Oct 23, 2020
1 parent 14c5769 commit 51dc34a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
16 changes: 16 additions & 0 deletions packages/cli/src/collect/node-runner.js
Expand Up @@ -21,6 +21,20 @@ class LighthouseRunner {
);
}

/**
* @param {string} url
* @param {Partial<LHCI.CollectCommand.Options>} [options]
* @return {Promise<string>}
*/
static buildMockLhr(url, options = {}) {
/** @type {LH.Result} */
const lhr = JSON.parse(process.env.LHCITEST_MOCK_LHR || '{}');
lhr.requestedUrl = url;
lhr.finalUrl = url;
lhr.configSettings = lhr.configSettings || options.settings || {};
return Promise.resolve(JSON.stringify(lhr));
}

/**
* @param {string} url
* @param {Partial<LHCI.CollectCommand.Options>} options
Expand Down Expand Up @@ -64,6 +78,8 @@ class LighthouseRunner {
* @return {Promise<string>}
*/
run(url, options = {}) {
if (process.env.LHCITEST_MOCK_LHR) return LighthouseRunner.buildMockLhr(url, options);

/** @type {(lhr: string) => void} */
let resolve;
/** @type {(e: Error) => void} */
Expand Down
1 change: 1 addition & 0 deletions packages/cli/test/autorun-static-dir.test.js
Expand Up @@ -19,6 +19,7 @@ describe('Lighthouse CI autorun CLI', () => {
const {stdout, stderr, status} = await runCLI(
['autorun', '--collect.numberOfRuns=1', '--upload.uploadUrlMap=true'],
{
useMockLhr: true,
cwd: autorunDir,
env: {
LHCI_BUILD_CONTEXT__GITHUB_REPO_SLUG: 'GoogleChrome/lighthouse-ci-unit-tests',
Expand Down
21 changes: 14 additions & 7 deletions packages/cli/test/collect-static-dir.test.js
Expand Up @@ -19,6 +19,7 @@ describe('Lighthouse CI collect CLI', () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--url=/child/grandchild.html'],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -31,7 +32,7 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});
});

describe('with autodiscover limit', () => {
Expand All @@ -40,6 +41,7 @@ describe('Lighthouse CI collect CLI', () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--maxAutodiscoverUrls=1'],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -52,12 +54,13 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});

it('should collect all available pages from static dir', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--maxAutodiscoverUrls=0'],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -80,7 +83,7 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});
});

describe('with autodiscoverUrlBlocklist', () => {
Expand All @@ -95,6 +98,7 @@ describe('Lighthouse CI collect CLI', () => {
'--autodiscoverUrlBlocklist=/team.html',
],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -107,12 +111,13 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});

it('should filter index file', async () => {
const {stdout, stderr, status} = await runCLI(
['collect', '-n=1', '--staticDistDir=./', '--autodiscoverUrlBlocklist=/index.html'],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -133,7 +138,7 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});

it('should filter index file and board file', async () => {
const {stdout, stderr, status} = await runCLI(
Expand All @@ -145,6 +150,7 @@ describe('Lighthouse CI collect CLI', () => {
'--autodiscoverUrlBlocklist=/board.html',
],
{
useMockLhr: true,
cwd: staticDistDir,
}
);
Expand All @@ -163,14 +169,15 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});
});

describe('by default', () => {
const staticDistDir = path.join(__dirname, 'fixtures/collect-static-dir-autodiscover-limit');

it('should collect 5 pages from static dir', async () => {
const {stdout, stderr, status} = await runCLI(['collect', '-n=1', '--staticDistDir=./'], {
useMockLhr: true,
cwd: staticDistDir,
});
expect(stdout).toMatchInlineSnapshot(`
Expand All @@ -190,6 +197,6 @@ describe('Lighthouse CI collect CLI', () => {
`);
expect(stderr).toMatchInlineSnapshot(`""`);
expect(status).toEqual(0);
}, 180000);
});
});
});
17 changes: 14 additions & 3 deletions packages/cli/test/test-utils.js
Expand Up @@ -19,6 +19,15 @@ jest.setTimeout(30e3);

const CLI_PATH = path.join(__dirname, '../src/cli.js');
const UUID_REGEX = /[0-9a-f-]{36}/gi;
const MOCK_LHR = JSON.stringify({
audits: {
viewport: {
title: 'Does not have a `<meta name="viewport">` tag with `width` or `initial-scale`',
description: `[Learn More](https://web.dev/viewport/)`,
score: 0,
},
},
});

function getSqlFilePath() {
return `cli-test-${Math.round(Math.random() * 1e9)}.tmp.sql`;
Expand Down Expand Up @@ -134,12 +143,14 @@ function getCleanEnvironment(extraEnvVars) {

/**
* @param {string[]} args
* @param {{cwd?: string, env?: Record<string, string>}} [overrides]
* @param {{cwd?: string, env?: Record<string, string>, useMockLhr?: boolean}} [overrides]
* @return {{stdout: string, stderr: string, status: number, matches: {uuids: RegExpMatchArray}, childProcess: NodeJS.ChildProcess, exitPromise: Promise<void>}}
*/
function runCLIWithoutWaiting(args, overrides = {}) {
const {env: extraEnvVars, cwd} = overrides;
const {env: extraEnvVars, cwd, useMockLhr = false} = overrides;
const env = getCleanEnvironment(extraEnvVars);
if (useMockLhr) env.LHCITEST_MOCK_LHR = MOCK_LHR;

const childProcess = spawn('node', [CLI_PATH, ...args], {
cwd,
env,
Expand All @@ -162,7 +173,7 @@ function runCLIWithoutWaiting(args, overrides = {}) {

/**
* @param {string[]} args
* @param {{cwd?: string, env?: Record<string, string>}} [overrides]
* @param {{cwd?: string, env?: Record<string, string>, useMockLhr?: boolean}} [overrides]
* @return {Promise<{stdout: string, stderr: string, status: number, matches: {uuids: RegExpMatchArray}}>}
*/
async function runCLI(args, overrides = {}) {
Expand Down

0 comments on commit 51dc34a

Please sign in to comment.