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(smokehouse): convert to ES modules #13046

Merged
merged 12 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@

/* eslint-disable no-console */

const http = require('http');
const zlib = require('zlib');
const path = require('path');
const fs = require('fs');
const glob = require('glob');
const mime = require('mime-types');
const parseQueryString = require('querystring').parse;
const parseURL = require('url').parse;
const URLSearchParams = require('url').URLSearchParams;
const {LH_ROOT} = require('../../../root.js');
import http from 'http';
import zlib from 'zlib';
import path from 'path';
import fs from 'fs';
import {parse as parseQueryString} from 'querystring';
import {parse as parseURL} from 'url';
import {URLSearchParams} from 'url';

import mime from 'mime-types';
import glob from 'glob';
import esMain from 'es-main';

import {LH_ROOT} from '../../../root.js';

const HEADER_SAFELIST = new Set(['x-robots-tag', 'link', 'content-security-policy']);

class Server {
baseDir = __dirname;
baseDir = `${LH_ROOT}/lighthouse-cli/test/fixtures`;

constructor() {
this._server = http.createServer(this._requestHandler.bind(this));
Expand Down Expand Up @@ -172,7 +175,7 @@ class Server {

// Create an index page that lists the available test pages.
if (filePath === '/') {
const fixturePaths = glob.sync('**/*.html', {cwd: __dirname});
const fixturePaths = glob.sync('**/*.html', {cwd: this.baseDir});
const html = `
<html>
<h1>Smoke test fixtures</h1>
Expand All @@ -187,7 +190,7 @@ class Server {

if (filePath.startsWith('/dist/gh-pages')) {
// Rewrite lighthouse-viewer paths to point to that location.
absoluteFilePath = path.join(__dirname, '/../../../', filePath);
absoluteFilePath = path.join(this.baseDir, '/../../../', filePath);
}

// Disallow file requests outside of LH folder
Expand Down Expand Up @@ -244,18 +247,18 @@ serverForOnline._server.on('error', e => console.error(e.code, e));
serverForOffline._server.on('error', e => console.error(e.code, e));

// If called via `node static-server.js` then start listening, otherwise, just expose the servers
if (require.main === module) {
if (esMain(import.meta)) {
// Start listening
const onlinePort = 10200;
const offlinePort = 10503;
serverForOnline.listen(onlinePort, 'localhost');
serverForOffline.listen(offlinePort, 'localhost');
console.log(`online: listening on http://localhost:${onlinePort}`);
console.log(`offline: listening on http://localhost:${offlinePort}`);
} else {
module.exports = {
Server,
server: serverForOnline,
serverForOffline,
};
}

export {
Server,
serverForOnline as server,
serverForOffline
};
4 changes: 2 additions & 2 deletions lighthouse-cli/test/smokehouse/frontends/back-compat-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ function updateTestDefnFormat(allTestDefns) {
return expandedTestDefns.flat();
}

module.exports = {
updateTestDefnFormat,
export {
updateTestDefnFormat
};
9 changes: 5 additions & 4 deletions lighthouse-cli/test/smokehouse/frontends/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

/* eslint-disable no-console */

const cloneDeep = require('lodash.clonedeep');
const smokeTests = require('../test-definitions/core-tests.js');
const {runSmokehouse} = require('../smokehouse.js');
import cloneDeep from 'lodash.clonedeep';

import smokeTests from '../test-definitions/core-tests.js';
import {runSmokehouse} from '../smokehouse.js';

/**
* @param {Smokehouse.SmokehouseLibOptions} options
Expand Down Expand Up @@ -43,4 +44,4 @@ async function smokehouse(options) {
return runSmokehouse(modifiedTests, smokehouseOptions);
}

module.exports = smokehouse;
export {smokehouse};
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/frontends/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
*/

// Smokehouse is runnable from within node, so just a no-op for now.
module.exports = require('../smokehouse.js');
export * from '../smokehouse.js';
35 changes: 20 additions & 15 deletions lighthouse-cli/test/smokehouse/frontends/smokehouse-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@

/* eslint-disable no-console */

const fs = require('fs');
const path = require('path');
const cloneDeep = require('lodash.clonedeep');
const yargs = require('yargs');
const log = require('lighthouse-logger');
const {runSmokehouse} = require('../smokehouse.js');
const {updateTestDefnFormat} = require('./back-compat-util.js');
const {LH_ROOT} = require('../../../../root.js');
import path from 'path';
import fs from 'fs';
import url from 'url';

import cloneDeep from 'lodash.clonedeep';
import yargs from 'yargs';
import * as yargsHelpers from 'yargs/helpers';
import log from 'lighthouse-logger';

import {runSmokehouse} from '../smokehouse.js';
import {updateTestDefnFormat} from './back-compat-util.js';
import {LH_ROOT} from '../../../../root.js';

const coreTestDefnsPath =
path.join(LH_ROOT, 'lighthouse-cli/test/smokehouse/test-definitions/core-tests.js');
`${LH_ROOT}/lighthouse-cli/test/smokehouse/test-definitions/core-tests.js`;

/**
* Possible Lighthouse runners. Loaded dynamically so e.g. a CLI run isn't
Expand Down Expand Up @@ -115,7 +119,8 @@ function pruneExpectedNetworkRequests(testDefns, takeNetworkRequestUrls) {
* CLI entry point.
*/
async function begin() {
const rawArgv = yargs
const y = yargs(yargsHelpers.hideBin(process.argv));
const rawArgv = y
.help('help')
.usage('node $0 [<options>] <test-ids>')
.example('node $0 -j=1 pwa seo', 'run pwa and seo tests serially')
Expand Down Expand Up @@ -159,7 +164,7 @@ async function begin() {
describe: 'Run all available tests except the ones provided',
},
})
.wrap(yargs.terminalWidth())
.wrap(y.terminalWidth())
.argv;

// Augmenting yargs type with auto-camelCasing breaks in [email protected] and @types/[email protected],
Expand All @@ -173,13 +178,13 @@ async function begin() {
if (argv.runner === 'bundle') {
console.log('\n✨ Be sure to have recently run this: yarn build-all');
}
const lighthouseRunner = require(runnerPath).runLighthouse;
const {runLighthouse} = await import(runnerPath);

// Find test definition file and filter by requestedTestIds.
let testDefnPath = argv.testsPath || coreTestDefnsPath;
testDefnPath = path.resolve(process.cwd(), testDefnPath);
const requestedTestIds = argv._;
const rawTestDefns = require(testDefnPath);
const {default: rawTestDefns} = await import(url.pathToFileURL(testDefnPath).href);
const allTestDefns = updateTestDefnFormat(rawTestDefns);
const invertMatch = argv.invertMatch;
const testDefns = getDefinitionsToRun(allTestDefns, requestedTestIds, {invertMatch});
Expand All @@ -192,7 +197,7 @@ async function begin() {
try {
// If running the core tests, spin up the test server.
if (testDefnPath === coreTestDefnsPath) {
({server, serverForOffline} = require('../../fixtures/static-server.js'));
({server, serverForOffline} = await import('../../fixtures/static-server.js'));
server.listen(10200, 'localhost');
serverForOffline.listen(10503, 'localhost');
takeNetworkRequestUrls = server.takeRequestUrls.bind(server);
Expand All @@ -204,7 +209,7 @@ async function begin() {
retries,
isDebug: argv.debug,
useFraggleRock: argv.fraggleRock,
lighthouseRunner,
lighthouseRunner: runLighthouse,
takeNetworkRequestUrls,
};

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/lib/child-process-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ class ChildProcessError extends Error {
}
}

module.exports = ChildProcessError;
export {ChildProcessError};
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/lib/concurrent-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,4 @@ class ConcurrentMapper {
}
}

module.exports = ConcurrentMapper;
export {ConcurrentMapper};
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/lib/local-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class LocalConsole {
}
}

module.exports = LocalConsole;
export {LocalConsole};
11 changes: 6 additions & 5 deletions lighthouse-cli/test/smokehouse/lighthouse-runners/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
* Currently uses `lighthouse-dt-bundle.js`.
*/

const ChromeLauncher = require('chrome-launcher');
const ChromeProtocol = require('../../../../lighthouse-core/gather/connections/cri.js');
import ChromeLauncher from 'chrome-launcher';

import ChromeProtocol from '../../../../lighthouse-core/gather/connections/cri.js';

const originalRequire = global.require;
if (typeof globalThis === 'undefined') {
Expand All @@ -22,7 +23,7 @@ if (typeof globalThis === 'undefined') {

// Load bundle, which creates a `global.runBundledLighthouse`.
// @ts-ignore - file exists if `yarn build-all` is run, but not used for types anyways.
require('../../../../dist/lighthouse-dt-bundle.js'); // eslint-disable-line
import '../../../../dist/lighthouse-dt-bundle.js'; // eslint-disable-line

global.require = originalRequire;

Expand Down Expand Up @@ -60,6 +61,6 @@ async function runLighthouse(url, configJson, testRunnerOptions = {}) {
}
}

module.exports = {
runLighthouse,
export {
runLighthouse
};
24 changes: 13 additions & 11 deletions lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
* them.
*/

const fs = require('fs').promises;
const {promisify} = require('util');
const execFileAsync = promisify(require('child_process').execFile);
import {promises as fs} from 'fs';
import {promisify} from 'util';
import {execFile} from 'child_process';

const log = require('lighthouse-logger');
import log from 'lighthouse-logger';

const assetSaver = require('../../../../lighthouse-core/lib/asset-saver.js');
const LocalConsole = require('../lib/local-console.js');
const ChildProcessError = require('../lib/child-process-error.js');
const {LH_ROOT} = require('../../../../root.js');
import assetSaver from '../../../../lighthouse-core/lib/asset-saver.js';
import {LocalConsole} from '../lib/local-console.js';
import {ChildProcessError} from '../lib/child-process-error.js';
import {LH_ROOT} from '../../../../root.js';

const execFileAsync = promisify(execFile);

/**
* Launch Chrome and do a full Lighthouse run via the Lighthouse CLI.
Expand Down Expand Up @@ -70,7 +72,7 @@ async function internalRun(url, tmpPath, configJson, options) {
const artifactsDirectory = `${tmpPath}/artifacts/`;

const args = [
`${__dirname}/../../../index.js`, // 'lighthouse-cli/index.js'
`${LH_ROOT}/lighthouse-cli/index.js`,
`${url}`,
`--output-path=${outputPath}`,
'--output=json',
Expand Down Expand Up @@ -143,6 +145,6 @@ async function internalRun(url, tmpPath, configJson, options) {
};
}

module.exports = {
runLighthouse,
export {
runLighthouse
};
4 changes: 4 additions & 0 deletions lighthouse-cli/test/smokehouse/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "module",
"//": "Any directory that uses `import ... from` or `export ...` must be type module. Temporary file until root package.json is type: module"
}
21 changes: 11 additions & 10 deletions lighthouse-cli/test/smokehouse/report-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
* against the results actually collected from Lighthouse.
*/

const cloneDeep = require('lodash.clonedeep');
const log = require('lighthouse-logger');
const LocalConsole = require('./lib/local-console.js');
import cloneDeep from 'lodash.clonedeep';
import log from 'lighthouse-logger';

const NUMBER_REGEXP = /(?:\d|\.)+/.source;
const OPS_REGEXP = /<=?|>=?|\+\/-|±/.source;
// An optional number, optional whitespace, an operator, optional whitespace, a number.
const NUMERICAL_EXPECTATION_REGEXP =
new RegExp(`^(${NUMBER_REGEXP})?\\s*(${OPS_REGEXP})\\s*(${NUMBER_REGEXP})$`);
import {LocalConsole} from './lib/local-console.js';

/**
* @typedef Difference
connorjclark marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -36,6 +31,12 @@ const NUMERICAL_EXPECTATION_REGEXP =
* @property {Difference|null} [diff]
*/

const NUMBER_REGEXP = /(?:\d|\.)+/.source;
const OPS_REGEXP = /<=?|>=?|\+\/-|±/.source;
// An optional number, optional whitespace, an operator, optional whitespace, a number.
const NUMERICAL_EXPECTATION_REGEXP =
new RegExp(`^(${NUMBER_REGEXP})?\\s*(${OPS_REGEXP})\\s*(${NUMBER_REGEXP})$`);

/**
* Checks if the actual value matches the expectation. Does not recursively search. This supports
* - Greater than/less than operators, e.g. "<100", ">90"
Expand Down Expand Up @@ -370,7 +371,7 @@ function reportAssertion(localConsole, assertion) {
* @param {{isDebug?: boolean, isBundled?: boolean}=} reportOptions
* @return {{passed: number, failed: number, log: string}}
*/
function report(actual, expected, reportOptions = {}) {
function getAssertionReport(actual, expected, reportOptions = {}) {
const localConsole = new LocalConsole();

expected = pruneExpectations(localConsole, actual.lhr, expected, reportOptions);
Expand Down Expand Up @@ -398,4 +399,4 @@ function report(actual, expected, reportOptions = {}) {
};
}

module.exports = report;
export {getAssertionReport};
27 changes: 14 additions & 13 deletions lighthouse-cli/test/smokehouse/smokehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,9 @@
* smoke tests passed.
*/

const log = require('lighthouse-logger');
const cliLighthouseRunner = require('./lighthouse-runners/cli.js').runLighthouse;
const getAssertionReport = require('./report-assert.js');
const LocalConsole = require('./lib/local-console.js');
const ConcurrentMapper = require('./lib/concurrent-mapper.js');

/* eslint-disable no-console */

/** @typedef {import('./lib/child-process-error.js')} ChildProcessError */

// The number of concurrent (`!runSerially`) tests to run if `jobs` isn't set.
const DEFAULT_CONCURRENT_RUNS = 5;
const DEFAULT_RETRIES = 0;
/** @typedef {import('./lib/child-process-error.js').ChildProcessError} ChildProcessError */

/**
* @typedef Run
Expand All @@ -42,6 +32,17 @@ const DEFAULT_RETRIES = 0;
* @property {Run[]} runs
*/

import log from 'lighthouse-logger';

import {runLighthouse as cliLighthouseRunner} from './lighthouse-runners/cli.js';
import {getAssertionReport} from './report-assert.js';
import {LocalConsole} from './lib/local-console.js';
import {ConcurrentMapper} from './lib/concurrent-mapper.js';

// The number of concurrent (`!runSerially`) tests to run if `jobs` isn't set.
const DEFAULT_CONCURRENT_RUNS = 5;
const DEFAULT_RETRIES = 0;

/**
* Runs the selected smoke tests. Returns whether all assertions pass.
* @param {Array<Smokehouse.TestDfn>} smokeTestDefns
Expand Down Expand Up @@ -226,6 +227,6 @@ function getAssertionLog(count) {
return `${count} assertion${plural}`;
}

module.exports = {
runSmokehouse,
export {
runSmokehouse
};
Loading