QA Interview Hub · Knowledge base · Quizzes · Українська

Cypress › Basics and architecture

How is the Cypress architecture built under the hood?

Cypress consists of a Node process and a browser in which the test and the application live in one tab, in two iframes. The Node process launches the browser and does what the browser cannot: files, cy.task, screenshots, video, and a WebSocket connects the two. Test code is browser code with no Node modules, and cy commands are queued and run later with retries. A different origin needs cy.origin.

Cypress · Architecture

Reading a file in a Cypress test fails with fs is not defined. Where does this code actually run?

A tester adds import fs from 'fs' to a test to read a data file and gets an error, because the browser has no such module. The same code would work in Playwright. The reason is that a Cypress test runs in the browser, not in Node. This lesson covers what Cypress consists of, how its parts communicate and what that means in practice.

How it works and why. Cypress consists of two parts that constantly talk to each other. The first is the Node process: it launches the browser, does what the browser cannot (the file system, cy.task, screenshots, video) and takes part in the network. The second is the browser. In the Test Runner the Cypress page contains two iframes: the spec iframe with the test code and the command queue and the app iframe with the application. They sit in one tab, so the test sees the DOM of the app with no protocol in between. A WebSocket connects the Node process and the browser.

The two parts of Cypress

Browser, one tabspec iframetest + cy queueapp iframethe applicationthe test sees the app DOM directlyWebSocketCypress Node processcy.task · files · screenshots · video
Figure 1. The test and the app are neighbours in the browser, and everything that needs more privileges is done by the Node process.

This layout explains two things. First, cy.* commands are only queued while the test runs and are executed later, one by one, with retries. So they do not return a value and await is not needed. The section on asynchrony explains why. Second, the test runs in the same origin and under the same browser security rules as the app. For several origins in one test there is cy.origin: it runs its block of code in the context of the second origin. Since Cypress 14 another subdomain (app.example.com and auth.example.com) is also another origin, so it needs cy.origin too. Everything the browser cannot do (read files, reach a database) is done in Node through cy.task.

Example: from fs to cy.task. An attempt to read a file in a test:

import { readFileSync } from 'node:fs'; // not in the browser
const seed = readFileSync('seeds/orders.json', 'utf8');

A Node module does not work in browser code, and the test bundle fails to build or fails at runtime. The right way: move the action into the Node process and call it from the test. The task is described in the config in setupNodeEvents and the test reaches it through cy.task:

// cypress.config.ts
import { defineConfig } from 'cypress';
import { readFileSync } from 'node:fs';

export default defineConfig({
  e2e: {
    setupNodeEvents(on) {
      on('task', {
        // runs in Node, not in the browser
        readSeed: (name: string) =>
          JSON.parse(readFileSync('seeds/' + name + '.json', 'utf8')),
      });
    },
  },
});

// in the test
cy.task('readSeed', 'orders').then((orders) => {
  cy.log('orders: ' + (orders as unknown[]).length);
});

Data between the browser and Node travels as serialised values, so functions or complex objects will not pass. Other actions outside the browser work the same way: a screenshot on failure, video, writing to a file or a database query.

Where network interception happens. cy.intercept sees and replaces the app's requests, but the path depends on the browser. According to the Cypress 16 changelog, in Chrome, Chromium and Edge traffic is intercepted on the browser's native network, while Firefox, WebKit and Electron keep the old path through a proxy in Node.

Where test traffic goes

Chrome family

Chrome, Chromium, Edge

Since Cypress 16 test traffic is intercepted on the browser’s own native network.

Others

Firefox, WebKit, Electron

As before: traffic goes through a proxy in the Node process. The forceHttp1 option (deprecated from the start, a temporary workaround) turns this path on for every browser.

Figure 2. Consequence for QA: interception behaviour can differ between browsers, so check key scenarios in the browser CI uses.
In Playwright:

Playwright has no iframe with the test next to the app. The test lives in Node and the Playwright server sends commands to the browser through a protocol. So the test has no direct DOM access (only through page.evaluate), but works freely with several tabs, domains and contexts.

Limits and alternatives. A test in the browser does not see what is outside the page: a second tab, the browser window, system dialogs. Cypress works in one tab, and several domains require cy.origin with its rules (data between contexts is passed explicitly). For files, a database, email and services outside the browser you need the Node process with cy.task. If "two users at the same time" is the main scenario, the Playwright architecture fits better.

Common mistakes.

  • Importing Node modules (fs, path with file calls, a database driver) into a test: it is browser code. Use cy.task.
  • Passing functions or classes to cy.task: arguments and results are serialised, so return plain data.
  • Expecting cy.get to return the element at once: the command is only queued.
  • Assuming cy.intercept behaves the same in every browser: Chrome-family browsers and Firefox use different interception paths.

Check yourself

A test calls fs.readFileSync and fails. Why would this work in Playwright but not in Cypress, and what should you do?

Answer

A Playwright test runs in Node, so fs is available. A Cypress test runs in the browser, which has no Node modules. Move the action into the Node process: describe a task in setupNodeEvents through on('task', ...) and call it from the test as cy.task('name', argument). Data is passed serialised.

What does it mean that the spec iframe and the app iframe live in one tab, and what follows for the DOM?

Answer

The test and the app share one browser context, so the test code can read the app DOM directly with no protocol in between. A side effect: this also explains why there is one tab and why another domain needs cy.origin.

In an interview. Junior: Cypress works as two processes. The Node process launches the browser and does what the browser cannot, such as working with files or a database through cy.task. In the browser itself two iframes live in one tab: the test and the app, so the test sees the DOM directly. A WebSocket connects Node and the browser. Middle/Senior: It follows that the test code is browser code with no Node modules, cy commands are queued and run with retries, and everything privileged goes through cy.task. Network interception depends on the browser: in Cypress 16 it goes through the browser's native network for Chrome-family browsers and through a Node proxy for Firefox, WebKit and Electron. So I check cy.intercept behaviour in the browser CI uses.

More articles in this chapter