IndexedDBShim icon indicating copy to clipboard operation
IndexedDBShim copied to clipboard

TypeError: Illegal invocation when trying to start shim within jest

Open tarjei opened this issue 6 years ago • 7 comments

Hi, I tried to use indeeddbshim with FBs jest, but I got:

TypeError: Illegal invocation

Here's the stacktrace: at Window.get [as indexedDB] (node_modules/indexeddbshim/dist/indexeddbshim-node.js:11625:33) at shim (node_modules/indexeddbshim/dist/indexeddbshim-node.js:11557:24) at IDBFactory.IDB.shimIndexedDB.__useShim (node_modules/indexeddbshim/dist/indexeddbshim-node.js:11619:21) at setGlobalVars (node_modules/indexeddbshim/dist/indexeddbshim-node.js:11798:35) at Object.<anonymous> (tests/setupTests.js:23:29)

And the testcode:

import setGlobalVars from 'indexeddbshim'
import Adapter from 'enzyme-adapter-react-16'
import { configure } from 'enzyme'

configure({ adapter: new Adapter() })

//global.window = global // We'll allow ourselves to use `window.indexedDB` or `indexedDB` as a global
setGlobalVars(global) // See signature below

tarjei avatar Apr 05 '18 07:04 tarjei

Our previous version (and the one I've just added, 3.6.0) have this file a lot smaller in size than your stack trace would indicate, so I'm wondering whether you are testing against the latest version?

brettz9 avatar Apr 09 '18 07:04 brettz9

Some other example:

TypeError: Illegal invocation

      at Window.get [as indexedDB] (node_modules/indexeddbshim/dist/indexeddbshim-node.js:6890:35)
      at shim (node_modules/indexeddbshim/dist/indexeddbshim-node.js:6841:16)
      at IDBFactory.IDB.shimIndexedDB.__useShim (node_modules/indexeddbshim/dist/indexeddbshim-node.js:6884:17)
      at setGlobalVars (node_modules/indexeddbshim/dist/indexeddbshim-node.js:6988:27)
      at Object.<anonymous> (jest/testSetup.js:19:1)
          at Generator.next (<anonymous>)
          at new Promise (<anonymous>)
          at Generator.next (<anonymous>)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:118:7)

rafayepes avatar May 25 '18 12:05 rafayepes

I've reproduced (on accident), no clue what's going on.

ErikBjare avatar Jun 13 '18 12:06 ErikBjare

We are attempting to follow the standardized interfaces precisely, and they are picky about how a property is accessed. What is your exact call to setGlobalVars?

As we needed a workaround for our own test environment, I put in a hack that checks for a shimNS on the this (e.g., window or global), and if present, it avoids the error. In the next version I think I will define this with config, but for now that might work as a workaround. But I'd still like to know how your Jest testSetup is invoking setGlobalVars. Thanks!

brettz9 avatar Jun 25 '18 02:06 brettz9

I just got the same issue with jest. What's the workaround? if any.

andreujuanc avatar Aug 08 '18 17:08 andreujuanc

Try setting global.shimNS = true against the code in master and see if that solves it.

brettz9 avatar Aug 08 '18 19:08 brettz9

Try setting global.shimNS = true against the code in master and see if that solves it.

How do we set this in Jest? I get the same problem when doing a test on react-native expo app.

` import React from 'react'; import renderer from 'react-test-renderer';

import App from './App';

describe('<App />', () => { it('has 1 child', () => { const tree = renderer.create(<App />).toJSON(); expect(tree.children.length).toBe(1); }); }); `

cbasah avatar Oct 18 '20 02:10 cbasah