Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jorisre/jest-wake-lock-mock

Mock Screen Wake Lock API `navigator.wakeLock` with ease and run your tests using Jest
https://github.com/jorisre/jest-wake-lock-mock

jest mock nosleep test wakelock

Last synced: 20 days ago
JSON representation

Mock Screen Wake Lock API `navigator.wakeLock` with ease and run your tests using Jest

Awesome Lists containing this project

README

        

Welcome to jest-wake-lock-mock 👋



npm
GitHub Workflow Status
Codecov

License: MIT


Twitter: _jorisre

> Mock [Screen Wake Lock API](https://w3c.github.io/screen-wake-lock/) _(`navigator.wakeLock`)_ with ease and run your tests using Jest

### 🏠 [Homepage](https://github.com/jorisre/jest-wake-lock-mock#readme)

## Prerequisites

- node >=10

## Install

```sh
npm i -D jest-wake-lock-mock
# or
yarn add -D jest-wake-lock-mock
```

## Usage

In your `jest.config.js` or `package.json` under `jest` section create a [`setupFiles`](https://jestjs.io/docs/en/configuration#setupfiles-array) array and add `jest-wake-lock-mock` to it.

```js
{
setupFiles: ['jest-wake-lock-mock'],
// jest config...
}
```

## Tests

Write your tests with confidence using the same [Screen Wake Lock API](https://w3c.github.io/screen-wake-lock/) api as in the browser.

**Example** ([More](https://github.com/jorisre/jest-wake-lock-mock/blob/master/test/jest-wake-lock-mock.test.ts)):

```js
const requestWakeLock = async () => {
try {
const wakeLock = await navigator.wakeLock.request('screen');

return { wakeLock };
} catch (error) {
return { error };
}
};

test('wakeLock request with success', async () => {
const { wakeLock, error } = await requestWakeLock(handleRelease);

expect(error).not.toBeDefined();
expect(wakeLock).toBeDefined();
expect(wakeLock?.type).toEqual('screen');
expect(wakeLock?.released).toBe(false);
});
```

## Author

👤 **Joris**

- Twitter: [@\_jorisre](https://twitter.com/_jorisre)
- Github: [@jorisre](https://github.com/jorisre)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/jorisre/jest-wake-lock-mock/issues).

## Show your support

Give a ⭐️ if this project helped you!

## 📝 License

Copyright © 2020 [Joris](https://github.com/jorisre).

This project is [MIT](https://github.com/jorisre/jest-wake-lock-mock/blob/master/LICENSE) licensed.

---

_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_