cypress.config.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. let reporterOptions = {}
  34. if (process.env.CI) {
  35. reporterOptions = {
  36. reporter: `${process.env.MONOREPO}/node_modules/cypress-multi-reporters`,
  37. reporterOptions: {
  38. configFile: 'cypress/cypress-multi-reporters.cjs',
  39. },
  40. }
  41. }
  42. export default defineConfig({
  43. defaultCommandTimeout: 10_000,
  44. fixturesFolder: 'cypress/fixtures',
  45. video: process.env.CYPRESS_VIDEO === 'true',
  46. screenshotsFolder: 'cypress/results',
  47. videosFolder: 'cypress/results',
  48. viewportHeight: 768,
  49. viewportWidth: 1024,
  50. e2e: {
  51. baseUrl: 'http://localhost',
  52. setupNodeEvents(on) {
  53. on('task', {
  54. readPdf,
  55. readFileInZip,
  56. })
  57. },
  58. specPattern,
  59. },
  60. retries: {
  61. runMode: 3,
  62. },
  63. ...reporterOptions,
  64. })