cypress.config.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. let reporterOptions = {}
  34. if (process.env.CI) {
  35. reporterOptions = {
  36. reporter: '/overleaf/server-ce/test/node_modules/cypress-multi-reporters',
  37. reporterOptions: {
  38. configFile: 'cypress/cypress-multi-reporters.json',
  39. },
  40. }
  41. }
  42. module.exports = 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. videoUploadOnPasses: false,
  49. viewportHeight: 768,
  50. viewportWidth: 1024,
  51. e2e: {
  52. baseUrl: 'http://localhost',
  53. setupNodeEvents(on, config) {
  54. on('task', {
  55. readPdf,
  56. readFileInZip,
  57. })
  58. },
  59. specPattern,
  60. },
  61. retries: {
  62. runMode: 3,
  63. },
  64. ...reporterOptions,
  65. })