Skip to content

Commit

Permalink
Pass test results file path as an env var
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Mar 2, 2023
1 parent cc09c6f commit d27f085
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 49 deletions.
23 changes: 9 additions & 14 deletions bin/plugin/commands/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,14 @@ function curateResults( testSuite, results ) {
* @return {Promise<WPPerformanceResults>} Performance results for the branch.
*/
async function runTestSuite( testSuite, performanceTestDirectory, runKey ) {
fs.mkdirSync( './__test-results', { recursive: true } );
const testResultsFilePath = path.resolve(
`./__test-results/${ runKey }.results.json`
);

try {
await runShellScript(
`npm run test:performance -- packages/e2e-tests/specs/performance/${ testSuite }.test.js`,
`TEST_RESULTS_FILE_PATH="${ testResultsFilePath }" npm run test:performance -- packages/e2e-tests/specs/performance/${ testSuite }.test.js`,
performanceTestDirectory
);
} catch ( error ) {
Expand All @@ -200,20 +205,10 @@ async function runTestSuite( testSuite, performanceTestDirectory, runKey ) {
throw error;
}

const resultsFile = path.join(
performanceTestDirectory,
`packages/e2e-tests/specs/performance/${ testSuite }.test.results.json`
return curateResults(
testSuite,
await readJSONFile( testResultsFilePath )
);
fs.mkdirSync( './__test-results', { recursive: true } );
fs.copyFileSync( resultsFile, `./__test-results/${ runKey }.results.json` );
const rawResults = await readJSONFile(
path.join(
performanceTestDirectory,
`packages/e2e-tests/specs/performance/${ testSuite }.test.results.json`
)
);

return curateResults( testSuite, rawResults );
}

/**
Expand Down
15 changes: 5 additions & 10 deletions packages/e2e-tests/specs/performance/front-end-block-theme.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* External dependencies
* WordPress dependencies
*/
import { basename, join } from 'path';
import { writeFileSync } from 'fs';
import { activateTheme, createURL, logout } from '@wordpress/e2e-test-utils';

/**
* WordPress dependencies
* Internal dependencies
*/
import { activateTheme, createURL, logout } from '@wordpress/e2e-test-utils';
import { writeTestResultsFile } from './utils';

describe( 'Front End Performance', () => {
const results = {
Expand All @@ -22,12 +21,8 @@ describe( 'Front End Performance', () => {
} );

afterAll( async () => {
writeTestResultsFile( __filename, results );
await activateTheme( 'twentytwentyone' );
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
} );

it( 'Report TTFB, LCP, and LCP-TTFB', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
* External dependencies
* WordPress dependencies
*/
import { basename, join } from 'path';
import { writeFileSync } from 'fs';
import { activateTheme, createURL, logout } from '@wordpress/e2e-test-utils';

/**
* WordPress dependencies
* Internal dependencies
*/
import { activateTheme, createURL, logout } from '@wordpress/e2e-test-utils';
import { writeTestResultsFile } from './utils';

describe( 'Front End Performance', () => {
const results = {
Expand All @@ -22,11 +21,7 @@ describe( 'Front End Performance', () => {
} );

afterAll( async () => {
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
writeTestResultsFile( __filename, results );
} );

it( 'Report TTFB, LCP, and LCP-TTFB', async () => {
Expand Down
10 changes: 3 additions & 7 deletions packages/e2e-tests/specs/performance/post-editor.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* External dependencies
*/
import { basename, join } from 'path';
import { writeFileSync } from 'fs';
import { join } from 'path';

/**
* WordPress dependencies
Expand All @@ -24,6 +23,7 @@ import {
import {
readFile,
deleteFile,
writeTestResultsFile,
getTypingEventDurations,
getClickEventDurations,
getHoverEventDurations,
Expand Down Expand Up @@ -82,11 +82,7 @@ describe( 'Post Editor Performance', () => {
let traceResults;

afterAll( async () => {
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
writeTestResultsFile( __filename, results );
deleteFile( traceFile );
} );

Expand Down
10 changes: 3 additions & 7 deletions packages/e2e-tests/specs/performance/site-editor.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* External dependencies
*/
import { basename, join } from 'path';
import { writeFileSync } from 'fs';
import { join } from 'path';

/**
* WordPress dependencies
Expand All @@ -24,6 +23,7 @@ import {
import {
readFile,
deleteFile,
writeTestResultsFile,
getTypingEventDurations,
getLoadingDurations,
sequence,
Expand Down Expand Up @@ -83,11 +83,7 @@ describe( 'Site Editor Performance', () => {
} );

afterAll( async () => {
const resultsFilename = basename( __filename, '.js' ) + '.results.json';
writeFileSync(
join( __dirname, resultsFilename ),
JSON.stringify( results, null, 2 )
);
writeTestResultsFile( __filename, results );

await deleteAllTemplates( 'wp_template' );
await deleteAllTemplates( 'wp_template_part' );
Expand Down
15 changes: 14 additions & 1 deletion packages/e2e-tests/specs/performance/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
/**
* External dependencies
*/
import { existsSync, readFileSync, unlinkSync } from 'fs';
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'fs';

export function writeTestResultsFile( testFilePath, results ) {
const testResultsFilePath =
process.env.TEST_RESULTS_FILE_PATH ||
// Fall back to the same directory as the test file. Useful when e.g.
// running the tests locally.
testFilePath.replace( '.js', '.results.json' );

return writeFileSync(
testResultsFilePath,
JSON.stringify( results, null, 2 )
);
}

export function readFile( filePath ) {
return existsSync( filePath )
Expand Down

0 comments on commit d27f085

Please sign in to comment.