Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(cli): add mock LHR support #477

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/cli/src/collect/node-runner.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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