CodeSampleX

Ejemplo

react-dom 19.0.0: createRoot

Muestra verificada para npm react-dom 19.0.0: createRoot. El contrato se ejecutó en node 22 · linux debian/x64 · docker y pasó.

sha256:ff9a79a4b52513040c2cde5860e1b49d7a36b9ddbe5ef9ec72ba5256ea5fb8b8

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

Caso

HOW
Objetivo
verify react-dom.createRoot in pkg:npm/react-dom@19.0.0
Paquetes
Símbolos
  • react-dom.createRoot
Entorno
node 22.23.2
Creado
2026-09-06T20:59:50Z

Contrato

  1. createRoot is exported as a function from react-dom/client
  2. createRoot(null) and createRoot({}) throw Error 'Target container is not a DOM element.'
  3. createRoot(container) returns a root object with render and unmount methods
  4. root.render(element) renders React elements into the target DOM container
  5. root.unmount() unmounts the rendered tree and clears the container contents
  6. createRoot accepts optional configuration including identifierPrefix for React useId hooks

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify react-dom.createRoot in pkg:npm/react-dom@19.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/react-dom@19.0.0
Demonstrate these symbols/APIs:
  - react-dom.createRoot

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:144ef3bfe09de50745bea4980613d4261bd387e99491bdf6858a8b3f7d0a6bc8","contract":["createRoot is exported as a function from react-dom/client","createRoot(null) and createRoot({}) throw Error 'Target container is not a DOM element.'","createRoot(container) returns a root object with render and unmount methods","root.render(element) renders React elements into the target DOM container","root.unmount() unmounts the rendered tree and clears the container contents","createRoot accepts optional configuration including identifierPrefix for React useId hooks"],"goal":"verify react-dom.createRoot in pkg:npm/react-dom@19.0.0","kind":"HOW","packages":["pkg:npm/react-dom@19.0.0"],"schemaVersion":1,"symbols":["react-dom.createRoot"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"esm","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/react-dom@19.0.0"],"schemaVersion":1,"subject":"pkg:npm/react-dom@19.0.0","symbols":["react-dom.createRoot"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-react-dom-createroot",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-react-dom-createroot",
      "version": "1.0.0",
      "dependencies": {
        "react": "19.0.0",
        "react-dom": "19.0.0"
      }
    },
    "node_modules/react": {
      "version": "19.0.0",
      "resolved": "https://registry.npmjs.org/react/-/react-19.0.0.tgz",
      "integrity": "sha512-V8AVnmPIICiWpGfm6GLzCR/W5FXLchHop40W4nXBmdlEceh16rCN8O8LNWm5bh5XUX91fh7KpA+W0TgMKmgTpQ==",
      "license": "MIT",
      "engines": {
        "node": ">=0.10.0"
      }
    },
    "node_modules/react-dom": {
      "version": "19.0.0",
      "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-19.0.0.tgz",
      "integrity": "sha512-4GV5sHFG0e/0AD4X+ySy6UJd3jVl1iNsNHdpad0qhABJ11twS3TTBnseqsKurKcsNqCEFeGL3uLpVChpIO3QfQ==",
      "license": "MIT",
      "dependencies": {
        "scheduler": "^0.25.0"
      },
      "peerDependencies": {
        "react": "^19.0.0"
      }
    },
    "node_modules/scheduler": {
      "version": "0.25.0",
      "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.25.0.tgz",
      "integrity": "sha512-xFVuu11jh+xcO7JOAGJNOXld8/TcEHK/4CituBUeUb5hqxJLj9YuemAEuvm9gQ/+pgXYfbQuqAkiYu+u7YEsNA==",
      "license": "MIT"
    }
  }
}
package.json
{
  "name": "sample-react-dom-createroot",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "react": "19.0.0",
    "react-dom": "19.0.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify react-dom.createRoot in pkg:npm/react-dom@19.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/react-dom@19.0.0"
  ],
  "symbols": [
    "react-dom.createRoot"
  ]
}
src/index.mjs
import { createRoot } from "react-dom/client";

/**
 * Creates and mounts a React root into a DOM container element.
 *
 * @param {Element|Document|DocumentFragment} container
 * @param {import("react").ReactNode} element
 * @param {object} [options]
 * @returns {{ root: object, unmount: () => void }}
 */
export function createAndMountRoot(container, element, options = {}) {
  const root = createRoot(container, options);
  root.render(element);
  return {
    root,
    unmount: () => root.unmount()
  };
}

export { createRoot };
test/contract.mjs
import assert from "node:assert/strict";
import React from "react";
import { createRoot } from "react-dom/client";
import { flushSync } from "react-dom";
import { createAndMountRoot } from "../src/index.mjs";

// Setup minimal offline DOM environment for Node.js
class Node {}
class Element extends Node {}
class HTMLElement extends Element {}
class HTMLIFrameElement extends HTMLElement {}
class Document extends Node {}
class DocumentFragment extends Node {}

const window = {
  event: undefined,
  Node,
  Element,
  HTMLElement,
  HTMLIFrameElement,
  Document,
  DocumentFragment
};
globalThis.window = window;
globalThis.Node = Node;
globalThis.Element = Element;
globalThis.HTMLElement = HTMLElement;
globalThis.HTMLIFrameElement = HTMLIFrameElement;
globalThis.Document = Document;
globalThis.DocumentFragment = DocumentFragment;

