Registry / testing / jest-useragent-mock

jest-useragent-mock

JSON →
library0.1.1jsnpmunverified

jest-useragent-mock is a lightweight utility designed to facilitate the mocking of `navigator.userAgent` within Jest unit tests. It provides simple functions, `mockUserAgent` and `clear`, to set and reset the browser's user-agent string, making it easier to test browser-specific logic or user-agent dependent features. The current stable version is 0.1.1. Given its specific scope and low version, it operates on an infrequent release cadence, appearing to be in a maintenance state with no recent updates since its initial release. Its key differentiator is its singular focus on `navigator.userAgent` mocking, directly integrating with Jest's setup files for global application across test suites, distinguishing it from broader mocking libraries by offering a specialized, minimalist solution.

npm install jest-useragent-mock
INSTALL
IMPORT
SIG · JEST-USERAGENT-MOC
J
jest-useragent-mock
testingjavascriptv0.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

mockUserAgent
import { mockUserAgent } from 'jest-useragent-mock';
const { mockUserAgent } = require('jest-useragent-mock');
The library primarily uses ES modules. While CommonJS might technically work, the recommended approach, especially with TypeScript, is ES module imports.
clear
import { clear } from 'jest-useragent-mock';
import clear from 'jest-useragent-mock';
`clear` is a named export, not a default export. Ensure correct destructuring from the module.

Demonstrates how to set up `jest-useragent-mock` with Jest's `setupFiles`, mock `navigator.userAgent` for specific tests, and ensure cleanup after each test.

import { clear, mockUserAgent } from 'jest-useragent-mock'; // jest.config.js should include setupFiles: ["jest-useragent-mock"] describe('User-Agent Dependent Feature', () => { afterEach(() => { // It's crucial to clear the mock after each test to prevent side effects // and ensure isolation between test cases. clear(); }); it('should detect a Chrome user agent correctly', () => { const chromeAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'; mockUserAgent(chromeAgent); expect(window.navigator.userAgent).toEqual(chromeAgent); // Example of browser detection logic (simplistic) const isChrome = window.navigator.userAgent.includes('Chrome') && !window.navigator.userAgent.includes('Edg'); expect(isChrome).toBe(true); }); it('should detect a Firefox user agent correctly', () => { const firefoxAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0'; mockUserAgent(firefoxAgent); expect(window.navigator.userAgent).toEqual(firefoxAgent); const isFirefox = window.navigator.userAgent.includes('Firefox'); expect(isFirefox).toBe(true); }); });
Debug
Known issues
gotchaFailing to call `clear()` after each test can lead to global state pollution. Mocks will persist across tests, causing unpredictable behavior and flaky test results.
fix
Always include `afterEach(() => { clear(); });` in your test files or a shared setup script to reset the user agent mock.
affects: >=0.1.0
breakingThis package is explicitly designed for Jest and relies on a browser-like environment (e.g., `testEnvironment: 'jsdom'`). Using it in a Node.js-only Jest environment (`testEnvironment: 'node'`) will result in `window` or `navigator` being undefined.
fix
Ensure your Jest configuration sets `testEnvironment: 'jsdom'` for test files that interact with browser globals like `window.navigator`.
affects: >=0.1.0
gotchaThe package currently resides at a low version (0.1.1) and has seen no updates in several years. While functional for its stated purpose, the lack of active development means no new features, bug fixes, or compatibility updates for newer browser/Node.js versions or Jest major releases should be expected. Use with caution for long-term projects.
fix
Consider pinning the package version to avoid unexpected dependency resolution issues. For projects requiring active maintenance or advanced features, evaluate alternative, more actively developed mocking solutions or custom Jest setups.
affects: <=0.1.1
gotchaThe library works by modifying `window.navigator.userAgent` directly. If other parts of your test setup or application code also modify this global property, conflicts can occur. Ensure `jest-useragent-mock` is the sole modifier in your test environment for this specific property.
fix
Review your test suite and application code for other direct manipulations of `window.navigator.userAgent`. Isolate its usage to `jest-useragent-mock` within your Jest tests.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: window is not defined
Jest is running tests in a Node.js environment (`testEnvironment: 'node'`) which lacks browser globals like `window`.
fix
Configure Jest to use a browser-like environment by adding `testEnvironment: 'jsdom'` to your `jest.config.js` or within the test file's docblock.
TypeError: Cannot read properties of undefined (reading 'userAgent')
`navigator` or `window.navigator` is undefined, typically due to the same reason as 'window is not defined'.
fix
Set `testEnvironment: 'jsdom'` in your Jest configuration.
Expected: 'test-agent' Received: 'default-agent' (or an unexpected user agent string)
The mock set by `mockUserAgent` is not being applied, or a previous mock was not cleared, leading to an incorrect `userAgent` value.
fix
Ensure `jest-useragent-mock` is correctly listed in `setupFiles` in `jest.config.js` and that `clear()` is called in an `afterEach` hook.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies
jestrequiredThis package is a Jest test utility and requires Jest to run and function correctly. It relies on Jest's test environment and global setup capabilities.
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources