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

Save snapshots on e2e test failures #11786

Merged
merged 4 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,
};
1 change: 1 addition & 0 deletions packages/e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"expect-puppeteer": "^6.0.2",
"jest-extended": "^2.0.0",
"jest-json-schema": "^6.1.0",
"jest-environment-puppeteer": "^6.0.3",
"jest-puppeteer": "^6.1.0"
},
"peerDependencies": {
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);
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
} 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;