db.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * ================================================================
  3. * E2E Test Database Reset Global Setup
  4. * ================================================================
  5. * Runs reset-db.sh before all E2E tests to ensure clean database state
  6. *
  7. * Usage:
  8. * Add to playwright.config.js:
  9. * globalSetup: path.join(__dirname, 'e2e', 'helpers', 'db.js')
  10. * ================================================================
  11. */
  12. var path = require('path');
  13. var child_process = require('child_process');
  14. var SCRIPT_PATH = path.join(__dirname, '..', 'reset-db.sh');
  15. /**
  16. * Reset database by executing reset-db.sh
  17. * This function is called as globalSetup before all E2E tests
  18. * @throws {Error} - If script execution fails
  19. */
  20. function resetDatabase() {
  21. console.log('============================================================');
  22. console.log('E2E Global Setup: Running Database Reset');
  23. console.log('============================================================');
  24. console.log('Script path: ' + SCRIPT_PATH);
  25. console.log('============================================================');
  26. try {
  27. // Execute the reset-db.sh script
  28. // Using execSync for synchronous execution in globalSetup
  29. child_process.execSync('bash ' + SCRIPT_PATH, {
  30. stdio: 'inherit',
  31. cwd: __dirname
  32. });
  33. console.log('============================================================');
  34. console.log('E2E Global Setup: Database Reset Complete');
  35. console.log('============================================================');
  36. } catch (error) {
  37. console.error('============================================================');
  38. console.error('E2E Global Setup: Database Reset FAILED');
  39. console.error('============================================================');
  40. console.error('Error: ' + error.message);
  41. console.error('============================================================');
  42. console.error('Please check:');
  43. console.error(' 1. MySQL server is running');
  44. console.error(' 2. Network access to ' + (process.env.MYSQL_HOST || '192.168.16.251') + ' is available');
  45. console.error(' 3. reset-db.sh exists at: ' + SCRIPT_PATH);
  46. console.error(' 4. seed-data.sql exists in the e2e directory');
  47. console.error('============================================================');
  48. process.exit(1);
  49. }
  50. }
  51. // Run if executed directly
  52. resetDatabase();
  53. // ================================================================
  54. // Module exports (CommonJS)
  55. // ================================================================
  56. module.exports = resetDatabase;