function createMockDocument() {
  const docListeners = new Map();
  const doc = {
    nodeType: 9,
    nodeName: "#document",
    activeElement: null,
    defaultView: window,
    createElement(tag) {
      const elListeners = new Map();
      const elChildren = [];
      const el = {
        nodeType: 1,
        nodeName: tag.toUpperCase(),
        tagName: tag.toUpperCase(),
        childNodes: elChildren,
        children: elChildren,
        style: {},
        ownerDocument: doc,
        setAttribute(k, v) { this[k] = v; },
        getAttribute(k) { return this[k] || null; },
        appendChild(child) { elChildren.push(child); return child; },
        removeChild(child) {
          const idx = elChildren.indexOf(child);
          if (idx >= 0) elChildren.splice(idx, 1);
          return child;
        },
        insertBefore(newChild, refChild) {
          const idx = elChildren.indexOf(refChild);
          if (idx >= 0) elChildren.splice(idx, 0, newChild);
          else elChildren.push(newChild);
          return newChild;
        },
        addEventListener(event, handler) {
          if (!elListeners.has(event)) elListeners.set(event, []);
          elListeners.get(event).push(handler);
        },
        removeEventListener(event, handler) {
          if (elListeners.has(event)) {
            const arr = elListeners.get(event);
            const idx = arr.indexOf(handler);
            if (idx >= 0) arr.splice(idx, 1);
          }
        }
      };
      return el;
    },
    createTextNode(text) {
      return {
        nodeType: 3,
        nodeName: "#text",
        nodeValue: String(text),
        textContent: String(text),
        ownerDocument: doc
      };
    },
    createComment(data) {
      return {
        nodeType: 8,
        nodeName: "#comment",
        nodeValue: String(data),
        ownerDocument: doc
      };
    },
    addEventListener(event, handler) {
      if (!docListeners.has(event)) docListeners.set(event, []);
      docListeners.get(event).push(handler);
    },
    removeEventListener(event, handler) {
      if (docListeners.has(event)) {
        const arr = docListeners.get(event);
        const idx = arr.indexOf(handler);
        if (idx >= 0) arr.splice(idx, 1);
      }
    }
  };
  window.document = doc;
  return doc;
}

const doc = createMockDocument();
globalThis.document = doc;

// 1. Symbol availability: createRoot is exported as a function from react-dom/client
assert.strictEqual(typeof createRoot, "function", "createRoot must be exported as a function");

// 2. Error handling: createRoot(null) and createRoot({}) throw Error 'Target container is not a DOM element.'
assert.throws(
  () => createRoot(null),
  /Target container is not a DOM element/,
  "createRoot(null) must throw Target container is not a DOM element"
);
assert.throws(
  () => createRoot({}),
  /Target container is not a DOM element/,
  "createRoot({}) must throw Target container is not a DOM element"
);

// 3. Create root on valid DOM element: createRoot(container) returns a root object with render and unmount methods
const container = doc.createElement("div");
const root = createRoot(container);
assert.strictEqual(typeof root.render, "function", "root must have a render method");
assert.strictEqual(typeof root.unmount, "function", "root must have an unmount method");

// 4. Render element: root.render(element) renders React elements into the target DOM container
flushSync(() => {
  root.render(React.createElement("span", { id: "test-node" }, "React 19.0.0 createRoot verified"));
});
assert.strictEqual(container.childNodes.length, 1, "Container should have 1 child node after render");
assert.strictEqual(container.childNodes[0].tagName, "SPAN", "Rendered child tag should be SPAN");

// 5. Unmount root: root.unmount() unmounts the rendered tree and clears the container contents
flushSync(() => {
  root.unmount();
});
assert.strictEqual(container.childNodes.length, 0, "Container should have 0 children after unmount");

// 6. Options support: createRoot accepts optional configuration including identifierPrefix for React useId hooks
const optContainer = doc.createElement("div");
const optRoot = createRoot(optContainer, {
  identifierPrefix: "custom-prefix-"
});

function AppWithId() {
  const generatedId = React.useId();
  return React.createElement("div", { id: generatedId }, "Component with ID");
}

flushSync(() => {
  optRoot.render(React.createElement(AppWithId));
});

assert.strictEqual(optContainer.childNodes.length, 1, "Option container should have 1 child");
assert.ok(
  optContainer.childNodes[0].id && optContainer.childNodes[0].id.includes("custom-prefix-"),
  "Generated ID should contain identifierPrefix"
);

flushSync(() => {
  optRoot.unmount();
});
assert.strictEqual(optContainer.childNodes.length, 0, "Option container should be empty after unmount");

// 7. Helper verification
const helperContainer = doc.createElement("div");
let helperHandle;
flushSync(() => {
  helperHandle = createAndMountRoot(
    helperContainer,
    React.createElement("p", null, "Mounted via helper")
  );
});
assert.strictEqual(helperContainer.childNodes.length, 1, "Helper container should have 1 child");
flushSync(() => {
  helperHandle.unmount();
});
assert.strictEqual(helperContainer.childNodes.length, 0, "Helper container should be empty after unmount");

console.log("All contract assertions passed successfully.");

Seeder de origen

anónimo