|
// *********************************************************** |
|
// This example plugins/index.js can be used to load plugins |
|
// |
|
// You can change the location of this file or turn off loading |
|
// the plugins file with the 'pluginsFile' configuration option. |
|
// |
|
// You can read more here: |
|
// https://on.cypress.io/plugins-guide |
|
// *********************************************************** |
|
|
|
const { intersection } = require('lodash'); |
|
const glob = require('glob'); |
|
const { writeFileSync, existsSync, readFileSync, unlinkSync } = require('fs'); |
|
const { join } = require('path'); |
|
|
|
const repoRootPath = join(__dirname, '..', '..', '..', '..'); |
|
|
|
module.exports = (on, config) => { |
|
// Your other Cypress plugins here |
|
|
|
// When running non-interactively and not running storybook specs, this saves |
|
// spec files that the Jenkinsfile should retry, and saves the test cases that |
|
// have not passed yet. Saving the test cases allows the Jenkinsfile to not |
|
// fail the e2e stage if e.g. the individual tests that failed the first time |
|
// passed the second time, and to not fail if tests that passed the first time |
|
// failed the second time: |
|
on('after:run', (results) => { |
|
if ( |
|
results && |
|
results.runs.every((r) => !r.spec.relative.includes('storybook')) |
|
) { |
|
const failedSpecs = results.runs.filter((r) => r.stats.failures !== 0); |
|
let failedTests = []; |
|
for (const run of failedSpecs) { |
|
for (const test of run.tests) { |
|
if (test.state === 'failed') { |
|
failedTests.push( |
|
JSON.stringify([run.spec.relative, ...test.title]), |
|
); |
|
} |
|
} |
|
} |
|
const appE2ePath = join(repoRootPath, 'tmp', 'app-e2e'); |
|
const failedTestsPath = join(appE2ePath, 'tests-still-failing.txt'); |
|
if (existsSync(failedTestsPath)) { |
|
const prevFailedTests = readFileSync(failedTestsPath, { |
|
encoding: 'utf8', |
|
}); |
|
failedTests = intersection(prevFailedTests.split('\n'), failedTests); |
|
} |
|
const specsToRetry = failedSpecs.map((run) => |
|
join('**', run.spec.relative), |
|
); |
|
const specsToRetryPath = join(appE2ePath, 'specs-to-retry.txt'); |
|
writeFileSync(specsToRetryPath, specsToRetry.join(',')); |
|
writeFileSync(failedTestsPath, failedTests.join('\n')); |
|
|
|
if (failedTests.length === 0) { |
|
// This part deletes mocha files; modify if your config generates a |
|
// different kind of report files or just delete if you don't use |
|
// reporting: |
|
|
|
// If all tests passed at least once, delete the non-storybook mocha |
|
// results files; the tests passed anyway and deleting the files is the |
|
// only way without parsing the XML to prevent Jenkins from setting the |
|
// pipeline to "unstable" due to failures it finds in these files: |
|
const testResultsPath = join( |
|
repoRootPath, |
|
'build', |
|
'test-results', |
|
'e2e-*.xml', |
|
); |
|
for (const e2eMochaFilepath of glob.sync(testResultsPath)) { |
|
const contents = readFileSync(e2eMochaFilepath, { encoding: 'utf8' }); |
|
if (!contents.includes('storybook')) { |
|
unlinkSync(e2eMochaFilepath); |
|
} |
|
} |
|
} |
|
} |
|
}); |
|
|
|
return config; |
|
}; |