Socket
Socket
Sign inDemoInstall

jest-location-mock

Package Overview
Dependencies
14
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    jest-location-mock

Jest hooks for JSDOM location mock


Version published
Weekly downloads
121K
decreased by-0.48%
Maintainers
1
Install size
780 kB
Created
Weekly downloads
 

Changelog

Source

v2.0.0

v1.0.10...v2.0.0 - 2023-09-04

💥 Breaking

  • Drop support node 17 and < 16.10.0 4512b25

📦 Package

  • dev: npm update and npm audit fix ab8036d
  • npm: Bump jest-diff from 27.2.0 to 29.6.4 830895f

Readme

Source
Jest Location Mock icon

Jest Location Mock

Jest hooks for JSDOM location mock

npm version check status license: MIT

Description

Ever gotten the following error when using window.location.assign, reload, or replace?

Error: Not implemented: navigation (except hash changes)

This Jest plugin fixes this error and mocks out window.location so it behaves similar to how does in the browser.

Features

  • Mock and control window.location
  • Relative URL support
  • TypeScript extend expect support

Installation

npm install --save-dev jest-location-mock

Usage

To start using Jest Location Mock, extend expect and add hooks by importing the default export in your jest setup file.

Setup

jest.config.js

module.exports = {
    setupFilesAfterEnv: [
        "./config/jest-setup.js"
    ]
};

config/jest-setup.js

// Mock `window.location` with Jest spies and extend expect
import "jest-location-mock";

Matchers

expect(location).toBeAt(url, [base])

Throws: When the URLs have a different absolute href.

ParameterTypeDescription
locationLocation | URLInstance of URL to check its href
urlstring | URLRelative or absolute URL
basestring | URLIf the url parameter is relative, an base URL for the URL constructor
it("should call assign with a relative url", () => {
    window.location.assign("/relative-url");
    expect(window.location).not.toBeAt("/");
    expect(window.location).toBeAt("/relative-url");
});

License

Copyright Evelyn Hathaway, MIT License

Keywords

FAQs

Last updated on 04 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc