/window-resizeto

A window.resizeTo polyfill for test environments like Jest & JSDOM (in <10 LoC)

Primary LanguageTypeScriptOtherNOASSERTION

window-resizeto

package-json releases commits
dt dy dm dw
typings build status code coverage
NPM
A window.resizeTo polyfill for test environments like Jest & JSDOM.

Installation

npm i -D window-resizeto

Usage

There are a few different ways you can use window-resizeto:

  • With Jest:

    // jest.config.js
    module.exports = {
      setupFilesAfterEnv: [
        // polyfill window.resizeTo
        'window-resizeto/polyfill'
      ]
    }
    // some-test.spec.js
    window.resizeTo(500, 500)
    // window is now resize to 500x500
  • Standalone with the polyfill:

    import 'window-resizeto/polyfill'
    
    window.resizeTo(500, 500)
    // window is now resized to 500x500
  • With the ponyfill:

    import { resizeTo } from 'window-resizeto'
    
    resizeTo(window, 500, 500)
    // window is now resized to 500x500

How it works

Basically just sets the window's outerWidth, outerHeight, innerWidth, innerHeight, and fires a resize event. The source code is currently just <10 LoC, so take a look under the hood! :)

Credits

Inspiration for creating this came from mq-polyfill and a few other code samples that are nearly exact replicas. I wanted a package I could re-use in my projects' tests instead of having to constantly create a helper file, and so window-resizeto was born!