A mock web3 provider for Cypress built in Cyprus.
Under development and missing many request methods and listeners. See below under 'contributing' to help!
Setup the mock provider before the window loads and set it to the win.ethereum
variable. For a complete integration example, see the rLogin basic dapp test integration.
import { MockProvider } from './index'
describe('test interaction with web3', () => {
beforeEach(() => {
cy.on("window:before:load", (win) => {
win.ethereum = new MockProvider({ address, privateKey, networkVersion: 31, debug:false })
// when used with rLogin:
cy.visit('/')
cy.contains('Connect with rLogin').click()
cy.contains('MetaMask').click()
})
})
// assuming there is an item displaying the account when the user logs in
it('displays the account', () => {
cy.get('.account').should('have.text', `Account: ${address}`)
})
// additional e2e tests...
})
To handle user acceptance on provider.request({ method: 'eth_requestAccounts', params: [] })
describe('test user acceptance for eth_requestAccounts', function test(this: {
const address = '0xB98bD7C7f656290071E52D1aA617D9cB4467Fd6D'
const privateKey = 'de926db3012af759b4f24b5a51ef6afa397f04670f634aa4f48d4480417007f3'
beforeEach(async () => {
this.provider = new MockProvider({
address, privateKey, networkVersion: 31, debug: false, manualConfirmEnable: true
})
})
it('resolves with acceptance', async () => {
expect.assertions(1)
const responsePromise = this.provider.request({ method: 'eth_requestAccounts', params: [] })
.then((accounts: any) => expect(accounts[0]).toEqual(address))
this.provider.answerEnable(true)
await responsePromise
})
it('rejects with denial', async () => {
expect.assertions(1)
const responsePromise = this.provider.request({ method: 'eth_requestAccounts', params: [] })
.catch((e) => expect(e).toBeDefined())
this.provider.answerEnable(false)
await responsePromise
})
})
Additional mock methods should be added as well as the event listeners. PRs are welcomed as long as corresponding tests are included.