Arrays, objects and destructuring
Test data is almost always arrays and objects. The most important array methods (all return a NEW array/value, leaving the original untouched):
map— transform each element:users.map(u => u.email)filter— select matching ones:orders.filter(o => o.status === 'failed')find— first matching element (orundefined)some/every— does at least one / do all pass (boolean)includes— is a value in the arraysort— CAREFUL: mutates the original and sorts as strings by default:[10, 2].sort()→[10, 2]; for numbers usesort((a, b) => a - b)
Destructuring — extracting values from objects/arrays in one line:
const { id, email } = user;const [first, second] = rows;- In function parameters:
function login({ user, pass }) {...}— Playwright'sasync ({ page }) => {...}syntax is exactly this!
Spread (...) — copying and merging: const updated = { ...user, role: 'admin' } (a copy of user with role overridden). The core pattern for test data variations.
Optional chaining (?.) — safe deep access: response.data?.user?.email — returns undefined instead of throwing when an intermediate field is missing.
The copy trap: const copy = original is NOT a copy for objects — both names point to the same object. Shallow copy: { ...original }. Deep copy (for nested objects): structuredClone(original). The "shared object" bugs that mutate test data come from here.
📚 Sources and documentation
- Arrayofficialdeveloper.mozilla.org
- Destructuringofficialdeveloper.mozilla.org
- Optional chaining (?.)officialdeveloper.mozilla.org