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

Cannot call cy.window() outside a running test. #38

Open blassaut opened 1 week ago

blassaut commented 1 week ago

Hello guys,

I am getting this Cannot call cy.window() outside a running test. error with cypress 13.14.2 when I want to test web3-mock.

Here is the simple code:

import { Given, When, Then, Before } from '@badeball/cypress-cucumber-preprocessor';
import { mock, resetMocks } from '@depay/web3-mock';

const blockchain = 'ethereum';
const accounts = ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045'];

Before(() => {
  resetMocks();
  mock({
    blockchain,
    accounts: {
      delay: 10000, // ms
      return: accounts
    }
  });
});

Given('I visit the dapp', () => {
  cy.visit('https://metamask.github.io/test-dapp/');
});

Would you have any idea why I am getting this? I'm sure I'm forgetting some configuration somewhere (I'm using typescript with Cucumber). FYI, I also tried without Cucumber in a single typescript/javascript file with describe/it as well, without success.

0xNe0x1 commented 1 week ago

Hi 👋

You could try to move the resetMocks and mock into the Given, just to see if cy.window is available then.

blassaut commented 6 days ago

Hey @0xNe0x1 , also tried this without success.

This does not work:

import { Given, When, Then, Before } from '@badeball/cypress-cucumber-preprocessor';
import { mock, resetMocks } from '@depay/web3-mock';

const blockchain = 'ethereum';
const accounts = ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045'];

Given('I visit the dapp', () => {
  resetMocks();
  mock({
    blockchain,
    accounts: {
      delay: 10000, // ms
      return: accounts
    }
  });
  cy.visit('https://metamask.github.io/test-dapp/');
});

Capture d’écran 2024-09-23 à 12 12 53

This works (but no mocks):

import { Given, When, Then, Before } from '@badeball/cypress-cucumber-preprocessor';
import { mock, resetMocks } from '@depay/web3-mock';

const blockchain = 'ethereum';
const accounts = ['0xd8da6bf26964af9d7eed9e03e53415d37aa96045'];

Given('I visit the dapp', () => {
  // resetMocks();
  // mock({
  //   blockchain,
  //   accounts: {
  //     delay: 10000, // ms
  //     return: accounts
  //   }
  // });
  cy.visit('https://metamask.github.io/test-dapp/');
});

I also tried with a classic describe/it FYI by putting the resetMocks and mock in the it, without success as well.