JavaScript traps · Chapter 7 of 16

Mutating shared objects

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • JavaScript
  • In the all labs plan

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.

Why you need this

push on a shared array is a perfectly good push. Nothing throws, nothing warns, and the first order looks right. Only the next one is wrong, and the change lasts as long as the module does: in a server, until it restarts.

Freezing the default does not stop it, because Object.freeze is shallow too: it stops new top-level properties, not changes inside the arrays the object holds.

const KIT = Object.freeze({ bag: 'thermal', items: ['raincoat'] });
KIT.items.push('torch');
console.log(KIT.items);
console.log(Object.isFrozen(KIT), Object.isFrozen(KIT.items));
[ 'raincoat', 'torch' ]
true false

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.js: make one customer's ginger reach the next order

    Write reproduce() in repro.js. Make a new order with newOrder from orders.js (any item), add the extra 'ginger' to it with addExtra(order, 'ginger'), then make a second new order and resolve to the second order's extras array.

    The check runs your reproduce() twice, each time with a freshly loaded orders.js: beside the original orders.js it must resolve to [ 'ginger' ] (the ginger reached an order that never asked for it), and beside a fixed orders.js it must resolve to []. So it must really make two orders and add ginger to the first. Nothing to wait for: reproduce() must finish within 2 seconds.

  2. Task 2

    orders.js: every order gets its own extras

    Fix newOrder(item) in orders.js so each new order is a copy of DEFAULT_ORDER that shares nothing with DEFAULT_ORDER or with any other order. Adding an extra to one order with addExtra changes no other order and not DEFAULT_ORDER. Changing a nested value of one order, such as its cup's kind or lid, changes no other order and not DEFAULT_ORDER. After any number of orders, DEFAULT_ORDER is exactly as it was: item 'masala chai', size 'regular', sugar 'normal', extras [] and cup { kind: 'kulhad', lid: false }.

    Keep everything else as it is: DEFAULT_ORDER's values, the item passed in, ids that go up by one, addExtra, and the two log lines (order <id>: new <item>, extras: <extras or none>, and order <id>: added <extra>). The check uses its own extras and changes an order's cup, and a fix that freezes DEFAULT_ORDER still lets the shared extras change.

  3. Task 3

    orders.test.js: a test that would have caught it

    Write a vitest test file, orders.test.js, that would have caught this bug before it reached customers. The check runs your file three times: beside the original orders.js, where at least one test must fail; beside the fixed orders.js, where every test must pass; and beside a wrong fix that freezes DEFAULT_ORDER but still shares its extras array, where at least one test must fail.

    So test what the customers saw: add an extra to one order, make a later order, and expect the later order's extras to be empty, and the first order to keep its own extra. Each test must finish within 3 seconds.

What you will learn

  • repro.js: make one customer's ginger reach the next order
  • orders.js: every order gets its own extras
  • orders.test.js: a test that would have caught it

More in Everyday traps

Build "Mutating shared objects" yourself

Tapri runs on JavaScript, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of JavaScript traps are free. This one comes with the all labs plan.