Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evelynhathaway/jest-location-mock
πΊπ§ͺ Jest hooks for JSDOM location mock
https://github.com/evelynhathaway/jest-location-mock
jest jest-hooks jsdom location mock mocks url
Last synced: 9 days ago
JSON representation
πΊπ§ͺ Jest hooks for JSDOM location mock
- Host: GitHub
- URL: https://github.com/evelynhathaway/jest-location-mock
- Owner: evelynhathaway
- License: mit
- Created: 2020-10-18T20:21:29.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-16T23:55:32.000Z (5 months ago)
- Last Synced: 2024-10-14T15:37:31.716Z (25 days ago)
- Topics: jest, jest-hooks, jsdom, location, mock, mocks, url
- Language: TypeScript
- Homepage: https://npmjs.com/package/jest-location-mock
- Size: 794 KB
- Stars: 58
- Watchers: 2
- Forks: 5
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-jest - jest-location-mock
README
# Jest Location Mock
**Jest hooks for JSDOM location mock**
[![npm version](https://badgen.net/npm/v/jest-location-mock?icon=npm)](https://www.npmjs.com/package/jest-location-mock)
[![check status](https://badgen.net/github/checks/evelynhathaway/jest-location-mock/main?icon=github)](https://github.com/evelynhathaway/jest-location-mock/actions)
[![license: MIT](https://badgen.net/badge/license/MIT/blue)](/LICENSE)## Description
Ever gotten the following error when using `window.location.assign`, `reload`, or `replace`?
```txt
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
```bash
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`**
```js
module.exports = {
setupFilesAfterEnv: [
"./config/jest-setup.js"
]
};
```**`config/jest-setup.js`**
```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`.
| Parameter | Type | Description |
| --------- | ------------------- | ----------------------------------------------------------------------- |
| location | `Location` \| `URL` | Instance of `URL` to check its `href` |
| url | `string` \| `URL` | Relative or absolute `URL` |
| base | `string` \| `URL` | If the `url` parameter is relative, an base URL for the URL constructor |```ts
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](/LICENSE)