Node.js traps · Chapter 9 of 16

A default import from CommonJS

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

  • Part: Modules
  • 3 tasks
  • Node.js
  • In the all labs plan

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

Why you need this

TypeScript, bundlers and vitest's module loader all read the __esModule marker and unwrap default for you. The same import in a vitest test (its report trimmed to the result line):

import { it, expect } from 'vitest';
import totalFee from './fee.cjs';

it('charges ₹20 a seat', () => {
  expect(totalFee(3)).toBe(60);
});
 ✓ chapters/fee-demo/fee.test.js (1 test) 2ms

So the tests ran the code one way and the kiosk another. The lesson is wider than this file: test the way production loads the code.

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: crash the kiosk's printer on purpose

    Write reproduce() in repro.js. Print one ticket with printTicket from ticket-print.js (any booking: { code, show, venue, time, seat, className, price }) and resolve to the text it returned. When printTicket throws, catch the error and resolve to its message instead.

    The check runs your reproduce() twice, each time in a plain node process, the way the kiosk runs: beside the original ticket-print.js it must resolve to 'qr is not a function', and beside a fixed ticket-print.js to the printed ticket (five lines, PARDA first, its [QR <code>] line last). Try it with node repro.js, not only in vitest: vitest loads CommonJS its own way and hides the crash. reproduce() must finish within 2 seconds.

  2. Task 2

    ticket-print.js: tickets that print in Node, not only in tests

    Fix ticket-print.js so printTicket(booking) prints the ticket both when plain node runs the module, as the kiosk does, and inside vitest. It returns five lines joined with a newline: PARDA, the show, '<venue>, <time>', 'Seat <seat>, <className>, ₹<price>', and '[QR <code>]' from qr-legacy.cjs's function, and it logs printer: ticket <code> printed.

    Keep the layout, the log line and the export as they are, and do not change qr-legacy.cjs. The check uses its own bookings, prints them in a plain node process and in vitest, and compares the text.

  3. Task 3

    ticket-print.test.js: a test that would have caught it

    Write a vitest test file, ticket-print.test.js, that would have caught the crash before Tuesday's release. The check runs your file three times: beside the original ticket-print.js, where at least one test must fail; beside the fixed ticket-print.js, where every test must pass; and beside a wrong fix that imports the module's namespace and calls qr.default, where at least one test must fail.

    In vitest the original code prints fine, so a test that only imports printTicket passes the bug. Start plain node yourself with execFileSync from node:child_process, in this file's folder, have it import ticket-print.js and print a ticket, and check what it printed: the ticket's lines and its [QR <code>] line. Each test must finish within 10 seconds.

What you will learn

  • repro.js: crash the kiosk's printer on purpose
  • ticket-print.js: tickets that print in Node, not only in tests
  • ticket-print.test.js: a test that would have caught it

More in Modules

Build "A default import from CommonJS" yourself

Parda's booking backend has run on Node for a year, and Nikhil keeps handing you incidents where Node did exactly what the team told it. Each is a runtime 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 Node.js traps are free. This one comes with the all labs plan.