cypress.config.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const { defineConfig } = require('cypress')
  2. const { readPdf, readFileInZip } = require('./helpers/read-file')
  3. const fs = require('node:fs')
  4. if (process.env.CYPRESS_SHARD && !process.env.SPEC_PATTERN) {
  5. // Running Cypress on all the specs is wasteful (~1min) when only few of them
  6. // will have relevant tasks to run for a given shard. Filter the spec files
  7. // based on the existence of the shard identifier in the spec source.
  8. const files = []
  9. for (const name of fs.readdirSync('.')) {
  10. if (!name.endsWith('.spec.ts')) continue
  11. const src = fs.readFileSync(name, 'utf-8')
  12. if (!src.includes('isExcludedBySharding(')) {
  13. throw new Error(
  14. `Spec ${name} is not using sharding. Add an appropriate "'if (isExcludedBySharding('...')) return" call.`
  15. )
  16. }
  17. if (!src.includes(process.env.CYPRESS_SHARD)) continue
  18. files.push(name)
  19. }
  20. if (files.length === 0) {
  21. throw new Error(
  22. `Bad process.env.CYPRESS_SHARD=${process.env.CYPRESS_SHARD}; no spec files matched!`
  23. )
  24. }
  25. if (files.length === 1) {
  26. // Cypress does not like `{single-file}`. Make this a special case.
  27. process.env.SPEC_PATTERN = `./${files[0]}`
  28. } else {
  29. process.env.SPEC_PATTERN = `./{${files.join(',')}}`
  30. }
  31. }
  32. const specPattern = process.env.SPEC_PATTERN || './**/*.spec.ts'
  33. module.exports = defineConfig({
  34. defaultCommandTimeout: 10_000,
  35. fixturesFolder: 'cypress/fixtures',
  36. video: process.env.CYPRESS_VIDEO === 'true',
  37. screenshotsFolder: 'cypress/results',
  38. videosFolder: 'cypress/results',
  39. videoUploadOnPasses: false,
  40. viewportHeight: 768,
  41. viewportWidth: 1024,
  42. e2e: {
  43. baseUrl: 'http://localhost',
  44. setupNodeEvents(on, config) {
  45. on('task', {
  46. readPdf,
  47. readFileInZip,
  48. })
  49. },
  50. specPattern,
  51. },
  52. retries: {
  53. runMode: 3,
  54. },
  55. })