How to mock chrome extension functions in Cypress?

Viewed 196

I am working on a chrome extension and want to create end-to-end tests. We are using Cypress as our testing framework.

I unpack the extension as follows:

module.exports = (on, config) => {
  on('before:browser:launch', (browser, launchOptions) => {
    // supply the absolute path to an unpacked extension's folder
    // NOTE: extensions cannot be loaded in headless Chrome
    launchOptions.extensions.push('Users/jane/path/to/extension')

    return launchOptions
  })
}

currently I am able to mock http requests, but I want to be able to mock functions inside the extension in my test as well, is it possible? something like this:

import { GithubPullRequestListPageHandler } from '../../../../../extension/page-handlers/github/pull-request-list/page-handler';
import { GithubPage } from './../../../../page-objects/github/github.pageobject';
const githubPage = new GithubPage();

    describe('GithubPullRequestListPageHandler', () => {
        beforeEach(() => {
            cy.intercept('path/to/api', { statusCode: 204 }).as('check');
        
            githubPage.visit();
        });
    
        describe('when visited', () => {
            it('should work', () => {
                /** this is what I want to achieve (pseudo) - override the method's normal behavior only in the context of the test
                GithubPullRequestListPageHandler.isMatch = () => true;
                **/
                cy.wait('@check').should('have.property', 'response.statusCode', 204)
            })
        });
    })

the check http request happens only if GithubPullRequestListPageHandler.isMatch returns true. I am looking for something like: GithubPullRequestListPageHandler.isMatch = () => true;

0 Answers
Related