DePayFi / web3-mock

🤡 JavaScript library to mock web3 responses either by emulating web3 wallets or web3 RPC requests.
https://depay.com
MIT License
84 stars 20 forks source link

Getting solana code error while using ethereum. Error: global$1.XMLHttpRequest is not a constructor #25

Closed keerthana-inmar closed 1 year ago

keerthana-inmar commented 1 year ago

I have file called "contractTest.spec.js" under specs/services folder. The "contractTest.spec.js" file contains the sample code of advanced EVM section. In package.json, i have this line { "test" : "jest './specs/services' --forceExit }. I installed the "depay module" dependency . when i do npm test, it is throwing below error.

But i am getting error while requiring the module,

● Test suite failed to run

TypeError: global$1.XMLHttpRequest is not a constructor

  3 | // Contract.setProvider(process.env.WS_PROVIDER_URL);
  4 | console.log("window", global.window);
> 5 | const { mock , resetMocks } = require('@depay/web3-mock');
    |                               ^
  6 | const{ expect } =  require('@jest/globals');
  7 |
  8 |

  at checkTypeSupport (node_modules/@depay/solana-web3.js/dist/umd/index.js:12965:12)
  at node_modules/@depay/solana-web3.js/dist/umd/index.js:12984:39
  at node_modules/@depay/solana-web3.js/dist/umd/index.js:2:65
  at Object.<anonymous> (node_modules/@depay/solana-web3.js/dist/umd/index.js:5:3)
  at node_modules/@depay/web3-mock/dist/umd/index.js:3:138
  at Object.<anonymous> (node_modules/@depay/web3-mock/dist/umd/index.js:6:2)
  at Object.require (specs/services/contractService.spec.js:5:31)

I am using ubuntu version 22.04 Here are the other version details,

node - v16.17.1 @depay/web3-mock@13.22.11 jest@29.3.1

Please guide me to approach the error .

10xSebastian commented 1 year ago

Can you try the following please: DePayFi/web3-mock#jest

Let me know if that works for you.

keerthana-inmar commented 1 year ago

Hi, I create a setup.js under my test folder "specs/services/setup.js" and i installed the dev dependency xhr2 and put these lines.

// web3-mock polyfills: global.XMLHttpRequest = require('xhr2') global.location = { host: undefined }

But still it throws the same error.

10xSebastian commented 1 year ago

What happens if you actually install solana dependencies?

yarn add @depay/solana-web3.js --dev
npm install @depay/solana-web3.js --save-dev

?

keerthana-inmar commented 1 year ago

I would like to mock the ethereum contract functions(user defined function using my own contract API ). So i tried installing this below dependency. npm install --save-dev @depay/web3-mock I have not installed the solana-web3.js

10xSebastian commented 1 year ago

Can you please just try it (to install solana-web3.js) to see if the error disappears in your situation?

10xSebastian commented 1 year ago

Then we can try to find a solution.

keerthana-inmar commented 1 year ago

I am getting the same error after i installing solana dependency.

FAIL  specs/services/contract.spec.js
  ● Test suite failed to run

    TypeError: global$1.XMLHttpRequest is not a constructor

       6 |
       7 | const { expect } = require('@jest/globals');
    >  8 | const { mock, resetMocks } = require('@depay/web3-mock');
         |                              ^
       9 |
      10 | describe('something',  ()=> {
      11 |     // web3-mock polyfills:

      at checkTypeSupport (node_modules/@depay/solana-web3.js/dist/umd/index.js:12965:12)
      at node_modules/@depay/solana-web3.js/dist/umd/index.js:12984:39
      at node_modules/@depay/solana-web3.js/dist/umd/index.js:2:65
      at Object.<anonymous> (node_modules/@depay/solana-web3.js/dist/umd/index.js:5:3)
      at node_modules/@depay/web3-mock/dist/umd/index.js:3:138
      at Object.<anonymous> (node_modules/@depay/web3-mock/dist/umd/index.js:6:2)
10xSebastian commented 1 year ago

can you add

// web3-mock polyfills:
console.log('IS MY SETUP EVEN LOADED?')
global.XMLHttpRequest = require('xhr2')
global.location = { host: undefined }

to your setup.js and check if IS MY SETUP EVEN LOADED? appears during your test run?

10xSebastian commented 1 year ago

and how does your jest.config.js look like?

keerthana-inmar commented 1 year ago

The message is not showing while running the test. My structure is like below myproject contains some folders , pacakge.json, etc. one among them is spec folder ,which again contains some folders that have jest test cases. one among them is contract test. myproject/specs/services/contract.spec.js under the spec folder(myproject/specs/), i have created the setup.js file which contains // web3-mock polyfills: console.log(" ===loaded==="); global.XMLHttpRequest = require('xhr2') global.location = { host: undefined }

keerthana-inmar commented 1 year ago

i donot have jest.config.js file. so i created one in the place where the package.json file exist. jest.config.js contains same as setup.js

10xSebastian commented 1 year ago

Please setup jest correctly before we can investigate. Currently it sounds like your jest is just not configured correctly.

Have a look at other libraries that integrate web3-mock without problems in a jest setup: e.g. https://github.com/DePayFi/widgets

keerthana-inmar commented 1 year ago

Hi, If anyone faced the same problem, follow the below steps( for nodejs ),

  1. setup the jest file(jest.config.js) in the root folder containing below code

    {
    global.XMLHttpRequest = require('xhr2');
    global.location = { host: undefined };
    testEnvironment = 'node';
    }
  2. If you running test case from package.json, then add the below line under the script section to add the config "jest 'your test folder' --config ./jest.config.js

  3. You might install ethers separately when we want to use advanced EVM code.

10xSebastian commented 1 year ago

Thanks a lot for sharing your findings 🙏

keerthana-inmar commented 1 year ago

Thank you for guiding me.

keerthana-inmar commented 1 year ago

Hi, When i try to run tests which contains in sub folders. One among them is the web3 mock tests. when i run npm test, it is taking the jest.config too. contractService.spec.js contains the web3 mock tests

PASS specs/controller/registryService.spec.js FAIL specs/services/contractService.spec.js ● Test suite failed to run

TypeError: global$1.XMLHttpRequest is not a constructor

  1 | const { expect } = require('@jest/globals');
> 2 | const { mock, resetMocks } = require('@depay/web3-mock');
    |                              ^
  3 | const { ethers } =  require('ethers');
  4 |

 Could you help me?
keerthana-inmar commented 1 year ago

Hi, I resolved by setting varibale "setupFilesAfterEnv" under jest in the pacakage.json. Since you setup in package.json, you do not need to send like this npm test --config "jest.config.js". You can simply do npm test. example: name: "server-1", "jest": { "setupFilesAfterEnv": ["./specs/jestSetup.js"] } reference :https://til.hashrocket.com/posts/dknpxv9kwo-configure-jest-to-run-a-test-setup-file