Registry / testing / throw-on

throw-on

JSON →
library0.8.0jsnpmunverified

A tiny zero-dependency TypeScript library (v0.8.0) that forces console.error/warn and network requests (fetch, XMLHttpRequest) to throw exceptions, failing tests or code paths early. Designed to catch React warnings like 'act(...)' or unmounted component updates, and unmocked network calls. Works in Node.js (>=14) and browsers. < 100 lines, fully tested, alternative to jest-fail-on-console with broader scope (not Jest-specific).

npm install throw-on
INSTALL
IMPORT
SIG · THROW-ON
T
throw-on
testingjavascriptv0.8.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

throwOnConsole
import { throwOnConsole } from 'throw-on'
import throwOnConsole from 'throw-on'
Named export, not default.
throwOnFetch
import { throwOnFetch } from 'throw-on'
const throwOnFetch = require('throw-on').throwOnFetch
ESM-only; CommonJS require works but prefer named import.
restoreConsole
import { restoreConsole } from 'throw-on'
import { restoreConsole } from 'throw-on/restoreConsole'
All exports are from the main package entry, no subpath imports.

Shows how to enable throw-on for console.error, console.warn, and fetch in a test setup, including the ignore option.

import { throwOnConsole, throwOnFetch } from 'throw-on'; // In test setup (e.g., jest.setup.js) throwOnConsole('error'); throwOnConsole('warn'); throwOnFetch(); // Original console message still printed, then throws: // "throw-on console.error: ..." // Optional: ignore certain messages by substring or regex throwOnConsole('warn', { ignore: ['act('] });
Debug
Known issues
gotchaimport { throwOnConsole, throwOnFetch } from 'throw-on'
fix
Use named imports from the package, not from subpaths.
affects: >=0.0.0
gotchathrowOnConsole replaces the console method globally; calling restoreConsole is needed to revert.
fix
Always call restoreConsole in test teardown (e.g., afterEach) to avoid side effects.
affects: >=0.0.0
gotchathrowOnFetch does not ignore same-origin or data: URLs; all fetch calls throw.
fix
Manually mock fetch in tests instead of using throwOnFetch if you need selective behavior.
affects: >=0.0.0
deprecatedthrowOnXMLHttpRequestOpen is deprecated in favor of throwOnFetch for modern codebases.
fix
Use throwOnFetch instead, as XMLHttpRequest is legacy.
affects: <0.7.0
Errors
Common errors & fixes
Cannot find module 'throw-on' or its corresponding type declarations.
Missing installation or TypeScript not resolving package types.
fix
Run npm install --save-dev throw-on and ensure tsconfig.json includes node module resolution.
throw-on console.error: ...
throwOnConsole('error') is active and console.error was called.
fix
Either fix the code causing the console.error, or add the message to the ignore list.
TypeError: throwOnConsole is not a function
Default import instead of named import.
fix
Use import { throwOnConsole } from 'throw-on' (curly braces).
Upgrade
Version history
0.8.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
18
Resources
throw-on — npm install throw-on · libregistry