indexeddbshim / IndexedDBShim

A polyfill for IndexedDB using WebSql
Other
968 stars 191 forks source link

TypeError: Illegal invocation when trying to start shim within jest #318

Open tarjei opened 6 years ago

tarjei commented 6 years ago

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
brettz9 commented 6 years ago

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?

rafayepes commented 6 years ago

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)
ErikBjare commented 6 years ago

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

brettz9 commented 6 years ago

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!

andreujuanc commented 6 years ago

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

brettz9 commented 6 years ago

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

cbasah commented 4 years ago

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('', () => { it('has 1 child', () => { const tree = renderer.create().toJSON(); expect(tree.children.length).toBe(1); }); }); `