Programmatic API
peek exports its core functions for use as a Node.js/Bun library. This is useful when you want to integrate peek into your own tooling, test suites, or automation scripts.
Installation
npm install peek
# or
bun add peekExports
import {
snap,
checkPage,
createChecker,
runChecks,
formatResults,
runSceneChecks,
} from 'peek';snap(options)
Take a screenshot programmatically.
import { snap } from 'peek';
const result = await snap({
url: 'http://localhost:3000',
output: '/tmp/screenshot.png', // optional
viewport: '1920x1080', // optional, default: '1280x720'
fullPage: true, // optional, default: false
wait: 1000, // optional, ms to wait after load
waitUntil: 'networkidle2', // optional, default: 'networkidle2'
checks: true, // optional, run layout checks
noPool: false, // optional, disable browser pooling
setup: './login.ts', // optional, path to setup script
});
console.log(result.path); // Absolute path to screenshot
console.log(result.durationMs); // Time taken in ms
console.log(result.checks); // HealthResults (if checks: true)SnapOptions
| Property | Type | Default | Description |
|---|---|---|---|
url | string | - | URL to screenshot |
output | string | Auto-generated | Output file path |
viewport | string | '1280x720' | Viewport size |
fullPage | boolean | false | Capture full scrollable page |
wait | number | 0 | Wait time after page load (ms) |
waitUntil | string | 'networkidle2' | Puppeteer waitUntil event |
checks | boolean | false | Run layout checks |
checksOnly | string[] | All | Only run specific checks |
checksIgnore | string[] | None | Ignore selectors for checks |
noPool | boolean | false | Disable browser pooling |
setup | string | None | Path to setup script |
SnapResult
| Property | Type | Description |
|---|---|---|
path | string | Absolute path to screenshot file |
target | string | URL that was screenshotted |
viewport | { width, height } | Viewport used |
timestamp | string | ISO timestamp |
durationMs | number | Time taken |
checks | HealthResults | Layout check results (if enabled) |
checkPage(page, config?)
Run layout checks on an existing Puppeteer Page object. This is the simplest way to integrate peek checks into existing Puppeteer scripts.
import puppeteer from 'puppeteer-core';
import { checkPage } from 'peek';
const browser = await puppeteer.launch({ executablePath: '/path/to/chrome' });
const page = await browser.newPage();
await page.goto('http://localhost:3000');
const results = await checkPage(page, {
ignore: ['.skip-this'],
checks: {
touchTargets: { minWidth: 48, minHeight: 48 },
},
});
console.log(results.summary);
// { passed: 5, errors: 0, warnings: 2 }
await browser.close();createChecker(config)
Create a reusable checker with pre-configured settings.
import { createChecker } from 'peek';
const checker = createChecker({
ignore: ['.ad-banner'],
checks: {
overflow: true,
clickability: true,
touchTargets: false, // disabled
},
severity: {
overflow: 'error',
},
});
// Use it on multiple pages
const results1 = await checker.run(page1);
const results2 = await checker.run(page2);runChecks(page, options?)
Lower-level check runner. Takes a Puppeteer Page and optional runner options.
import { runChecks } from 'peek';
const results = await runChecks(page, {
config: {
ignore: ['.banner'],
checks: { overflow: true },
},
checks: ['overflow', 'clickability'], // Only run these
});formatResults(results, format?)
Format HealthResults into a string.
import { formatResults } from 'peek';
const text = formatResults(results, 'text'); // Human-readable
const json = formatResults(results, 'json'); // JSON string
const junit = formatResults(results, 'junit'); // JUnit XMLrunSceneChecks(manifest, config?)
Run spatial checks on a scene manifest without a browser.
import { runSceneChecks } from 'peek';
const manifest = {
room_width: 20,
room_height: 15,
player: { id: 1, name: 'Player', x: 0, y: 0 },
npcs: [
{ id: 2, name: 'NPC1', kind: 'npc', x: 3, y: 2 },
],
entities: [],
};
const results = runSceneChecks(manifest, {
checks: {
'entity-overlap': true,
'entity-bounds': true,
'entity-size': true,
'entity-visibility': false,
},
});
console.log(results.summary);Type Exports
peek exports all its TypeScript types:
import type {
SnapOptions,
SnapResult,
CheckConfig,
ChecksConfig,
HealthResults,
Issue,
Severity,
ElementInfo,
Check,
CheckResult,
CheckContext,
SceneManifest,
SceneEntity,
SceneCheckConfig,
} from 'peek';