CodeSampleX

Exemple

happy-dom 20.11.2: Capture and drain Happy DOM console output through VirtualConsolePrinter, including grouped entries, filtered string reads, and custom-console bypass behavior

Échantillon vérifié pour npm happy-dom 20.11.2: Capture and drain Happy DOM console output through VirtualConsolePrinter, including grouped entries, filtered…

sha256:3098b65e11bf49d14ab84c27e05ab483a1e7c1eeae701327dadd384fd3ad8cd0

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
3
Clés de signature qui l’ont compilé
3
Environnement déclaré node linux x64 node node npm

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-17
node 22 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1
2026-08-18
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-09-07

Cas

HOW
Objectif
Capture and drain Happy DOM console output through VirtualConsolePrinter, including grouped entries, filtered string reads, and custom-console bypass behavior
Paquets
Symboles
  • Window
  • console.group
  • VirtualConsolePrinter.read
  • VirtualConsolePrinter.readAsString
  • VirtualConsoleLogLevelEnum
Environnement
node
Créé
2026-08-17T01:52:28Z

Contrat

  1. A default Window buffers log, warn, and error calls as exact structured entries with type, level, message, and null group metadata
  2. console.group records a group entry and attaches the same group metadata to nested warn and error entries
  3. VirtualConsolePrinter.read returns the buffered entries and destructively drains the queue
  4. readAsString with an error-level filter formats matching entries while also draining unmatched entries
  5. Supplying a custom console routes log calls to that console and leaves the Window virtual console queue empty

Fichiers

  • csx.json
  • package-lock.json
  • package.json
  • test/contract.mjs

Télécharger l’artefact source (tar.gz)

Code source

csx.json
{"case":{"believed":"Happy DOM writes console output directly to the host console, and filtering virtual console output leaves unmatched entries queued for a later read.","caseId":"case:sha256:d7645b1d8fbe427ec561977768a788c4420e14e73450e1a23db3a323627b6b86","contract":["A default Window buffers log, warn, and error calls as exact structured entries with type, level, message, and null group metadata","console.group records a group entry and attaches the same group metadata to nested warn and error entries","VirtualConsolePrinter.read returns the buffered entries and destructively drains the queue","readAsString with an error-level filter formats matching entries while also draining unmatched entries","Supplying a custom console routes log calls to that console and leaves the Window virtual console queue empty"],"goal":"Capture and drain Happy DOM console output through VirtualConsolePrinter, including grouped entries, filtered string reads, and custom-console bypass behavior","kind":"HOW","packages":["pkg:npm/happy-dom@20.11.2"],"schemaVersion":1,"symbols":["Window","console.group","VirtualConsolePrinter.read","VirtualConsolePrinter.readAsString","VirtualConsoleLogLevelEnum"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","language":"node","os":"linux","packageManager":"npm","runtime":"node","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/happy-dom@20.11.2"],"schemaVersion":1,"symbols":["Window","console.group","VirtualConsolePrinter.read","VirtualConsolePrinter.readAsString","VirtualConsoleLogLevelEnum"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "happy-dom-console-contract",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "happy-dom-console-contract",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "happy-dom": "20.11.2"
      }
    },
    "node_modules/@types/node": {
      "version": "26.2.0",
      "resolved": "https://registry.npmjs.org/@types/node/-/node-26.2.0.tgz",
      "integrity": "sha512-5IviulTZeRNp2vAJ514cc/HUlY5nZ9fCbq9DMyC52BrhFZACo3nI0R7qBxhQmo/d27NFe96ur/b7Wwxklda+kg==",
      "license": "MIT",
      "dependencies": {
        "undici-types": "~8.3.0"
      }
    },
    "node_modules/@types/whatwg-mimetype": {
      "version": "3.0.2",
      "resolved": "https://registry.npmjs.org/@types/whatwg-mimetype/-/whatwg-mimetype-3.0.2.tgz",
      "integrity": "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA==",
      "license": "MIT"
    },
    "node_modules/@types/ws": {
      "version": "8.18.1",
      "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz",
      "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==",
      "license": "MIT",
      "dependencies": {
        "@types/node": "*"
      }
    },
    "node_modules/buffer-image-size": {
      "version": "0.6.4",
      "resolved": "https://registry.npmjs.org/buffer-image-size/-/buffer-image-size-0.6.4.tgz",
      "integrity": "sha512-nEh+kZOPY1w+gcCMobZ6ETUp9WfibndnosbpwB1iJk/8Gt5ZF2bhS6+B6bPYz424KtwsR6Rflc3tCz1/ghX2dQ==",
      "license": "MIT",
      "dependencies": {
        "@types/node": "*"
      },
      "engines": {
        "node": ">=4.0"
      }
    },
    "node_modules/entities": {
      "version": "7.0.1",
      "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz",
      "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==",
      "license": "BSD-2-Clause",
      "engines": {
        "node": ">=0.12"
      },
      "funding": {
        "url": "https://github.com/fb55/entities?sponsor=1"
      }
    },
    "node_modules/happy-dom": {
      "version": "20.11.2",
      "resolved": "https://registry.npmjs.org/happy-dom/-/happy-dom-20.11.2.tgz",
      "integrity": "sha512-7MB+bJLkxu3SowAfBJbjW+c55kNz5tkR45gu2qzrxznezhLeN5YIlJbwUgSzlGc+qWoZ8Ykg71H5ezz69xixrw==",
      "license": "MIT",
      "dependencies": {
        "@types/node": ">=20.0.0",
        "@types/whatwg-mimetype": "^3.0.2",
        "@types/ws": "^8.18.1",
        "buffer-image-size": "^0.6.4",
        "entities": "^7.0.1",
        "whatwg-mimetype": "^3.0.0",
        "ws": "^8.21.0"
      },
      "engines": {
        "node": ">=20.0.0"
      }
    },
    "node_modules/undici-types": {
      "version": "8.3.0",
      "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-8.3.0.tgz",
      "integrity": "sha512-j375ScV60dom+YkPFIfTLcOiPxkN/buHz5GobjLhixFuANaNs3C9l4GmrWqejgXWJ7BbJcFYpTEUkS1Ge8bpZQ==",
      "license": "MIT"
    },
    "node_modules/whatwg-mimetype": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz",
      "integrity": "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==",
      "license": "MIT",
      "engines": {
        "node": ">=12"
      }
    },
    "node_modules/ws": {
      "version": "8.21.3",
      "resolved": "https://registry.npmjs.org/ws/-/ws-8.21.3.tgz",
      "integrity": "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw==",
      "license": "MIT",
      "engines": {
        "node": ">=10.0.0"
      },
      "peerDependencies": {
        "bufferutil": "^4.0.1",
        "utf-8-validate": ">=5.0.2"
      },
      "peerDependenciesMeta": {
        "bufferutil": {
          "optional": true
        },
        "utf-8-validate": {
          "optional": true
        }
      }
    }
  }
}
package.json
{
  "name": "happy-dom-console-contract",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "keywords": [],
  "author": "",
  "license": "MIT-0",
  "type": "commonjs",
  "dependencies": {
    "happy-dom": "20.11.2"
  }
}
test/contract.mjs
import assert from 'node:assert/strict';
import { Window, VirtualConsoleLogLevelEnum } from 'happy-dom';

