cypress.config.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { defineConfig } from 'cypress'
  2. import { readFileInZip, readPdf } from './helpers/read-file'
  3. import fs from '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. // Cypress's Electron process uses PackherdModuleLoader which doesn't go through
  34. // PnP hooks. Reporter packages are materialized into /tmp/cypress-reporter by
  35. // scripts/materialize-cypress-reporter.cjs at container startup.
  36. let reporterOptions = {}
  37. if (process.env.CI) {
  38. reporterOptions = {
  39. reporter: '/tmp/cypress-reporter/node_modules/cypress-multi-reporters',
  40. reporterOptions: {
  41. configFile: 'cypress/cypress-multi-reporters.cjs',
  42. },
  43. }
  44. }
  45. export default defineConfig({
  46. defaultCommandTimeout: 10_000,
  47. fixturesFolder: 'cypress/fixtures',
  48. video: process.env.CYPRESS_VIDEO === 'true',
  49. screenshotsFolder: 'cypress/results',
  50. videosFolder: 'cypress/results',
  51. viewportHeight: 768,
  52. viewportWidth: 1024,
  53. e2e: {
  54. setupNodeEvents(on) {
  55. on('task', {
  56. readPdf,
  57. readFileInZip,
  58. })
  59. },
  60. specPattern,
  61. },
  62. retries: {
  63. runMode: 3,
  64. },
  65. ...reporterOptions,
  66. })