playwright.config.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { resolve } from 'node:path'
  2. import { defineConfig, devices } from '@playwright/test'
  3. /**
  4. * End-to-end tests: the app in a real browser, against a real Postgres.
  5. *
  6. * The 190-odd vitest files mock Prisma, so they prove the actions think
  7. * correctly and nothing else. These prove the parts that only break when the
  8. * pieces are assembled: migrations, the session cookie, server actions wired to
  9. * forms, and the invoice numbering that customers actually see.
  10. *
  11. * Deliberately serial against one seeded database. Parallel workers sharing a
  12. * quote counter would fail on each other's numbers rather than on real bugs.
  13. */
  14. const baseURL = process.env.E2E_BASE_URL ?? 'http://127.0.0.1:3100'
  15. const port = new URL(baseURL).port || '3100'
  16. /** The database the harness is allowed to destroy. Never the dev one. */
  17. const databaseUrl = process.env.E2E_DATABASE_URL ?? ''
  18. /** Where the mail sink listens: SMTP for the app, HTTP for the specs. */
  19. const smtpPort = process.env.E2E_SMTP_PORT ?? '1025'
  20. const mailApiPort = process.env.E2E_MAIL_API_PORT ?? '8025'
  21. /**
  22. * A mail server that delivers nothing, so the specs can read what the app
  23. * posted. Started whether or not the suite starts the app: pointed at a
  24. * server somebody else launched, that server is told to send here too.
  25. */
  26. /** Where the stand-in payment vendor listens, for the app and for the specs. */
  27. const paymentPort = process.env.E2E_PAYMENT_PORT ?? '8026'
  28. const paymentSinkUrl = `http://127.0.0.1:${paymentPort}`
  29. /**
  30. * Stripe and PayPal as far as the app can tell, with a checkout page a spec
  31. * can pay on. Started in every mode, like the mail sink: a server somebody
  32. * else launched is told to use it through the two base URLs below.
  33. */
  34. const paymentSink = {
  35. command: 'npx tsx e2e/payment-sink.ts',
  36. url: `${paymentSinkUrl}/health`,
  37. reuseExistingServer: !process.env.CI,
  38. timeout: 60_000,
  39. stdout: 'pipe' as const,
  40. stderr: 'pipe' as const,
  41. env: { E2E_PAYMENT_PORT: paymentPort },
  42. }
  43. /**
  44. * Which app the run is against. The suite's specs expect a self-hosted
  45. * install, where every feature is unlocked. `E2E_MODE=cloud` runs only
  46. * `specs/cloud`, against the same build started in cloud mode: plan limits,
  47. * the sign-up pitch and Google sign-in exist only there. One build serves
  48. * both, because the app URL baked into it is the same.
  49. */
  50. const cloud = process.env.E2E_MODE === 'cloud'
  51. /** Where the Google stand-in listens, for the app's server and for the specs. */
  52. const googlePort = process.env.E2E_GOOGLE_PORT ?? '8027'
  53. const googleStandinUrl = `http://127.0.0.1:${googlePort}`
  54. /**
  55. * Google's account chooser and token endpoint, for the cloud run. The browser
  56. * is routed to it by the specs; the app's server is pointed at it by a
  57. * preload, because better-auth has Google's endpoints written into it.
  58. */
  59. const googleStandin = {
  60. command: 'npx tsx e2e/google-standin.ts',
  61. url: `${googleStandinUrl}/health`,
  62. reuseExistingServer: !process.env.CI,
  63. timeout: 60_000,
  64. stdout: 'pipe' as const,
  65. stderr: 'pipe' as const,
  66. env: { E2E_GOOGLE_PORT: googlePort },
  67. }
  68. const mailSink = {
  69. command: 'npx tsx e2e/mail-sink.ts',
  70. url: `http://127.0.0.1:${mailApiPort}/health`,
  71. reuseExistingServer: !process.env.CI,
  72. timeout: 60_000,
  73. stdout: 'pipe' as const,
  74. stderr: 'pipe' as const,
  75. env: { E2E_SMTP_PORT: smtpPort, E2E_MAIL_API_PORT: mailApiPort },
  76. }
  77. export default defineConfig({
  78. testDir: './e2e',
  79. globalSetup: './e2e/global-setup.ts',
  80. timeout: 60_000,
  81. expect: { timeout: 10_000 },
  82. fullyParallel: false,
  83. workers: 1,
  84. forbidOnly: !!process.env.CI,
  85. retries: process.env.CI ? 1 : 0,
  86. // On CI the suite runs in shards, one job each, and every shard writes a
  87. // blob; the workflow's last job merges them into a single HTML report.
  88. reporter: process.env.CI ? [['github'], ['blob']] : [['list'], ['html', { open: 'never' }]],
  89. use: {
  90. baseURL,
  91. // Pinned, because selectors read visible text and the app speaks twelve
  92. // languages. The locale cookie is set alongside this in auth.setup.ts.
  93. locale: 'en-US',
  94. extraHTTPHeaders: { 'accept-language': 'en' },
  95. // Pinned for the same reason invoice dates are: a floating timezone turns
  96. // a date assertion into a coin toss either side of midnight.
  97. timezoneId: process.env.E2E_TZ ?? 'Europe/Oslo',
  98. trace: 'retain-on-failure',
  99. screenshot: 'only-on-failure',
  100. video: 'retain-on-failure',
  101. },
  102. projects: [
  103. { name: 'setup', testMatch: /auth\.setup\.ts/ },
  104. {
  105. name: 'chromium',
  106. use: { ...devices['Desktop Chrome'], storageState: 'e2e/.auth/owner.json' },
  107. dependencies: ['setup'],
  108. // The cloud specs need the app in cloud mode, and the rest need it
  109. // self-hosted, so each run takes only its own.
  110. ...(cloud ? { testMatch: /specs\/cloud\/.*\.spec\.ts$/ } : { testIgnore: /specs\/cloud\// }),
  111. },
  112. ],
  113. /**
  114. * The mail sink always; the app only when E2E_BASE_URL is unset, so pointing
  115. * the suite at a running container (or a staging host) is a matter of
  116. * setting one variable.
  117. *
  118. * `next start` and not `next dev`: the dev server compiles routes on first
  119. * visit, which turns the first assertion in every spec into a timeout race,
  120. * and it is not the artifact that ships anyway.
  121. */
  122. webServer: process.env.E2E_BASE_URL
  123. ? [mailSink, paymentSink, ...(cloud ? [googleStandin] : [])]
  124. : [
  125. mailSink,
  126. paymentSink,
  127. ...(cloud ? [googleStandin] : []),
  128. {
  129. // The database first, then the server, in one command: Playwright
  130. // starts this before global setup, and a server on an empty schema
  131. // fails the readiness check on every page.
  132. command: `npx tsx e2e/prepare-db.ts && npm run start -- --port ${port}`,
  133. url: baseURL,
  134. reuseExistingServer: !process.env.CI,
  135. // Sixty-odd migrations, the seed and its vehicle photos, then the
  136. // server: a cold CI runner needs longer than a warm laptop.
  137. timeout: process.env.CI ? 420_000 : 180_000,
  138. stdout: 'pipe',
  139. stderr: 'pipe',
  140. env: {
  141. E2E_DATABASE_URL: databaseUrl,
  142. DATABASE_URL: databaseUrl,
  143. NEXT_PUBLIC_APP_URL: baseURL,
  144. // Long and random enough that better-auth does not spend the run
  145. // warning about it. Throwaway: it signs sessions for a database
  146. // the harness resets, and CI generates its own per run.
  147. BETTER_AUTH_SECRET:
  148. process.env.BETTER_AUTH_SECRET ?? 'k3Qb8vZ1hN7pXtR2yJm5Ls9CwD4gFa6UeH0iOoT+PbY=',
  149. // The schedulers would otherwise tick through the run, writing to
  150. // the rows the specs are asserting on.
  151. DISABLE_BACKGROUND_JOBS: '1',
  152. // Demo mode blocks invites, billing and outbound messages. Tests want
  153. // the real behaviour, so it stays off.
  154. DEMO_MODE: 'false',
  155. // Three sign-ins per ten seconds is right for a workshop and wrong
  156. // for a suite that signs in on every test.
  157. AUTH_RATE_LIMIT: 'off',
  158. // `next start` also reads the developer's .env, which may say cloud.
  159. // Self-hosted unlocks every feature, which is what a suite that
  160. // exercises them needs; plan gates are a subject of their own.
  161. TORQVOICE_MODE: cloud ? 'cloud' : 'self-hosted',
  162. // Google sign-in exists only in cloud mode, and only with a client
  163. // configured. The preload sends the server's token exchange to the
  164. // stand-in; outside the cloud run none of this is set.
  165. ...(cloud
  166. ? {
  167. GOOGLE_AUTH_CLIENT_ID: 'e2e-google-client',
  168. GOOGLE_AUTH_CLIENT_SECRET: 'e2e-google-secret',
  169. E2E_GOOGLE_STANDIN_URL: googleStandinUrl,
  170. NODE_OPTIONS: `--import=${resolve('e2e/google-standin-preload.mjs')}`,
  171. }
  172. : {}),
  173. TZ: process.env.E2E_TZ ?? 'Europe/Oslo',
  174. // Mail goes to the sink instead of a provider. The app's SMTP
  175. // settings fall back to these when nothing is configured in the
  176. // database, which is how the seeded workshop is left.
  177. SMTP_HOST: '127.0.0.1',
  178. SMTP_PORT: smtpPort,
  179. SMTP_FROM_EMAIL: 'workshop@e2e.test',
  180. SMTP_SECURE: 'false',
  181. // Payments go to the stand-in vendor. Read only from the
  182. // environment, so nothing a workshop stores can redirect a key.
  183. STRIPE_API_BASE_URL: paymentSinkUrl,
  184. PAYPAL_API_BASE_URL: paymentSinkUrl,
  185. },
  186. },
  187. ],
  188. })