Git Product home page Git Product logo

Comments (3)

scottysseus avatar scottysseus commented on June 16, 2024

I am encountering this same error when attempting to import keyboardjs into some test code.

Here is a snippet to demonstrate what I mean:

import { JSDOM } from 'jsdom'
import assert from 'assert'
import keyboardjs from 'keyboardjs'

// create mocked window object
const window = new JSDOM().window
const document = window.document

// pass this to keyboardjs?
keyboardjs.watch(document)

It seems like keyboardjs attempts to access the window object as it's loaded, and I am not sure how to prevent it from doing that.

from keyboardjs.

scottysseus avatar scottysseus commented on June 16, 2024

From StackOverflow:

I was able to get this to work using dynamic imports.

You can use an import function, which returns a Promise for when the module is imported.

JSDOM can be imported first, and then used to create an embedded window object which we then add to the global scope.

Then, we can import keyboardjs using the same mechanism.

Here is roughly the code I ended up with:

// import JSDOM first
import('jsdom').then(async module => {
  const window = new module.JSDOM().window

  // now set the global window object to the JSDOM window
  globalThis.window = window

  // _now_ import keyboardjs
  const keyboardjs = await import('keyboardjs')
  const document = window.document
  return { keyboardjs, document }
}).then(({ keyboardjs, document }) => {
  // test code goes here, and can use keyboardjs and document
})

from keyboardjs.

RobertWHurst avatar RobertWHurst commented on June 16, 2024

Just a quick update there, I've updated the lib to support non browser environments removing the need to perform a work around. See commit 3ab58eb

from keyboardjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.