Skip to content

Commit

Permalink
Save snapshots on e2e test failures (#11786)
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy authored Jun 24, 2022
1 parent 063ee36 commit 5fd62c1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/tests-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ jobs:
env:
COMPOSE_INTERACTIVE_NO_CLI: true

- name: Upload artifacts
uses: actions/upload-artifact@v3
if: always()
with:
name: failures-artifacts
path: build/e2e-artifacts
if-no-files-found: ignore

percy:
name: Percy
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions jest-puppeteer.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ module.exports = {
product: PUPPETEER_PRODUCT,
args: ['--window-size=1600,1000'], // Same as in percy.config.yml.
},
exitOnPageError: false,
};
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@wordpress/jest-puppeteer-axe": "^4.0.2",
"babel-jest": "^28.1.0",
"expect-puppeteer": "^6.0.2",
"jest-environment-puppeteer": "^6.0.3",
"jest-extended": "^2.0.0",
"jest-json-schema": "^6.1.0",
"jest-puppeteer": "^6.1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/src/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default {
rootDir: '../../../',
resolver: '@web-stories-wp/jest-resolver',
preset: 'jest-puppeteer',
testEnvironment: '<rootDir>/packages/e2e-tests/src/puppeteerEnvironment.js',
testMatch: ['**/specs/**/*.[jt]s', '**/?(*.)spec.[jt]s'],
testPathIgnorePatterns: [
'<rootDir>/.git',
Expand Down
62 changes: 62 additions & 0 deletions packages/e2e-tests/src/puppeteerEnvironment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import { mkdir, writeFile } from 'fs/promises';
import OriginalEnvironment from 'jest-environment-puppeteer';

const ARTIFACTS_PATH =
process.env.E2E_ARTIFACTS_PATH ||
(process.env.GITHUB_WORKSPACE || process.cwd()) + '/build/e2e-artifacts';

class PuppeteerEnvironment extends OriginalEnvironment {
async setup() {
await super.setup();

try {
await mkdir(ARTIFACTS_PATH, { recursive: true });
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
}

async handleTestEvent(event, state) {
if (event.name === 'test_fn_failure') {
const testName = state.currentlyRunningTest.name;
await this.storeArtifacts(testName);
}
}

async storeArtifacts(testName) {
const datetime = new Date().toISOString().split('.')[0];
const fileName = `${testName} ${datetime}`.replaceAll(/[ :"/\\|?*]+/g, '-');

await writeFile(
`${ARTIFACTS_PATH}/${fileName}-snapshot.html`,
await this.global.page.content()
);

await this.global.page.screenshot({
path: `${ARTIFACTS_PATH}/${fileName}.jpg`,
});
}
}

export default PuppeteerEnvironment;

0 comments on commit 5fd62c1

Please sign in to comment.