const window = new Window();
window.console.log('App started', 1);
window.console.warn('Deprecated API used');
window.console.error('Failed to load asset');

const entries = window.happyDOM.virtualConsolePrinter.read();
assert.strictEqual(entries.length, 3);
assert.deepStrictEqual(entries[0], {
  type: 'log',
  level: VirtualConsoleLogLevelEnum.log,
  message: ['App started', 1],
  group: null
});
assert.deepStrictEqual(entries[1], {
  type: 'warn',
  level: VirtualConsoleLogLevelEnum.warn,
  message: ['Deprecated API used'],
  group: null
});
assert.deepStrictEqual(entries[2], {
  type: 'error',
  level: VirtualConsoleLogLevelEnum.error,
  message: ['Failed to load asset'],
  group: null
});
assert.deepStrictEqual(window.happyDOM.virtualConsolePrinter.read(), []);

window.console.group('Request');
window.console.warn('Retrying request', 2);
window.console.error('Request failed');
window.console.groupEnd();

const group = {
  id: 1,
  label: 'Request',
  collapsed: false,
  parent: null
};
assert.deepStrictEqual(window.happyDOM.virtualConsolePrinter.read(), [
  {
    type: 'group',
    level: VirtualConsoleLogLevelEnum.log,
    message: ['Request'],
    group
  },
  {
    type: 'warn',
    level: VirtualConsoleLogLevelEnum.warn,
    message: ['Retrying request', 2],
    group
  },
  {
    type: 'error',
    level: VirtualConsoleLogLevelEnum.error,
    message: ['Request failed'],
    group
  }
]);

window.console.info('Background sync active');
window.console.error('Database connection timeout');
assert.strictEqual(
  window.happyDOM.virtualConsolePrinter.readAsString(VirtualConsoleLogLevelEnum.error),
  'Database connection timeout\n'
);
assert.strictEqual(window.happyDOM.virtualConsolePrinter.readAsString(), '');

const customSink = [];
const customWindow = new Window({
  console: {
    log: (...args) => customSink.push(args.join(' ')),
    warn: () => {},
    error: () => {}
  }
});
customWindow.console.log('Redirected to external sink');
assert.deepStrictEqual(customSink, ['Redirected to external sink']);
assert.deepStrictEqual(customWindow.happyDOM.virtualConsolePrinter.read(), []);

Seeder d'origine

csx-seed