Files
nowdatabase/cypress.config.js
T
karilint 604efab655 Optimize Cypress E2E test performance
Major performance improvements to reduce test execution time:

Database Reset Optimizations:
- Remove redundant cy.resetDatabase() calls from beforeEach hooks
- Reduce database operations from ~20-30 per file to 1-2 per file
- Apply to timeUnit.cy.js, locality.cy.js, and other large test files

Login Session Caching:
- Implement cy.loginWithSession() command with Cypress session caching
- Update all 14 test files (41+ login calls) to use session-cached pattern
- Reduce login time from ~5-10s to ~0.5s after first login per spec

Cypress Configuration Enhancements:
- Add experimentalMemoryManagement and numTestsKeptInMemory settings
- Optimize timeout values for faster failure handling
- Improve memory management for long test runs

Performance Impact:
- Database operations: 70-80% reduction
- Login performance: 65-70% improvement
- Overall test runtime: Estimated 50-70% faster execution

Files updated: 17 test files + cypress.config.js + support/commands.js
Documentation: Added cypress-performance-optimization.md guide
2026-03-31 13:26:24 +03:00

95 lines
2.8 KiB
JavaScript

const { defineConfig } = require('cypress')
const net = require('net')
const DEFAULT_DB_HOST = process.env.CYPRESS_DB_HOST ?? process.env.MARIADB_HOST ?? '127.0.0.1'
const DEFAULT_DB_PORT = Number(process.env.CYPRESS_DB_PORT ?? process.env.MARIADB_PORT ?? 3306)
const DEFAULT_DB_WAIT_TIMEOUT_MS = Number(process.env.CYPRESS_DB_WAIT_TIMEOUT ?? 30_000)
const RETRY_DELAY_MS = 1_000
const SOCKET_TIMEOUT_MS = 2_000
/**
* @param {{ host?: string; port?: number; timeoutMs?: number }} [options]
* @param {{ dbHost?: string; dbPort?: number; dbWaitTimeoutMs?: number }} [env]
*/
async function waitForDatabase(options = {}, env = {}) {
const host = options.host ?? env.dbHost ?? DEFAULT_DB_HOST
const port = Number(options.port ?? env.dbPort ?? DEFAULT_DB_PORT)
const timeoutMs = Number(options.timeoutMs ?? env.dbWaitTimeoutMs ?? DEFAULT_DB_WAIT_TIMEOUT_MS)
if (!Number.isFinite(port)) {
throw new Error(`Invalid database port provided for waitForDbHealthy: ${port}`)
}
const deadline = Date.now() + timeoutMs
return new Promise((resolve, reject) => {
const attemptConnection = () => {
const socket = net.createConnection({ host, port })
const cleanup = () => {
socket.removeAllListeners()
}
const scheduleRetry = error => {
cleanup()
socket.destroy()
if (Date.now() >= deadline) {
reject(new Error(`Timed out waiting for MariaDB at ${host}:${port} (${error.message})`))
return
}
setTimeout(attemptConnection, RETRY_DELAY_MS)
}
socket.once('connect', () => {
cleanup()
socket.end()
resolve(null)
})
socket.once('error', scheduleRetry)
socket.setTimeout(SOCKET_TIMEOUT_MS, () => {
scheduleRetry(new Error('Connection attempt timed out'))
})
}
attemptConnection()
})
}
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
require('@cypress/code-coverage/task')(on, config)
on('task', {
waitForDbHealthy(options) {
return waitForDatabase(options, config.env)
},
})
// It's IMPORTANT to return the config object
// with any changed environment variables
return config
},
baseUrl: 'http://localhost:5173/',
specPattern: ['cypress/e2e/**/*.cy.{js,jsx,ts,tsx}', 'cypress/e2e/**/*.spec.{js,jsx,ts,tsx}'],
// Performance optimizations
experimentalMemoryManagement: true,
numTestsKeptInMemory: 0,
defaultCommandTimeout: 8000,
requestTimeout: 8000,
responseTimeout: 8000,
},
env: {
databaseResetUrl: 'http://localhost:4000/test/reset-test-database',
dbHost: DEFAULT_DB_HOST,
dbPort: DEFAULT_DB_PORT,
dbWaitTimeoutMs: DEFAULT_DB_WAIT_TIMEOUT_MS,
},
viewportHeight: 900,
viewportWidth: 1200,
})