🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

vitest-location-mock

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vitest-location-mock

vitest hooks for JSDOM location mock

1.0.4
latest
Source
npm
Version published
Weekly downloads
3.1K
7.97%
Maintainers
1
Weekly downloads
 
Created
Source
Vitest Location Mock icon

Vitest Location Mock

(Fork of jest-location-mock, but for vitest)

Vitest 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 Vitest 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 vitest-location-mock

Usage

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

Setup

vite.config.js

export default defineConfig({
    setupFiles: [
        "./config/vitest-setup.js"
    ]
});

config/vitest-setup.js

// Mock `window.location` with Vitest spies and extend expect
import "vitest-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

vitest

FAQs

Package last updated on 11 Mar 2024

Did you know?

Socket

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