11. JavaScript Design Patterns for Test Automation

Centralized Configuration & Constants

Never hardcode raw timeouts or base URLs inside test files:
// config/constants.js
export const TIMEOUTS = Object.freeze({
  SHORT: 5000,
  MEDIUM: 15000,
  LONG: 30000,
});

export const ENVIRONMENTS = {
staging: {
baseUrl: "https://staging.targetqa.com",
apiUrl: "https://api.staging.targetqa.com",
defaultTimeout: TIMEOUTS.MEDIUM,
},
production: {
baseUrl: "https://targetqa.com",
apiUrl: "https://api.targetqa.com",
defaultTimeout: TIMEOUTS.LONG,
},
};

Utility Classes & Helper Functions

export class StringFormatter {
  static extractOrderId(text) {
    const match = text.match(/Order #(\d+)/);
    if (!match) throw new Error(Order ID not found in text: ${text});
    return match[1];
  }
}

Task

  • Implement buildAuthHeader:
  • 1. Accept user object with token property.
  • 2. If token is missing, throw an Error("Missing auth token").
  • 3. Return { Authorization: `Bearer ${user.token}` }.
πŸ’‘Common doubts
Did you like the problem?
Lesson 11 of 35
JavaScript
Loading code editor…
Next