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(smoke): deep clone expectations. do not use p.stdout #10361

Merged
merged 3 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
36 changes: 35 additions & 1 deletion lighthouse-cli/test/smokehouse/frontends/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,48 @@
const smokeTests = require('../test-definitions/core-tests.js');
const {runSmokehouse} = require('../smokehouse.js');

/**
* JSON.parse(JSON.stringify(object)), but persists RegExps too.
* @param {*} object
*/
function deepCopy(object) {
/**
* @param {string} _
* @param {*} value
*/
function replacer(_, value) {
if (value instanceof RegExp) {
return ('__REGEXP ' + value.toString());
} else {
return value;
}
}

/**
* @param {string} _
* @param {*} value
*/
function reviver(_, value) {
if (typeof value === 'string' && value.startsWith('__REGEXP ')) {
const m = value.split('__REGEXP ')[1].match(/\/(.*)\/(.*)?/);
if (!m) return value;
return new RegExp(m[1], m[2] || '');
} else {
return value;
}
}

return JSON.parse(JSON.stringify(object, replacer), reviver);
}

/**
* @param {Smokehouse.SmokehouseLibOptions} options
*/
async function smokehouse(options) {
const {urlFilterRegex, skip, modify, ...smokehouseOptions} = options;

/** @type {Smokehouse.TestDfn[]} */
const clonedTests = JSON.parse(JSON.stringify(smokeTests));
const clonedTests = deepCopy(smokeTests);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just dev dependency why not use just use _.cloneDeep? :) https://lodash.com/docs/4.17.15#cloneDeep

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we decide to keep and maintain this ourselves instead I expect rigorous tests and edge cases ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sgtm

const modifiedTests = clonedTests.map(test => {
const modifiedExpectations = [];
for (const expected of test.expectations) {
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-cli/test/smokehouse/smokehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ async function runSmokeTestDefn(concurrentMapper, smokeTestDefn, defnOptions) {
passingTestCount++;
}

process.stdout.write(result.log);
console.log(result.log);
}

console.log(`${purpleify(id)} smoketest complete.`);
Expand Down