Closed keerthana-inmar closed 1 year ago
Can you try the following please: DePayFi/web3-mock#jest
Let me know if that works for you.
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.
What happens if you actually install solana dependencies?
yarn add @depay/solana-web3.js --dev
npm install @depay/solana-web3.js --save-dev
?
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
Can you please just try it (to install solana-web3.js) to see if the error disappears in your situation?
Then we can try to find a solution.
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)
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?
and how does your jest.config.js
look like?
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 }
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
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
Hi, If anyone faced the same problem, follow the below steps( for nodejs ),
setup the jest file(jest.config.js) in the root folder containing below code
{
global.XMLHttpRequest = require('xhr2');
global.location = { host: undefined };
testEnvironment = 'node';
}
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
You might install ethers separately when we want to use advanced EVM code.
Thanks a lot for sharing your findings 🙏
Thank you for guiding me.
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?
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
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
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 .