Home

Awesome

<div align="center"> <img alt="Jest Location Mock icon" width="128" height="128" align="center" src=".github/icon.png"/>

Jest Location Mock

Jest hooks for JSDOM location mock

npm version check status license: MIT

</div>

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

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