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-mockVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to set up `jest-useragent-mock` with Jest's `setupFiles`, mock `navigator.userAgent` for specific tests, and ensure cleanup after each test.
Always include `afterEach(() => { clear(); });` in your test files or a shared setup script to reset the user agent mock.Ensure your Jest configuration sets `testEnvironment: 'jsdom'` for test files that interact with browser globals like `window.navigator`.
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.
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.
Configure Jest to use a browser-like environment by adding `testEnvironment: 'jsdom'` to your `jest.config.js` or within the test file's docblock.
Set `testEnvironment: 'jsdom'` in your Jest configuration.
Ensure `jest-useragent-mock` is correctly listed in `setupFiles` in `jest.config.js` and that `clear()` is called in an `afterEach` hook.